diff --git a/docs/rules/table-column-count.md b/docs/rules/table-column-count.md index 5bed7aa8..44f3a2b6 100644 --- a/docs/rules/table-column-count.md +++ b/docs/rules/table-column-count.md @@ -43,7 +43,7 @@ Examples of **correct** code for this rule: | Header | Header | Header | | ------ | ------ | ------ | -| Cell | Cell | | +| Cell | Cell | @@ -58,6 +58,23 @@ Examples of **correct** code for this rule: | Single Cell | ``` +## Options + +The following options are available on this rule: + +* `checkMissingCells: boolean` - When set to `true`, the rule will also flag rows that have fewer cells than the header row. (default: `false`) + +Examples of **incorrect** code when configured as `"table-column-count": ["error", { checkMissingCells: true }]`: + +```markdown + + + +| Header | Header | Header | +| ------ | ------ | ------ | +| Cell | Cell | +``` + ## When Not To Use It If you intentionally create Markdown tables where data rows are expected to contain more cells than the header, and you have a specific (perhaps non-standard) processing or rendering pipeline that handles this scenario correctly, you might choose to disable this rule. However, adhering to this rule is recommended for typical GFM rendering and data consistency. diff --git a/src/rules/table-column-count.js b/src/rules/table-column-count.js index e21e0cb5..f929f769 100644 --- a/src/rules/table-column-count.js +++ b/src/rules/table-column-count.js @@ -8,7 +8,10 @@ //----------------------------------------------------------------------------- /** - * @typedef {import("../types.ts").MarkdownRuleDefinition<{ RuleOptions: []; }>} TableColumnCountRuleDefinition + * @import { MarkdownRuleDefinition } from "../types.js"; + * @typedef {"extraCells" | "missingCells"} TableColumnCountMessageIds + * @typedef {[{ checkMissingCells?: boolean }]} TableColumnCountOptions + * @typedef {MarkdownRuleDefinition<{ RuleOptions: TableColumnCountOptions, MessageIds: TableColumnCountMessageIds }>} TableColumnCountRuleDefinition */ //----------------------------------------------------------------------------- @@ -28,12 +31,30 @@ export default { }, messages: { - inconsistentColumnCount: + extraCells: "Table column count mismatch (Expected: {{expectedCells}}, Actual: {{actualCells}}), extra data starting here will be ignored.", + missingCells: + "Table column count mismatch (Expected: {{expectedCells}}, Actual: {{actualCells}}), row might be missing data.", }, + + schema: [ + { + type: "object", + properties: { + checkMissingCells: { + type: "boolean", + }, + }, + additionalProperties: false, + }, + ], + + defaultOptions: [{ checkMissingCells: false }], }, create(context) { + const [{ checkMissingCells }] = context.options; + return { table(node) { if (node.children.length < 1) { @@ -46,20 +67,39 @@ export default { for (let i = 1; i < node.children.length; i++) { const currentRow = node.children[i]; const actualCellsLength = currentRow.children.length; + const lastActualCellNode = + currentRow.children[actualCellsLength - 1]; if (actualCellsLength > expectedCellsLength) { const firstExtraCellNode = currentRow.children[expectedCellsLength]; - const lastActualCellNode = - currentRow.children[actualCellsLength - 1]; - context.report({ loc: { start: firstExtraCellNode.position.start, end: lastActualCellNode.position.end, }, - messageId: "inconsistentColumnCount", + messageId: "extraCells", + data: { + actualCells: String(actualCellsLength), + expectedCells: String(expectedCellsLength), + }, + }); + } else if ( + checkMissingCells && + actualCellsLength < expectedCellsLength + ) { + context.report({ + loc: { + start: { + column: + lastActualCellNode.position.end.column - + 1, + line: lastActualCellNode.position.end.line, + }, + end: currentRow.position.end, + }, + messageId: "missingCells", data: { actualCells: String(actualCellsLength), expectedCells: String(expectedCellsLength), diff --git a/tests/rules/table-column-count.test.js b/tests/rules/table-column-count.test.js index 48702275..c6fd71c2 100644 --- a/tests/rules/table-column-count.test.js +++ b/tests/rules/table-column-count.test.js @@ -88,6 +88,24 @@ ruleTester.run("table-column-count", rule, { | abc | def | | --- | --- | `, + { + code: dedent` + | Header | Header | + | ------ | ------ | + | Cell | Cell | + | Cell | Cell | + `, + options: [{ checkMissingCells: true }], + }, + { + code: dedent` + | Header | Header | + | ------ | ------ | + | Cell | | + | Cell | Cell | + `, + options: [{ checkMissingCells: true }], + }, ], invalid: [ @@ -99,7 +117,7 @@ ruleTester.run("table-column-count", rule, { `, errors: [ { - messageId: "inconsistentColumnCount", + messageId: "extraCells", data: { actualCells: "3", expectedCells: "2" }, line: 3, column: 17, @@ -116,7 +134,7 @@ ruleTester.run("table-column-count", rule, { `, errors: [ { - messageId: "inconsistentColumnCount", + messageId: "extraCells", data: { actualCells: "4", expectedCells: "2" }, line: 3, column: 17, @@ -133,7 +151,7 @@ ruleTester.run("table-column-count", rule, { `, errors: [ { - messageId: "inconsistentColumnCount", + messageId: "extraCells", data: { actualCells: "2", expectedCells: "1" }, line: 3, column: 5, @@ -155,7 +173,7 @@ ruleTester.run("table-column-count", rule, { `, errors: [ { - messageId: "inconsistentColumnCount", + messageId: "extraCells", data: { actualCells: "3", expectedCells: "2" }, line: 5, column: 21, @@ -173,7 +191,7 @@ ruleTester.run("table-column-count", rule, { `, errors: [ { - messageId: "inconsistentColumnCount", + messageId: "extraCells", data: { actualCells: "3", expectedCells: "2" }, line: 4, column: 11, @@ -191,7 +209,7 @@ ruleTester.run("table-column-count", rule, { `, errors: [ { - messageId: "inconsistentColumnCount", + messageId: "extraCells", data: { actualCells: "3", expectedCells: "2" }, line: 3, column: 13, @@ -209,7 +227,7 @@ ruleTester.run("table-column-count", rule, { `, errors: [ { - messageId: "inconsistentColumnCount", + messageId: "extraCells", data: { actualCells: "3", expectedCells: "2" }, line: 3, column: 13, @@ -217,7 +235,7 @@ ruleTester.run("table-column-count", rule, { endColumn: 23, }, { - messageId: "inconsistentColumnCount", + messageId: "extraCells", data: { actualCells: "3", expectedCells: "2" }, line: 4, column: 13, @@ -236,7 +254,7 @@ ruleTester.run("table-column-count", rule, { `, errors: [ { - messageId: "inconsistentColumnCount", + messageId: "extraCells", data: { actualCells: "3", expectedCells: "2" }, line: 3, column: 13, @@ -244,7 +262,7 @@ ruleTester.run("table-column-count", rule, { endColumn: 23, }, { - messageId: "inconsistentColumnCount", + messageId: "extraCells", data: { actualCells: "3", expectedCells: "2" }, line: 5, column: 13, @@ -253,5 +271,154 @@ ruleTester.run("table-column-count", rule, { }, ], }, + { + code: dedent` + | Header | Header | Header | + | ------ | ------ | ------ | + | Cell | Cell | + `, + options: [{ checkMissingCells: true }], + errors: [ + { + messageId: "missingCells", + data: { actualCells: "2", expectedCells: "3" }, + line: 3, + column: 19, + endLine: 3, + endColumn: 20, + }, + ], + }, + { + code: dedent` + | Col A | Col B | Col C | + | ----- | ----- | ----- | + | Cell | | Cell | + | Cell | Cell | + `, + options: [{ checkMissingCells: true }], + errors: [ + { + messageId: "missingCells", + data: { actualCells: "2", expectedCells: "3" }, + line: 4, + column: 17, + endLine: 4, + endColumn: 18, + }, + ], + }, + { + code: dedent` + | Col A | Col B | Col C | + | ----- | ----- | ----- | + | Cell | + | Cell | Cell | + | Cell | Cell | Cell | + `, + options: [{ checkMissingCells: true }], + errors: [ + { + messageId: "missingCells", + data: { actualCells: "1", expectedCells: "3" }, + line: 3, + column: 9, + endLine: 3, + endColumn: 10, + }, + { + messageId: "missingCells", + data: { actualCells: "2", expectedCells: "3" }, + line: 4, + column: 17, + endLine: 4, + endColumn: 18, + }, + ], + }, + { + code: dedent` + | Table | + | ----- | + | Cell | Cell | + | Cell | + | Cell | Cell | + `, + options: [{ checkMissingCells: true }], + errors: [ + { + messageId: "extraCells", + data: { actualCells: "2", expectedCells: "1" }, + line: 3, + column: 9, + endLine: 3, + endColumn: 18, + }, + { + messageId: "extraCells", + data: { actualCells: "2", expectedCells: "1" }, + line: 5, + column: 9, + endLine: 5, + endColumn: 18, + }, + ], + }, + { + code: dedent` + | Table | Header | + | ----- | ------ | + | Cell | Cell | Cell | + | Cell | + | Cell | Cell | + `, + options: [{ checkMissingCells: true }], + errors: [ + { + messageId: "extraCells", + data: { actualCells: "3", expectedCells: "2" }, + line: 3, + column: 18, + endLine: 3, + endColumn: 28, + }, + { + messageId: "missingCells", + data: { actualCells: "1", expectedCells: "2" }, + line: 4, + column: 9, + endLine: 4, + endColumn: 10, + }, + ], + }, + { + code: dedent` + | Table | Header | Header | + | ----- | ------ | ------ | + | Cell | Cell | Cell | + | Cell | Cell | Cell | Cell | + | Cell | + `, + options: [{ checkMissingCells: true }], + errors: [ + { + messageId: "extraCells", + data: { actualCells: "4", expectedCells: "3" }, + line: 4, + column: 27, + endLine: 4, + endColumn: 37, + }, + { + messageId: "missingCells", + data: { actualCells: "1", expectedCells: "3" }, + line: 5, + column: 9, + endLine: 5, + endColumn: 10, + }, + ], + }, ], });