Skip to content

Commit

Permalink
Attempt to fix polynomial regular expression warning in formatter-tem…
Browse files Browse the repository at this point in the history
…plate by using separate patterns.
  • Loading branch information
DavidAnson committed Nov 9, 2024
1 parent 70a3324 commit 8d0501b
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions formatter-template/markdownlint-cli2-formatter-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

"use strict";

// eslint-disable-next-line prefer-named-capture-group
const tokenRe = /\$\{(fileName|lineNumber|columnNumber|ruleName|ruleDescription|ruleInformation|errorContext|errorDetail)(?:([:!])([^{}]*\{[^{}]+\}[^{}]*|[^}]+))?\}/igu;
// eslint-disable-next-line no-template-curly-in-string
const defaultTemplate = "fileName=\"${fileName}\" lineNumber=${lineNumber} ${columnNumber:columnNumber=${columnNumber} }ruleName=${ruleName} ruleDescription=\"${ruleDescription}\" ruleInformation=${ruleInformation} errorContext=\"${errorContext}\" errorDetail=\"${errorDetail}\"";

// Use separate regular expressions to avoid a polynomial worst case
const tokenRes = [ "fileName", "lineNumber", "columnNumber", "ruleName", "ruleDescription", "ruleInformation", "errorContext", "errorDetail" ].
map((token) => new RegExp(`\\$\\{(${token})(?:([:!])([^{}]*\\{[^{}]+\\}[^{}]*|[^}]+))?\\}`, "gu"));

// Output markdownlint-cli2 results using a template
const outputFormatter = (options, params) => {
const { logError, results } = options;
const template = params?.template ||
// eslint-disable-next-line no-template-curly-in-string
"fileName=\"${fileName}\" lineNumber=${lineNumber} ${columnNumber:columnNumber=${columnNumber} }ruleName=${ruleName} ruleDescription=\"${ruleDescription}\" ruleInformation=${ruleInformation} errorContext=\"${errorContext}\" errorDetail=\"${errorDetail}\"";
const template = params?.template || defaultTemplate;

for (const result of results) {
const tokenToResult = {
Expand Down Expand Up @@ -43,9 +45,10 @@ const outputFormatter = (options, params) => {
}
};

const output = template.
replaceAll(tokenRe, replacer).
replaceAll(tokenRe, replacer);
let output = template;
for (const tokenRe of tokenRes) {
output = output.replaceAll(tokenRe, replacer).replaceAll(tokenRe, replacer);
}
logError(output);
}
};
Expand Down

0 comments on commit 8d0501b

Please sign in to comment.