2024-01-04 23:07:55 -08:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const { addErrorDetailIf } = require("../helpers");
|
2024-09-28 16:26:38 -07:00
|
|
|
const { filterByTypes } = require("../helpers/micromark-helpers.cjs");
|
2024-08-24 22:05:16 -07:00
|
|
|
const { filterByTypesCached } = require("./cache");
|
2024-01-04 23:07:55 -08:00
|
|
|
|
|
|
|
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-08-24 22:05:16 -07:00
|
|
|
const tables = filterByTypesCached([ "table" ]);
|
2024-01-04 23:07:55 -08:00
|
|
|
for (const table of tables) {
|
2024-06-21 21:03:30 -07:00
|
|
|
const rows = filterByTypes(
|
|
|
|
table.children,
|
|
|
|
[ "tableDelimiterRow", "tableRow" ]
|
|
|
|
);
|
2024-01-04 23:07:55 -08:00
|
|
|
let expectedCount = 0;
|
|
|
|
for (const row of rows) {
|
2024-06-21 21:03:30 -07:00
|
|
|
const cells = filterByTypes(
|
|
|
|
row.children,
|
|
|
|
[ "tableData", "tableDelimiter", "tableHeader" ]
|
|
|
|
);
|
2024-01-04 23:07:55 -08:00
|
|
|
const actualCount = cells.length;
|
|
|
|
expectedCount ||= actualCount;
|
2024-06-21 21:03:30 -07:00
|
|
|
// eslint-disable-next-line no-undef-init
|
|
|
|
let detail = undefined;
|
|
|
|
// eslint-disable-next-line no-undef-init
|
|
|
|
let range = undefined;
|
2024-01-04 23:07:55 -08:00
|
|
|
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,
|
2024-06-21 21:03:30 -07:00
|
|
|
undefined,
|
2024-01-04 23:07:55 -08:00
|
|
|
range
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-09-01 16:16:05 -07:00
|
|
|
};
|