2024-01-04 23:07:55 -08:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const { addErrorDetailIf } = require("../helpers");
|
|
|
|
const { filterByTypes } = require("../helpers/micromark.cjs");
|
|
|
|
|
|
|
|
const makeRange = (start, end) => [ start, end - start + 1 ];
|
|
|
|
|
2024-02-27 20:42:09 -08:00
|
|
|
// eslint-disable-next-line jsdoc/valid-types
|
|
|
|
/** @type import("./markdownlint").Rule */
|
2024-01-04 23:07:55 -08:00
|
|
|
module.exports = {
|
|
|
|
"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-02-28 21:01:23 -08:00
|
|
|
// eslint-disable-next-line jsdoc/valid-types
|
|
|
|
/** @type import("../helpers/micromark.cjs").Token[] */
|
|
|
|
const micromarkTokens =
|
|
|
|
// @ts-ignore
|
|
|
|
params.parsers.micromark.tokens;
|
|
|
|
const tables = filterByTypes(micromarkTokens, [ "table" ]);
|
2024-01-04 23:07:55 -08:00
|
|
|
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;
|
|
|
|
let detail = null;
|
|
|
|
let range = null;
|
|
|
|
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,
|
|
|
|
null,
|
|
|
|
range
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|