mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-09-21 21:30:47 +02:00
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:
parent
2fa7730a6b
commit
d22c1f19ef
12 changed files with 171 additions and 220 deletions
|
@ -846,12 +846,12 @@ function getDescendantsByType(parent, typePath) {
|
|||
* @returns {number} Heading level.
|
||||
*/
|
||||
function getHeadingLevel(heading) {
|
||||
const headingSequence = filterByTypes(
|
||||
heading.children,
|
||||
[ "atxHeadingSequence", "setextHeadingLineSequence" ]
|
||||
);
|
||||
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] === "#") {
|
||||
level = Math.min(text.length, 6);
|
||||
} else if (text[0] === "-") {
|
||||
|
@ -870,9 +870,8 @@ function getHeadingStyle(heading) {
|
|||
if (heading.type === "setextHeading") {
|
||||
return "setext";
|
||||
}
|
||||
const atxHeadingSequenceLength = filterByTypes(
|
||||
heading.children,
|
||||
[ "atxHeadingSequence" ]
|
||||
const atxHeadingSequenceLength = heading.children.filter(
|
||||
(child) => child.type === "atxHeadingSequence"
|
||||
).length;
|
||||
if (atxHeadingSequenceLength === 1) {
|
||||
return "atx";
|
||||
|
@ -1188,7 +1187,6 @@ const { filterByTypes } = __webpack_require__(/*! ../helpers/micromark-helpers.c
|
|||
|
||||
/** @type {Map<string, object>} */
|
||||
const map = new Map();
|
||||
// eslint-disable-next-line no-undef-init
|
||||
let params = undefined;
|
||||
|
||||
/**
|
||||
|
@ -1227,7 +1225,8 @@ function getCached(name, getValue) {
|
|||
*/
|
||||
function filterByTypesCached(types, htmlFlow) {
|
||||
return getCached(
|
||||
types.join("|"),
|
||||
// eslint-disable-next-line prefer-rest-params
|
||||
JSON.stringify(arguments),
|
||||
() => filterByTypes(params.parsers.micromark.tokens, types, htmlFlow)
|
||||
);
|
||||
}
|
||||
|
@ -3911,7 +3910,6 @@ module.exports = {
|
|||
|
||||
|
||||
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 dollarCommandRe = /^(\s*)(\$\s+)/;
|
||||
|
@ -3925,10 +3923,7 @@ module.exports = {
|
|||
"parser": "micromark",
|
||||
"function": function MD014(params, onError) {
|
||||
for (const codeBlock of filterByTypesCached([ "codeFenced", "codeIndented" ])) {
|
||||
const codeFlowValues = filterByTypes(
|
||||
codeBlock.children,
|
||||
[ "codeFlowValue" ]
|
||||
);
|
||||
const codeFlowValues = codeBlock.children.filter((child) => child.type === "codeFlowValue");
|
||||
const dollarMatches = codeFlowValues
|
||||
.map((codeFlowValue) => ({
|
||||
"result": codeFlowValue.text.match(dollarCommandRe),
|
||||
|
@ -5359,8 +5354,8 @@ module.exports = {
|
|||
const spaceRight = rightSpaceRe.test(endData.text);
|
||||
if (spaceLeft || spaceRight) {
|
||||
let lineNumber = startSequence.startLine;
|
||||
let range = null;
|
||||
let fixInfo = null;
|
||||
let range = undefined;
|
||||
let fixInfo = undefined;
|
||||
if (startSequence.startLine === endSequence.endLine) {
|
||||
range = [
|
||||
startSequence.startColumn,
|
||||
|
@ -5428,7 +5423,6 @@ module.exports = {
|
|||
|
||||
|
||||
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");
|
||||
|
||||
/**
|
||||
|
@ -5487,10 +5481,7 @@ module.exports = {
|
|||
const labels = filterByTypesCached([ "label" ])
|
||||
.filter((label) => label.parent?.type === "link");
|
||||
for (const label of labels) {
|
||||
const labelTexts = filterByTypes(
|
||||
label.children,
|
||||
[ "labelText" ]
|
||||
);
|
||||
const labelTexts = label.children.filter((child) => child.type === "labelText");
|
||||
for (const labelText of labelTexts) {
|
||||
if (
|
||||
(labelText.text.trimStart().length !== labelText.text.length) &&
|
||||
|
@ -5896,7 +5887,7 @@ module.exports = {
|
|||
|
||||
|
||||
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 altRe = getHtmlAttributeRe("alt");
|
||||
|
@ -5912,7 +5903,7 @@ module.exports = {
|
|||
// Process Markdown images
|
||||
const images = filterByTypesCached([ "image" ]);
|
||||
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)) {
|
||||
const range = (image.startLine === image.endLine) ?
|
||||
[ image.startColumn, image.endColumn - image.startColumn ] :
|
||||
|
@ -6367,11 +6358,8 @@ module.exports = {
|
|||
!fragments.has(encodedText) &&
|
||||
!lineFragmentRe.test(encodedText)
|
||||
) {
|
||||
// eslint-disable-next-line no-undef-init
|
||||
let context = undefined;
|
||||
// eslint-disable-next-line no-undef-init
|
||||
let range = undefined;
|
||||
// eslint-disable-next-line no-undef-init
|
||||
let fixInfo = undefined;
|
||||
if (link.startLine === link.endLine) {
|
||||
context = link.text;
|
||||
|
@ -6634,9 +6622,8 @@ module.exports = {
|
|||
}
|
||||
}
|
||||
if (isError) {
|
||||
// eslint-disable-next-line no-undef-init
|
||||
let range = undefined;
|
||||
let fixInfo = null;
|
||||
let fixInfo = undefined;
|
||||
if (startLine === endLine) {
|
||||
range = [ startColumn, endColumn - startColumn ];
|
||||
let insertText = null;
|
||||
|
@ -6690,7 +6677,6 @@ module.exports = {
|
|||
|
||||
|
||||
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 whitespaceTypes = new Set([ "linePrefix", "whitespace" ]);
|
||||
|
@ -6715,12 +6701,7 @@ module.exports = {
|
|||
((expectedStyle !== "no_leading_or_trailing") && (expectedStyle !== "trailing_only"));
|
||||
let expectedTrailingPipe =
|
||||
((expectedStyle !== "no_leading_or_trailing") && (expectedStyle !== "leading_only"));
|
||||
const tables = filterByTypesCached([ "table" ]);
|
||||
for (const table of tables) {
|
||||
const rows = filterByTypes(
|
||||
table.children,
|
||||
[ "tableDelimiterRow", "tableRow" ]
|
||||
);
|
||||
const rows = filterByTypesCached([ "tableDelimiterRow", "tableRow" ]);
|
||||
for (const row of rows) {
|
||||
// The following uses of first/lastOrNothing lack fallback handling
|
||||
// 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 { 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 makeRange = (start, end) => [ start, end - start + 1 ];
|
||||
|
@ -6793,23 +6773,19 @@ module.exports = {
|
|||
"tags": [ "table" ],
|
||||
"parser": "micromark",
|
||||
"function": function MD056(params, onError) {
|
||||
const tables = filterByTypesCached([ "table" ]);
|
||||
for (const table of tables) {
|
||||
const rows = filterByTypes(
|
||||
table.children,
|
||||
[ "tableDelimiterRow", "tableRow" ]
|
||||
);
|
||||
const rows = filterByTypesCached([ "tableDelimiterRow", "tableRow" ]);
|
||||
let expectedCount = 0;
|
||||
let currentTable = null;
|
||||
for (const row of rows) {
|
||||
const cells = filterByTypes(
|
||||
row.children,
|
||||
[ "tableData", "tableDelimiter", "tableHeader" ]
|
||||
);
|
||||
const table = getParentOfType(row, [ "table" ]);
|
||||
if (currentTable !== table) {
|
||||
expectedCount = 0;
|
||||
currentTable = table;
|
||||
}
|
||||
const cells = row.children.filter((child) => [ "tableData", "tableDelimiter", "tableHeader" ].includes(child.type));
|
||||
const actualCount = cells.length;
|
||||
expectedCount ||= actualCount;
|
||||
// eslint-disable-next-line no-undef-init
|
||||
let detail = undefined;
|
||||
// eslint-disable-next-line no-undef-init
|
||||
let range = undefined;
|
||||
if (actualCount < expectedCount) {
|
||||
detail = "Too few cells, row will be missing data";
|
||||
|
@ -6829,7 +6805,6 @@ module.exports = {
|
|||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -67,6 +67,7 @@ export default [
|
|||
"no-param-reassign": "off",
|
||||
"no-plusplus": "off",
|
||||
"no-ternary": "off",
|
||||
"no-undef-init": "off",
|
||||
"no-undefined": "off",
|
||||
"no-useless-assignment": "off",
|
||||
"object-shorthand": "off",
|
||||
|
|
|
@ -176,12 +176,12 @@ function getDescendantsByType(parent, typePath) {
|
|||
* @returns {number} Heading level.
|
||||
*/
|
||||
function getHeadingLevel(heading) {
|
||||
const headingSequence = filterByTypes(
|
||||
heading.children,
|
||||
[ "atxHeadingSequence", "setextHeadingLineSequence" ]
|
||||
);
|
||||
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] === "#") {
|
||||
level = Math.min(text.length, 6);
|
||||
} else if (text[0] === "-") {
|
||||
|
@ -200,9 +200,8 @@ function getHeadingStyle(heading) {
|
|||
if (heading.type === "setextHeading") {
|
||||
return "setext";
|
||||
}
|
||||
const atxHeadingSequenceLength = filterByTypes(
|
||||
heading.children,
|
||||
[ "atxHeadingSequence" ]
|
||||
const atxHeadingSequenceLength = heading.children.filter(
|
||||
(child) => child.type === "atxHeadingSequence"
|
||||
).length;
|
||||
if (atxHeadingSequenceLength === 1) {
|
||||
return "atx";
|
||||
|
|
|
@ -7,7 +7,6 @@ const { filterByTypes } = require("../helpers/micromark-helpers.cjs");
|
|||
|
||||
/** @type {Map<string, object>} */
|
||||
const map = new Map();
|
||||
// eslint-disable-next-line no-undef-init
|
||||
let params = undefined;
|
||||
|
||||
/**
|
||||
|
@ -46,7 +45,8 @@ function getCached(name, getValue) {
|
|||
*/
|
||||
function filterByTypesCached(types, htmlFlow) {
|
||||
return getCached(
|
||||
types.join("|"),
|
||||
// eslint-disable-next-line prefer-rest-params
|
||||
JSON.stringify(arguments),
|
||||
() => filterByTypes(params.parsers.micromark.tokens, types, htmlFlow)
|
||||
);
|
||||
}
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
"use strict";
|
||||
|
||||
const { addErrorContext } = require("../helpers");
|
||||
const { filterByTypes } = require("../helpers/micromark-helpers.cjs");
|
||||
const { filterByTypesCached } = require("./cache");
|
||||
|
||||
const dollarCommandRe = /^(\s*)(\$\s+)/;
|
||||
|
@ -17,10 +16,7 @@ module.exports = {
|
|||
"parser": "micromark",
|
||||
"function": function MD014(params, onError) {
|
||||
for (const codeBlock of filterByTypesCached([ "codeFenced", "codeIndented" ])) {
|
||||
const codeFlowValues = filterByTypes(
|
||||
codeBlock.children,
|
||||
[ "codeFlowValue" ]
|
||||
);
|
||||
const codeFlowValues = codeBlock.children.filter((child) => child.type === "codeFlowValue");
|
||||
const dollarMatches = codeFlowValues
|
||||
.map((codeFlowValue) => ({
|
||||
"result": codeFlowValue.text.match(dollarCommandRe),
|
||||
|
|
|
@ -40,8 +40,8 @@ module.exports = {
|
|||
const spaceRight = rightSpaceRe.test(endData.text);
|
||||
if (spaceLeft || spaceRight) {
|
||||
let lineNumber = startSequence.startLine;
|
||||
let range = null;
|
||||
let fixInfo = null;
|
||||
let range = undefined;
|
||||
let fixInfo = undefined;
|
||||
if (startSequence.startLine === endSequence.endLine) {
|
||||
range = [
|
||||
startSequence.startColumn,
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
"use strict";
|
||||
|
||||
const { addErrorContext } = require("../helpers");
|
||||
const { filterByTypes } = require("../helpers/micromark-helpers.cjs");
|
||||
const { getReferenceLinkImageData, filterByTypesCached } = require("./cache");
|
||||
|
||||
/**
|
||||
|
@ -62,10 +61,7 @@ module.exports = {
|
|||
const labels = filterByTypesCached([ "label" ])
|
||||
.filter((label) => label.parent?.type === "link");
|
||||
for (const label of labels) {
|
||||
const labelTexts = filterByTypes(
|
||||
label.children,
|
||||
[ "labelText" ]
|
||||
);
|
||||
const labelTexts = label.children.filter((child) => child.type === "labelText");
|
||||
for (const labelText of labelTexts) {
|
||||
if (
|
||||
(labelText.text.trimStart().length !== labelText.text.length) &&
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
"use strict";
|
||||
|
||||
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 altRe = getHtmlAttributeRe("alt");
|
||||
|
@ -19,7 +19,7 @@ module.exports = {
|
|||
// Process Markdown images
|
||||
const images = filterByTypesCached([ "image" ]);
|
||||
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)) {
|
||||
const range = (image.startLine === image.endLine) ?
|
||||
[ image.startColumn, image.endColumn - image.startColumn ] :
|
||||
|
|
|
@ -124,11 +124,8 @@ module.exports = {
|
|||
!fragments.has(encodedText) &&
|
||||
!lineFragmentRe.test(encodedText)
|
||||
) {
|
||||
// eslint-disable-next-line no-undef-init
|
||||
let context = undefined;
|
||||
// eslint-disable-next-line no-undef-init
|
||||
let range = undefined;
|
||||
// eslint-disable-next-line no-undef-init
|
||||
let fixInfo = undefined;
|
||||
if (link.startLine === link.endLine) {
|
||||
context = link.text;
|
||||
|
|
|
@ -82,9 +82,8 @@ module.exports = {
|
|||
}
|
||||
}
|
||||
if (isError) {
|
||||
// eslint-disable-next-line no-undef-init
|
||||
let range = undefined;
|
||||
let fixInfo = null;
|
||||
let fixInfo = undefined;
|
||||
if (startLine === endLine) {
|
||||
range = [ startColumn, endColumn - startColumn ];
|
||||
let insertText = null;
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
"use strict";
|
||||
|
||||
const { addErrorDetailIf } = require("../helpers");
|
||||
const { filterByTypes } = require("../helpers/micromark-helpers.cjs");
|
||||
const { filterByTypesCached } = require("./cache");
|
||||
|
||||
const whitespaceTypes = new Set([ "linePrefix", "whitespace" ]);
|
||||
|
@ -28,12 +27,7 @@ module.exports = {
|
|||
((expectedStyle !== "no_leading_or_trailing") && (expectedStyle !== "trailing_only"));
|
||||
let expectedTrailingPipe =
|
||||
((expectedStyle !== "no_leading_or_trailing") && (expectedStyle !== "leading_only"));
|
||||
const tables = filterByTypesCached([ "table" ]);
|
||||
for (const table of tables) {
|
||||
const rows = filterByTypes(
|
||||
table.children,
|
||||
[ "tableDelimiterRow", "tableRow" ]
|
||||
);
|
||||
const rows = filterByTypesCached([ "tableDelimiterRow", "tableRow" ]);
|
||||
for (const row of rows) {
|
||||
// The following uses of first/lastOrNothing lack fallback handling
|
||||
// because it seems not to be possible (i.e., 0% coverage)
|
||||
|
@ -75,5 +69,4 @@ module.exports = {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
23
lib/md056.js
23
lib/md056.js
|
@ -3,7 +3,7 @@
|
|||
"use strict";
|
||||
|
||||
const { addErrorDetailIf } = require("../helpers");
|
||||
const { filterByTypes } = require("../helpers/micromark-helpers.cjs");
|
||||
const { getParentOfType } = require("../helpers/micromark-helpers.cjs");
|
||||
const { filterByTypesCached } = require("./cache");
|
||||
|
||||
const makeRange = (start, end) => [ start, end - start + 1 ];
|
||||
|
@ -16,23 +16,19 @@ module.exports = {
|
|||
"tags": [ "table" ],
|
||||
"parser": "micromark",
|
||||
"function": function MD056(params, onError) {
|
||||
const tables = filterByTypesCached([ "table" ]);
|
||||
for (const table of tables) {
|
||||
const rows = filterByTypes(
|
||||
table.children,
|
||||
[ "tableDelimiterRow", "tableRow" ]
|
||||
);
|
||||
const rows = filterByTypesCached([ "tableDelimiterRow", "tableRow" ]);
|
||||
let expectedCount = 0;
|
||||
let currentTable = null;
|
||||
for (const row of rows) {
|
||||
const cells = filterByTypes(
|
||||
row.children,
|
||||
[ "tableData", "tableDelimiter", "tableHeader" ]
|
||||
);
|
||||
const table = getParentOfType(row, [ "table" ]);
|
||||
if (currentTable !== table) {
|
||||
expectedCount = 0;
|
||||
currentTable = table;
|
||||
}
|
||||
const cells = row.children.filter((child) => [ "tableData", "tableDelimiter", "tableHeader" ].includes(child.type));
|
||||
const actualCount = cells.length;
|
||||
expectedCount ||= actualCount;
|
||||
// eslint-disable-next-line no-undef-init
|
||||
let detail = undefined;
|
||||
// eslint-disable-next-line no-undef-init
|
||||
let range = undefined;
|
||||
if (actualCount < expectedCount) {
|
||||
detail = "Too few cells, row will be missing data";
|
||||
|
@ -52,5 +48,4 @@ module.exports = {
|
|||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue