|
| 1 | +/** |
| 2 | + * @fileoverview Rule to disallow data rows in a GitHub Flavored Markdown table from having more cells than the header row |
| 3 | + * @author Sweta Tanwar (@SwetaTanwar) |
| 4 | + */ |
| 5 | + |
| 6 | +//----------------------------------------------------------------------------- |
| 7 | +// Type Definitions |
| 8 | +//----------------------------------------------------------------------------- |
| 9 | + |
| 10 | +/** |
| 11 | + * @typedef {import("../types.ts").MarkdownRuleDefinition<{ RuleOptions: []; }>} TableColumnCountRuleDefinition |
| 12 | + */ |
| 13 | + |
| 14 | +//----------------------------------------------------------------------------- |
| 15 | +// Rule Definition |
| 16 | +//----------------------------------------------------------------------------- |
| 17 | + |
| 18 | +/** @type {TableColumnCountRuleDefinition} */ |
| 19 | +export default { |
| 20 | + meta: { |
| 21 | + type: "problem", |
| 22 | + |
| 23 | + docs: { |
| 24 | + recommended: true, |
| 25 | + description: |
| 26 | + "Disallow data rows in a GitHub Flavored Markdown table from having more cells than the header row", |
| 27 | + url: "https://github.com/eslint/markdown/blob/main/docs/rules/table-column-count.md", |
| 28 | + }, |
| 29 | + |
| 30 | + messages: { |
| 31 | + inconsistentColumnCount: |
| 32 | + "Table column count mismatch (Expected: {{expectedCells}}, Actual: {{actualCells}}), extra data starting here will be ignored.", |
| 33 | + }, |
| 34 | + }, |
| 35 | + |
| 36 | + create(context) { |
| 37 | + return { |
| 38 | + table(node) { |
| 39 | + if (node.children.length < 1) { |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + const headerRow = node.children[0]; |
| 44 | + const expectedCellsLength = headerRow.children.length; |
| 45 | + |
| 46 | + for (let i = 1; i < node.children.length; i++) { |
| 47 | + const currentRow = node.children[i]; |
| 48 | + const actualCellsLength = currentRow.children.length; |
| 49 | + |
| 50 | + if (actualCellsLength > expectedCellsLength) { |
| 51 | + const firstExtraCellNode = |
| 52 | + currentRow.children[expectedCellsLength]; |
| 53 | + |
| 54 | + const lastActualCellNode = |
| 55 | + currentRow.children[actualCellsLength - 1]; |
| 56 | + |
| 57 | + context.report({ |
| 58 | + loc: { |
| 59 | + start: firstExtraCellNode.position.start, |
| 60 | + end: lastActualCellNode.position.end, |
| 61 | + }, |
| 62 | + messageId: "inconsistentColumnCount", |
| 63 | + data: { |
| 64 | + actualCells: String(actualCellsLength), |
| 65 | + expectedCells: String(expectedCellsLength), |
| 66 | + }, |
| 67 | + }); |
| 68 | + } |
| 69 | + } |
| 70 | + }, |
| 71 | + }; |
| 72 | + }, |
| 73 | +}; |
0 commit comments