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,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,41 +16,36 @@ 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" ]
);
let expectedCount = 0;
for (const row of rows) {
const cells = filterByTypes(
row.children,
[ "tableData", "tableDelimiter", "tableHeader" ]
);
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";
range = [ row.endColumn - 1, 1 ];
} else if (expectedCount < actualCount) {
detail = "Too many cells, extra data will be missing";
range = makeRange(cells[expectedCount].startColumn, row.endColumn - 1);
}
addErrorDetailIf(
onError,
row.endLine,
expectedCount,
actualCount,
detail,
undefined,
range
);
const rows = filterByTypesCached([ "tableDelimiterRow", "tableRow" ]);
let expectedCount = 0;
let currentTable = null;
for (const row of rows) {
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;
let detail = undefined;
let range = undefined;
if (actualCount < expectedCount) {
detail = "Too few cells, row will be missing data";
range = [ row.endColumn - 1, 1 ];
} else if (expectedCount < actualCount) {
detail = "Too many cells, extra data will be missing";
range = makeRange(cells[expectedCount].startColumn, row.endColumn - 1);
}
addErrorDetailIf(
onError,
row.endLine,
expectedCount,
actualCount,
detail,
undefined,
range
);
}
}
};