Refactor various code to do shallow/constrained search instead of deep search for better performance, make cache key for filterByTypesCached unique.

This commit is contained in:
David Anson 2024-10-09 22:42:36 -07:00
parent 2fa7730a6b
commit d22c1f19ef
12 changed files with 171 additions and 220 deletions

View file

@ -846,12 +846,12 @@ function getDescendantsByType(parent, typePath) {
* @returns {number} Heading level. * @returns {number} Heading level.
*/ */
function getHeadingLevel(heading) { function getHeadingLevel(heading) {
const headingSequence = filterByTypes(
heading.children,
[ "atxHeadingSequence", "setextHeadingLineSequence" ]
);
let level = 1; let level = 1;
const { text } = headingSequence[0]; const headingSequence = heading.children.find(
(child) => [ "atxHeadingSequence", "setextHeadingLine" ].includes(child.type)
);
// @ts-ignore
const { text } = headingSequence;
if (text[0] === "#") { if (text[0] === "#") {
level = Math.min(text.length, 6); level = Math.min(text.length, 6);
} else if (text[0] === "-") { } else if (text[0] === "-") {
@ -870,9 +870,8 @@ function getHeadingStyle(heading) {
if (heading.type === "setextHeading") { if (heading.type === "setextHeading") {
return "setext"; return "setext";
} }
const atxHeadingSequenceLength = filterByTypes( const atxHeadingSequenceLength = heading.children.filter(
heading.children, (child) => child.type === "atxHeadingSequence"
[ "atxHeadingSequence" ]
).length; ).length;
if (atxHeadingSequenceLength === 1) { if (atxHeadingSequenceLength === 1) {
return "atx"; return "atx";
@ -1188,7 +1187,6 @@ const { filterByTypes } = __webpack_require__(/*! ../helpers/micromark-helpers.c
/** @type {Map<string, object>} */ /** @type {Map<string, object>} */
const map = new Map(); const map = new Map();
// eslint-disable-next-line no-undef-init
let params = undefined; let params = undefined;
/** /**
@ -1227,7 +1225,8 @@ function getCached(name, getValue) {
*/ */
function filterByTypesCached(types, htmlFlow) { function filterByTypesCached(types, htmlFlow) {
return getCached( return getCached(
types.join("|"), // eslint-disable-next-line prefer-rest-params
JSON.stringify(arguments),
() => filterByTypes(params.parsers.micromark.tokens, types, htmlFlow) () => filterByTypes(params.parsers.micromark.tokens, types, htmlFlow)
); );
} }
@ -3911,7 +3910,6 @@ module.exports = {
const { addErrorContext } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"); const { addErrorContext } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
const { filterByTypes } = __webpack_require__(/*! ../helpers/micromark-helpers.cjs */ "../helpers/micromark-helpers.cjs");
const { filterByTypesCached } = __webpack_require__(/*! ./cache */ "../lib/cache.js"); const { filterByTypesCached } = __webpack_require__(/*! ./cache */ "../lib/cache.js");
const dollarCommandRe = /^(\s*)(\$\s+)/; const dollarCommandRe = /^(\s*)(\$\s+)/;
@ -3925,10 +3923,7 @@ module.exports = {
"parser": "micromark", "parser": "micromark",
"function": function MD014(params, onError) { "function": function MD014(params, onError) {
for (const codeBlock of filterByTypesCached([ "codeFenced", "codeIndented" ])) { for (const codeBlock of filterByTypesCached([ "codeFenced", "codeIndented" ])) {
const codeFlowValues = filterByTypes( const codeFlowValues = codeBlock.children.filter((child) => child.type === "codeFlowValue");
codeBlock.children,
[ "codeFlowValue" ]
);
const dollarMatches = codeFlowValues const dollarMatches = codeFlowValues
.map((codeFlowValue) => ({ .map((codeFlowValue) => ({
"result": codeFlowValue.text.match(dollarCommandRe), "result": codeFlowValue.text.match(dollarCommandRe),
@ -5359,8 +5354,8 @@ module.exports = {
const spaceRight = rightSpaceRe.test(endData.text); const spaceRight = rightSpaceRe.test(endData.text);
if (spaceLeft || spaceRight) { if (spaceLeft || spaceRight) {
let lineNumber = startSequence.startLine; let lineNumber = startSequence.startLine;
let range = null; let range = undefined;
let fixInfo = null; let fixInfo = undefined;
if (startSequence.startLine === endSequence.endLine) { if (startSequence.startLine === endSequence.endLine) {
range = [ range = [
startSequence.startColumn, startSequence.startColumn,
@ -5428,7 +5423,6 @@ module.exports = {
const { addErrorContext } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"); const { addErrorContext } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
const { filterByTypes } = __webpack_require__(/*! ../helpers/micromark-helpers.cjs */ "../helpers/micromark-helpers.cjs");
const { getReferenceLinkImageData, filterByTypesCached } = __webpack_require__(/*! ./cache */ "../lib/cache.js"); const { getReferenceLinkImageData, filterByTypesCached } = __webpack_require__(/*! ./cache */ "../lib/cache.js");
/** /**
@ -5487,10 +5481,7 @@ module.exports = {
const labels = filterByTypesCached([ "label" ]) const labels = filterByTypesCached([ "label" ])
.filter((label) => label.parent?.type === "link"); .filter((label) => label.parent?.type === "link");
for (const label of labels) { for (const label of labels) {
const labelTexts = filterByTypes( const labelTexts = label.children.filter((child) => child.type === "labelText");
label.children,
[ "labelText" ]
);
for (const labelText of labelTexts) { for (const labelText of labelTexts) {
if ( if (
(labelText.text.trimStart().length !== labelText.text.length) && (labelText.text.trimStart().length !== labelText.text.length) &&
@ -5896,7 +5887,7 @@ module.exports = {
const { addError, getHtmlAttributeRe, nextLinesRe } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"); const { addError, getHtmlAttributeRe, nextLinesRe } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
const { filterByTypes, getHtmlTagInfo } = __webpack_require__(/*! ../helpers/micromark-helpers.cjs */ "../helpers/micromark-helpers.cjs"); const { getHtmlTagInfo, getDescendantsByType } = __webpack_require__(/*! ../helpers/micromark-helpers.cjs */ "../helpers/micromark-helpers.cjs");
const { filterByTypesCached } = __webpack_require__(/*! ./cache */ "../lib/cache.js"); const { filterByTypesCached } = __webpack_require__(/*! ./cache */ "../lib/cache.js");
const altRe = getHtmlAttributeRe("alt"); const altRe = getHtmlAttributeRe("alt");
@ -5912,7 +5903,7 @@ module.exports = {
// Process Markdown images // Process Markdown images
const images = filterByTypesCached([ "image" ]); const images = filterByTypesCached([ "image" ]);
for (const image of images) { for (const image of images) {
const labelTexts = filterByTypes(image.children, [ "labelText" ]); const labelTexts = getDescendantsByType(image, [ "label", "labelText" ]);
if (labelTexts.some((labelText) => labelText.text.length === 0)) { if (labelTexts.some((labelText) => labelText.text.length === 0)) {
const range = (image.startLine === image.endLine) ? const range = (image.startLine === image.endLine) ?
[ image.startColumn, image.endColumn - image.startColumn ] : [ image.startColumn, image.endColumn - image.startColumn ] :
@ -6367,11 +6358,8 @@ module.exports = {
!fragments.has(encodedText) && !fragments.has(encodedText) &&
!lineFragmentRe.test(encodedText) !lineFragmentRe.test(encodedText)
) { ) {
// eslint-disable-next-line no-undef-init
let context = undefined; let context = undefined;
// eslint-disable-next-line no-undef-init
let range = undefined; let range = undefined;
// eslint-disable-next-line no-undef-init
let fixInfo = undefined; let fixInfo = undefined;
if (link.startLine === link.endLine) { if (link.startLine === link.endLine) {
context = link.text; context = link.text;
@ -6634,9 +6622,8 @@ module.exports = {
} }
} }
if (isError) { if (isError) {
// eslint-disable-next-line no-undef-init
let range = undefined; let range = undefined;
let fixInfo = null; let fixInfo = undefined;
if (startLine === endLine) { if (startLine === endLine) {
range = [ startColumn, endColumn - startColumn ]; range = [ startColumn, endColumn - startColumn ];
let insertText = null; let insertText = null;
@ -6690,7 +6677,6 @@ module.exports = {
const { addErrorDetailIf } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"); const { addErrorDetailIf } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
const { filterByTypes } = __webpack_require__(/*! ../helpers/micromark-helpers.cjs */ "../helpers/micromark-helpers.cjs");
const { filterByTypesCached } = __webpack_require__(/*! ./cache */ "../lib/cache.js"); const { filterByTypesCached } = __webpack_require__(/*! ./cache */ "../lib/cache.js");
const whitespaceTypes = new Set([ "linePrefix", "whitespace" ]); const whitespaceTypes = new Set([ "linePrefix", "whitespace" ]);
@ -6715,12 +6701,7 @@ module.exports = {
((expectedStyle !== "no_leading_or_trailing") && (expectedStyle !== "trailing_only")); ((expectedStyle !== "no_leading_or_trailing") && (expectedStyle !== "trailing_only"));
let expectedTrailingPipe = let expectedTrailingPipe =
((expectedStyle !== "no_leading_or_trailing") && (expectedStyle !== "leading_only")); ((expectedStyle !== "no_leading_or_trailing") && (expectedStyle !== "leading_only"));
const tables = filterByTypesCached([ "table" ]); const rows = filterByTypesCached([ "tableDelimiterRow", "tableRow" ]);
for (const table of tables) {
const rows = filterByTypes(
table.children,
[ "tableDelimiterRow", "tableRow" ]
);
for (const row of rows) { for (const row of rows) {
// The following uses of first/lastOrNothing lack fallback handling // The following uses of first/lastOrNothing lack fallback handling
// because it seems not to be possible (i.e., 0% coverage) // because it seems not to be possible (i.e., 0% coverage)
@ -6762,7 +6743,6 @@ module.exports = {
} }
} }
} }
}
}; };
@ -6780,7 +6760,7 @@ module.exports = {
const { addErrorDetailIf } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"); const { addErrorDetailIf } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
const { filterByTypes } = __webpack_require__(/*! ../helpers/micromark-helpers.cjs */ "../helpers/micromark-helpers.cjs"); const { getParentOfType } = __webpack_require__(/*! ../helpers/micromark-helpers.cjs */ "../helpers/micromark-helpers.cjs");
const { filterByTypesCached } = __webpack_require__(/*! ./cache */ "../lib/cache.js"); const { filterByTypesCached } = __webpack_require__(/*! ./cache */ "../lib/cache.js");
const makeRange = (start, end) => [ start, end - start + 1 ]; const makeRange = (start, end) => [ start, end - start + 1 ];
@ -6793,23 +6773,19 @@ module.exports = {
"tags": [ "table" ], "tags": [ "table" ],
"parser": "micromark", "parser": "micromark",
"function": function MD056(params, onError) { "function": function MD056(params, onError) {
const tables = filterByTypesCached([ "table" ]); const rows = filterByTypesCached([ "tableDelimiterRow", "tableRow" ]);
for (const table of tables) {
const rows = filterByTypes(
table.children,
[ "tableDelimiterRow", "tableRow" ]
);
let expectedCount = 0; let expectedCount = 0;
let currentTable = null;
for (const row of rows) { for (const row of rows) {
const cells = filterByTypes( const table = getParentOfType(row, [ "table" ]);
row.children, if (currentTable !== table) {
[ "tableData", "tableDelimiter", "tableHeader" ] expectedCount = 0;
); currentTable = table;
}
const cells = row.children.filter((child) => [ "tableData", "tableDelimiter", "tableHeader" ].includes(child.type));
const actualCount = cells.length; const actualCount = cells.length;
expectedCount ||= actualCount; expectedCount ||= actualCount;
// eslint-disable-next-line no-undef-init
let detail = undefined; let detail = undefined;
// eslint-disable-next-line no-undef-init
let range = undefined; let range = undefined;
if (actualCount < expectedCount) { if (actualCount < expectedCount) {
detail = "Too few cells, row will be missing data"; detail = "Too few cells, row will be missing data";
@ -6829,7 +6805,6 @@ module.exports = {
); );
} }
} }
}
}; };

View file

@ -67,6 +67,7 @@ export default [
"no-param-reassign": "off", "no-param-reassign": "off",
"no-plusplus": "off", "no-plusplus": "off",
"no-ternary": "off", "no-ternary": "off",
"no-undef-init": "off",
"no-undefined": "off", "no-undefined": "off",
"no-useless-assignment": "off", "no-useless-assignment": "off",
"object-shorthand": "off", "object-shorthand": "off",

View file

@ -176,12 +176,12 @@ function getDescendantsByType(parent, typePath) {
* @returns {number} Heading level. * @returns {number} Heading level.
*/ */
function getHeadingLevel(heading) { function getHeadingLevel(heading) {
const headingSequence = filterByTypes(
heading.children,
[ "atxHeadingSequence", "setextHeadingLineSequence" ]
);
let level = 1; let level = 1;
const { text } = headingSequence[0]; const headingSequence = heading.children.find(
(child) => [ "atxHeadingSequence", "setextHeadingLine" ].includes(child.type)
);
// @ts-ignore
const { text } = headingSequence;
if (text[0] === "#") { if (text[0] === "#") {
level = Math.min(text.length, 6); level = Math.min(text.length, 6);
} else if (text[0] === "-") { } else if (text[0] === "-") {
@ -200,9 +200,8 @@ function getHeadingStyle(heading) {
if (heading.type === "setextHeading") { if (heading.type === "setextHeading") {
return "setext"; return "setext";
} }
const atxHeadingSequenceLength = filterByTypes( const atxHeadingSequenceLength = heading.children.filter(
heading.children, (child) => child.type === "atxHeadingSequence"
[ "atxHeadingSequence" ]
).length; ).length;
if (atxHeadingSequenceLength === 1) { if (atxHeadingSequenceLength === 1) {
return "atx"; return "atx";

View file

@ -7,7 +7,6 @@ const { filterByTypes } = require("../helpers/micromark-helpers.cjs");
/** @type {Map<string, object>} */ /** @type {Map<string, object>} */
const map = new Map(); const map = new Map();
// eslint-disable-next-line no-undef-init
let params = undefined; let params = undefined;
/** /**
@ -46,7 +45,8 @@ function getCached(name, getValue) {
*/ */
function filterByTypesCached(types, htmlFlow) { function filterByTypesCached(types, htmlFlow) {
return getCached( return getCached(
types.join("|"), // eslint-disable-next-line prefer-rest-params
JSON.stringify(arguments),
() => filterByTypes(params.parsers.micromark.tokens, types, htmlFlow) () => filterByTypes(params.parsers.micromark.tokens, types, htmlFlow)
); );
} }

View file

@ -3,7 +3,6 @@
"use strict"; "use strict";
const { addErrorContext } = require("../helpers"); const { addErrorContext } = require("../helpers");
const { filterByTypes } = require("../helpers/micromark-helpers.cjs");
const { filterByTypesCached } = require("./cache"); const { filterByTypesCached } = require("./cache");
const dollarCommandRe = /^(\s*)(\$\s+)/; const dollarCommandRe = /^(\s*)(\$\s+)/;
@ -17,10 +16,7 @@ module.exports = {
"parser": "micromark", "parser": "micromark",
"function": function MD014(params, onError) { "function": function MD014(params, onError) {
for (const codeBlock of filterByTypesCached([ "codeFenced", "codeIndented" ])) { for (const codeBlock of filterByTypesCached([ "codeFenced", "codeIndented" ])) {
const codeFlowValues = filterByTypes( const codeFlowValues = codeBlock.children.filter((child) => child.type === "codeFlowValue");
codeBlock.children,
[ "codeFlowValue" ]
);
const dollarMatches = codeFlowValues const dollarMatches = codeFlowValues
.map((codeFlowValue) => ({ .map((codeFlowValue) => ({
"result": codeFlowValue.text.match(dollarCommandRe), "result": codeFlowValue.text.match(dollarCommandRe),

View file

@ -40,8 +40,8 @@ module.exports = {
const spaceRight = rightSpaceRe.test(endData.text); const spaceRight = rightSpaceRe.test(endData.text);
if (spaceLeft || spaceRight) { if (spaceLeft || spaceRight) {
let lineNumber = startSequence.startLine; let lineNumber = startSequence.startLine;
let range = null; let range = undefined;
let fixInfo = null; let fixInfo = undefined;
if (startSequence.startLine === endSequence.endLine) { if (startSequence.startLine === endSequence.endLine) {
range = [ range = [
startSequence.startColumn, startSequence.startColumn,

View file

@ -3,7 +3,6 @@
"use strict"; "use strict";
const { addErrorContext } = require("../helpers"); const { addErrorContext } = require("../helpers");
const { filterByTypes } = require("../helpers/micromark-helpers.cjs");
const { getReferenceLinkImageData, filterByTypesCached } = require("./cache"); const { getReferenceLinkImageData, filterByTypesCached } = require("./cache");
/** /**
@ -62,10 +61,7 @@ module.exports = {
const labels = filterByTypesCached([ "label" ]) const labels = filterByTypesCached([ "label" ])
.filter((label) => label.parent?.type === "link"); .filter((label) => label.parent?.type === "link");
for (const label of labels) { for (const label of labels) {
const labelTexts = filterByTypes( const labelTexts = label.children.filter((child) => child.type === "labelText");
label.children,
[ "labelText" ]
);
for (const labelText of labelTexts) { for (const labelText of labelTexts) {
if ( if (
(labelText.text.trimStart().length !== labelText.text.length) && (labelText.text.trimStart().length !== labelText.text.length) &&

View file

@ -3,7 +3,7 @@
"use strict"; "use strict";
const { addError, getHtmlAttributeRe, nextLinesRe } = require("../helpers"); const { addError, getHtmlAttributeRe, nextLinesRe } = require("../helpers");
const { filterByTypes, getHtmlTagInfo } = require("../helpers/micromark-helpers.cjs"); const { getHtmlTagInfo, getDescendantsByType } = require("../helpers/micromark-helpers.cjs");
const { filterByTypesCached } = require("./cache"); const { filterByTypesCached } = require("./cache");
const altRe = getHtmlAttributeRe("alt"); const altRe = getHtmlAttributeRe("alt");
@ -19,7 +19,7 @@ module.exports = {
// Process Markdown images // Process Markdown images
const images = filterByTypesCached([ "image" ]); const images = filterByTypesCached([ "image" ]);
for (const image of images) { for (const image of images) {
const labelTexts = filterByTypes(image.children, [ "labelText" ]); const labelTexts = getDescendantsByType(image, [ "label", "labelText" ]);
if (labelTexts.some((labelText) => labelText.text.length === 0)) { if (labelTexts.some((labelText) => labelText.text.length === 0)) {
const range = (image.startLine === image.endLine) ? const range = (image.startLine === image.endLine) ?
[ image.startColumn, image.endColumn - image.startColumn ] : [ image.startColumn, image.endColumn - image.startColumn ] :

View file

@ -124,11 +124,8 @@ module.exports = {
!fragments.has(encodedText) && !fragments.has(encodedText) &&
!lineFragmentRe.test(encodedText) !lineFragmentRe.test(encodedText)
) { ) {
// eslint-disable-next-line no-undef-init
let context = undefined; let context = undefined;
// eslint-disable-next-line no-undef-init
let range = undefined; let range = undefined;
// eslint-disable-next-line no-undef-init
let fixInfo = undefined; let fixInfo = undefined;
if (link.startLine === link.endLine) { if (link.startLine === link.endLine) {
context = link.text; context = link.text;

View file

@ -82,9 +82,8 @@ module.exports = {
} }
} }
if (isError) { if (isError) {
// eslint-disable-next-line no-undef-init
let range = undefined; let range = undefined;
let fixInfo = null; let fixInfo = undefined;
if (startLine === endLine) { if (startLine === endLine) {
range = [ startColumn, endColumn - startColumn ]; range = [ startColumn, endColumn - startColumn ];
let insertText = null; let insertText = null;

View file

@ -3,7 +3,6 @@
"use strict"; "use strict";
const { addErrorDetailIf } = require("../helpers"); const { addErrorDetailIf } = require("../helpers");
const { filterByTypes } = require("../helpers/micromark-helpers.cjs");
const { filterByTypesCached } = require("./cache"); const { filterByTypesCached } = require("./cache");
const whitespaceTypes = new Set([ "linePrefix", "whitespace" ]); const whitespaceTypes = new Set([ "linePrefix", "whitespace" ]);
@ -28,12 +27,7 @@ module.exports = {
((expectedStyle !== "no_leading_or_trailing") && (expectedStyle !== "trailing_only")); ((expectedStyle !== "no_leading_or_trailing") && (expectedStyle !== "trailing_only"));
let expectedTrailingPipe = let expectedTrailingPipe =
((expectedStyle !== "no_leading_or_trailing") && (expectedStyle !== "leading_only")); ((expectedStyle !== "no_leading_or_trailing") && (expectedStyle !== "leading_only"));
const tables = filterByTypesCached([ "table" ]); const rows = filterByTypesCached([ "tableDelimiterRow", "tableRow" ]);
for (const table of tables) {
const rows = filterByTypes(
table.children,
[ "tableDelimiterRow", "tableRow" ]
);
for (const row of rows) { for (const row of rows) {
// The following uses of first/lastOrNothing lack fallback handling // The following uses of first/lastOrNothing lack fallback handling
// because it seems not to be possible (i.e., 0% coverage) // because it seems not to be possible (i.e., 0% coverage)
@ -75,5 +69,4 @@ module.exports = {
} }
} }
} }
}
}; };

View file

@ -3,7 +3,7 @@
"use strict"; "use strict";
const { addErrorDetailIf } = require("../helpers"); const { addErrorDetailIf } = require("../helpers");
const { filterByTypes } = require("../helpers/micromark-helpers.cjs"); const { getParentOfType } = require("../helpers/micromark-helpers.cjs");
const { filterByTypesCached } = require("./cache"); const { filterByTypesCached } = require("./cache");
const makeRange = (start, end) => [ start, end - start + 1 ]; const makeRange = (start, end) => [ start, end - start + 1 ];
@ -16,23 +16,19 @@ module.exports = {
"tags": [ "table" ], "tags": [ "table" ],
"parser": "micromark", "parser": "micromark",
"function": function MD056(params, onError) { "function": function MD056(params, onError) {
const tables = filterByTypesCached([ "table" ]); const rows = filterByTypesCached([ "tableDelimiterRow", "tableRow" ]);
for (const table of tables) {
const rows = filterByTypes(
table.children,
[ "tableDelimiterRow", "tableRow" ]
);
let expectedCount = 0; let expectedCount = 0;
let currentTable = null;
for (const row of rows) { for (const row of rows) {
const cells = filterByTypes( const table = getParentOfType(row, [ "table" ]);
row.children, if (currentTable !== table) {
[ "tableData", "tableDelimiter", "tableHeader" ] expectedCount = 0;
); currentTable = table;
}
const cells = row.children.filter((child) => [ "tableData", "tableDelimiter", "tableHeader" ].includes(child.type));
const actualCount = cells.length; const actualCount = cells.length;
expectedCount ||= actualCount; expectedCount ||= actualCount;
// eslint-disable-next-line no-undef-init
let detail = undefined; let detail = undefined;
// eslint-disable-next-line no-undef-init
let range = undefined; let range = undefined;
if (actualCount < expectedCount) { if (actualCount < expectedCount) {
detail = "Too few cells, row will be missing data"; detail = "Too few cells, row will be missing data";
@ -52,5 +48,4 @@ module.exports = {
); );
} }
} }
}
}; };