1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-25 05:23:13 +03:00

Remove spec non-compliant extended glob format (#3423)

* Remove spec non-compliant extended glob format

* Simplify

* Remove tests for non spec compliant behaviour

* Remove stale rules
This commit is contained in:
Michael Telatynski
2023-05-31 10:05:55 +01:00
committed by GitHub
parent b29e1e9d21
commit 56c5375bbd
4 changed files with 26 additions and 100 deletions

View File

@@ -357,27 +357,14 @@ export function escapeRegExp(string: string): string {
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
export function globToRegexp(glob: string, extended = false): string {
// From
// https://github.com/matrix-org/synapse/blob/abbee6b29be80a77e05730707602f3bbfc3f38cb/synapse/push/__init__.py#L132
// Because micromatch is about 130KB with dependencies,
// and minimatch is not much better.
const replacements: [RegExp, string | ((substring: string, ...args: any[]) => string)][] = [
[/\\\*/g, ".*"],
[/\?/g, "."],
];
if (!extended) {
replacements.push([
/\\\[(!|)(.*)\\]/g,
(_match: string, neg: string, pat: string): string =>
["[", neg ? "^" : "", pat.replace(/\\-/, "-"), "]"].join(""),
]);
}
return replacements.reduce(
// https://github.com/microsoft/TypeScript/issues/30134
(pat, args) => (args ? pat.replace(args[0], args[1] as any) : pat),
escapeRegExp(glob),
);
/**
* Converts Matrix glob-style string to a regular expression
* https://spec.matrix.org/v1.7/appendices/#glob-style-matching
* @param glob - Matrix glob-style string
* @returns regular expression
*/
export function globToRegexp(glob: string): string {
return escapeRegExp(glob).replace(/\\\*/g, ".*").replace(/\?/g, ".");
}
export function ensureNoTrailingSlash(url: string): string;