mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-16 22:10:13 +01:00
Remove helpers.deepFreeze and call Object.freeze only on things that need it for ~11% time reduction measured via profile-fixture.mjs on Apple Silicon M1.
This commit is contained in:
parent
b6471fba31
commit
936c876810
5 changed files with 110 additions and 97 deletions
|
|
@ -191,13 +191,38 @@ function removeFrontMatter(content, frontMatter) {
|
|||
}
|
||||
|
||||
/**
|
||||
* Annotate tokens with line/lineNumber.
|
||||
* Freeze all freeze-able members of a token and its children.
|
||||
*
|
||||
* @param {MarkdownItToken} token A markdown-it token.
|
||||
* @returns {void}
|
||||
*/
|
||||
function freezeToken(token) {
|
||||
if (token.attrs) {
|
||||
for (const attr of token.attrs) {
|
||||
Object.freeze(attr);
|
||||
}
|
||||
Object.freeze(token.attrs);
|
||||
}
|
||||
if (token.children) {
|
||||
for (const child of token.children) {
|
||||
freezeToken(child);
|
||||
}
|
||||
Object.freeze(token.children);
|
||||
}
|
||||
if (token.map) {
|
||||
Object.freeze(token.map);
|
||||
}
|
||||
Object.freeze(token);
|
||||
}
|
||||
|
||||
/**
|
||||
* Annotate tokens with line/lineNumber and freeze them.
|
||||
*
|
||||
* @param {MarkdownItToken[]} tokens Array of markdown-it tokens.
|
||||
* @param {string[]} lines Lines of Markdown content.
|
||||
* @returns {void}
|
||||
*/
|
||||
function annotateTokens(tokens, lines) {
|
||||
function annotateAndFreezeTokens(tokens, lines) {
|
||||
let trMap = null;
|
||||
for (const token of tokens) {
|
||||
// Provide missing maps for table content
|
||||
|
|
@ -222,7 +247,9 @@ function annotateTokens(tokens, lines) {
|
|||
while (token.map[1] && !((lines[token.map[1] - 1] || "").trim())) {
|
||||
token.map[1]--;
|
||||
}
|
||||
// Annotate children with lineNumber
|
||||
}
|
||||
// Annotate children with lineNumber
|
||||
if (token.children) {
|
||||
let lineNumber = token.lineNumber;
|
||||
const codeSpanExtraLines = [];
|
||||
helpers.forEachInlineCodeSpan(
|
||||
|
|
@ -231,7 +258,7 @@ function annotateTokens(tokens, lines) {
|
|||
codeSpanExtraLines.push(code.split(helpers.newLineRe).length - 1);
|
||||
}
|
||||
);
|
||||
for (const child of (token.children || [])) {
|
||||
for (const child of token.children) {
|
||||
child.lineNumber = lineNumber;
|
||||
child.line = lines[lineNumber - 1];
|
||||
if ((child.type === "softbreak") || (child.type === "hardbreak")) {
|
||||
|
|
@ -241,7 +268,9 @@ function annotateTokens(tokens, lines) {
|
|||
}
|
||||
}
|
||||
}
|
||||
freezeToken(token);
|
||||
}
|
||||
Object.freeze(tokens);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -532,14 +561,14 @@ function lintContent(
|
|||
// Parse content into tokens and lines
|
||||
const tokens = md.parse(content, {});
|
||||
const lines = content.split(helpers.newLineRe);
|
||||
annotateTokens(tokens, lines);
|
||||
// Create parameters for rules
|
||||
const paramsBase = helpers.deepFreeze({
|
||||
annotateAndFreezeTokens(tokens, lines);
|
||||
// Create (frozen) parameters for rules
|
||||
const paramsBase = {
|
||||
name,
|
||||
tokens,
|
||||
lines,
|
||||
frontMatterLines
|
||||
});
|
||||
"lines": Object.freeze(lines),
|
||||
"frontMatterLines": Object.freeze(frontMatterLines)
|
||||
};
|
||||
const lineMetadata =
|
||||
helpers.getLineMetadata(paramsBase);
|
||||
const codeBlockAndSpanRanges =
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue