-
-
Notifications
You must be signed in to change notification settings - Fork 687
/
md056.js
52 lines (48 loc) · 1.69 KB
/
md056.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// @ts-check
"use strict";
const { addErrorDetailIf } = require("../helpers");
const { filterByTypes } = require("../helpers/micromark.cjs");
const makeRange = (start, end) => [ start, end - start + 1 ];
// eslint-disable-next-line jsdoc/valid-types
/** @type import("./markdownlint").Rule */
module.exports = {
"names": [ "MD056", "table-column-count" ],
"description": "Table column count",
"tags": [ "table" ],
"parser": "micromark",
"function": function MD056(params, onError) {
// 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" ]);
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
);
}
}
}
}