mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Simplify or optimize regexes with polynomial time worst cases (#44197)
* Simplify or optimize regexes with polynomial time worst cases * PR feedback & cleanup Co-authored-by: David Michon <dmichon-msft@users.noreply.github.com> * Use builtin scanner function for checking whitespace in fallback method (its faster) Co-authored-by: David Michon <dmichon-msft@users.noreply.github.com>
This commit is contained in:
co-authored by
David Michon
parent
2203228b62
commit
fcabb5c0cc
@@ -9094,7 +9094,7 @@ namespace ts {
|
||||
if (namedArgRegExCache.has(name)) {
|
||||
return namedArgRegExCache.get(name)!;
|
||||
}
|
||||
const result = new RegExp(`(\\s${name}\\s*=\\s*)('|")(.+?)\\2`, "im");
|
||||
const result = new RegExp(`(\\s${name}\\s*=\\s*)(?:(?:'([^']*)')|(?:"([^"]*)"))`, "im");
|
||||
namedArgRegExCache.set(name, result);
|
||||
return result;
|
||||
}
|
||||
@@ -9118,16 +9118,17 @@ namespace ts {
|
||||
return; // Missing required argument, don't parse
|
||||
}
|
||||
else if (matchResult) {
|
||||
const value = matchResult[2] || matchResult[3];
|
||||
if (arg.captureSpan) {
|
||||
const startPos = range.pos + matchResult.index + matchResult[1].length + matchResult[2].length;
|
||||
const startPos = range.pos + matchResult.index + matchResult[1].length + 1;
|
||||
argument[arg.name] = {
|
||||
value: matchResult[3],
|
||||
value,
|
||||
pos: startPos,
|
||||
end: startPos + matchResult[3].length
|
||||
end: startPos + value.length
|
||||
};
|
||||
}
|
||||
else {
|
||||
argument[arg.name] = matchResult[3];
|
||||
argument[arg.name] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9145,7 +9146,7 @@ namespace ts {
|
||||
}
|
||||
|
||||
if (range.kind === SyntaxKind.MultiLineCommentTrivia) {
|
||||
const multiLinePragmaRegEx = /\s*@(\S+)\s*(.*)\s*$/gim; // Defined inline since it uses the "g" flag, which keeps a persistent index (for iterating)
|
||||
const multiLinePragmaRegEx = /@(\S+)(\s+.*)?$/gim; // Defined inline since it uses the "g" flag, which keeps a persistent index (for iterating)
|
||||
let multiLineMatch: RegExpExecArray | null;
|
||||
while (multiLineMatch = multiLinePragmaRegEx.exec(text)) {
|
||||
addPragmaForMatch(pragmas, range, PragmaKindFlags.MultiLine, multiLineMatch);
|
||||
@@ -9170,7 +9171,7 @@ namespace ts {
|
||||
function getNamedPragmaArguments(pragma: PragmaDefinition, text: string | undefined): {[index: string]: string} | "fail" {
|
||||
if (!text) return {};
|
||||
if (!pragma.args) return {};
|
||||
const args = text.split(/\s+/);
|
||||
const args = trimString(text).split(/\s+/);
|
||||
const argMap: {[index: string]: string} = {};
|
||||
for (let i = 0; i < pragma.args.length; i++) {
|
||||
const argument = pragma.args[i];
|
||||
|
||||
Reference in New Issue
Block a user