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

@ -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,51 +27,45 @@ 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" ]
);
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)
const firstCell = firstOrNothing(row.children);
const leadingToken = firstOrNothing(ignoreWhitespace(firstCell.children));
const actualLeadingPipe = (leadingToken.type === "tableCellDivider");
const lastCell = lastOrNothing(row.children);
const trailingToken = lastOrNothing(ignoreWhitespace(lastCell.children));
const actualTrailingPipe = (trailingToken.type === "tableCellDivider");
const actualStyle = actualLeadingPipe ?
(actualTrailingPipe ? "leading_and_trailing" : "leading_only") :
(actualTrailingPipe ? "trailing_only" : "no_leading_or_trailing");
if (expectedStyle === "consistent") {
expectedStyle = actualStyle;
expectedLeadingPipe = actualLeadingPipe;
expectedTrailingPipe = actualTrailingPipe;
}
if (actualLeadingPipe !== expectedLeadingPipe) {
addErrorDetailIf(
onError,
firstCell.startLine,
expectedStyle,
actualStyle,
`${expectedLeadingPipe ? "Missing" : "Unexpected"} leading pipe`,
undefined,
makeRange(row.startColumn, firstCell.startColumn)
);
}
if (actualTrailingPipe !== expectedTrailingPipe) {
addErrorDetailIf(
onError,
lastCell.endLine,
expectedStyle,
actualStyle,
`${expectedTrailingPipe ? "Missing" : "Unexpected"} trailing pipe`,
undefined,
makeRange(lastCell.endColumn - 1, row.endColumn - 1)
);
}
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)
const firstCell = firstOrNothing(row.children);
const leadingToken = firstOrNothing(ignoreWhitespace(firstCell.children));
const actualLeadingPipe = (leadingToken.type === "tableCellDivider");
const lastCell = lastOrNothing(row.children);
const trailingToken = lastOrNothing(ignoreWhitespace(lastCell.children));
const actualTrailingPipe = (trailingToken.type === "tableCellDivider");
const actualStyle = actualLeadingPipe ?
(actualTrailingPipe ? "leading_and_trailing" : "leading_only") :
(actualTrailingPipe ? "trailing_only" : "no_leading_or_trailing");
if (expectedStyle === "consistent") {
expectedStyle = actualStyle;
expectedLeadingPipe = actualLeadingPipe;
expectedTrailingPipe = actualTrailingPipe;
}
if (actualLeadingPipe !== expectedLeadingPipe) {
addErrorDetailIf(
onError,
firstCell.startLine,
expectedStyle,
actualStyle,
`${expectedLeadingPipe ? "Missing" : "Unexpected"} leading pipe`,
undefined,
makeRange(row.startColumn, firstCell.startColumn)
);
}
if (actualTrailingPipe !== expectedTrailingPipe) {
addErrorDetailIf(
onError,
lastCell.endLine,
expectedStyle,
actualStyle,
`${expectedTrailingPipe ? "Missing" : "Unexpected"} trailing pipe`,
undefined,
makeRange(lastCell.endColumn - 1, row.endColumn - 1)
);
}
}
}