mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-22 17:00:13 +01: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
85
lib/md055.js
85
lib/md055.js
|
|
@ -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)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue