2024-01-04 23:07:55 -08:00
|
|
|
// @ts-check
|
|
|
|
|
2024-11-28 20:36:44 -08:00
|
|
|
import { addErrorDetailIf } from "../helpers/helpers.cjs";
|
|
|
|
import { getParentOfType } from "../helpers/micromark-helpers.cjs";
|
|
|
|
import { filterByTypesCached } from "./cache.mjs";
|
2024-01-04 23:07:55 -08:00
|
|
|
|
|
|
|
const makeRange = (start, end) => [ start, end - start + 1 ];
|
|
|
|
|
2024-11-28 20:36:44 -08:00
|
|
|
/** @typedef {import("micromark-extension-gfm-table")} */
|
|
|
|
|
2024-12-03 19:58:28 -08:00
|
|
|
/** @type {import("markdownlint").Rule} */
|
2024-11-28 20:36:44 -08:00
|
|
|
export default {
|
2024-01-04 23:07:55 -08:00
|
|
|
"names": [ "MD056", "table-column-count" ],
|
|
|
|
"description": "Table column count",
|
|
|
|
"tags": [ "table" ],
|
2024-03-09 16:17:50 -08:00
|
|
|
"parser": "micromark",
|
2024-01-04 23:07:55 -08:00
|
|
|
"function": function MD056(params, onError) {
|
2024-10-09 22:42:36 -07:00
|
|
|
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);
|
2024-01-04 23:07:55 -08:00
|
|
|
}
|
2024-10-09 22:42:36 -07:00
|
|
|
addErrorDetailIf(
|
|
|
|
onError,
|
|
|
|
row.endLine,
|
|
|
|
expectedCount,
|
|
|
|
actualCount,
|
|
|
|
detail,
|
|
|
|
undefined,
|
|
|
|
range
|
|
|
|
);
|
2024-01-04 23:07:55 -08:00
|
|
|
}
|
|
|
|
}
|
2024-09-01 16:16:05 -07:00
|
|
|
};
|