Skip to content

feat: add checkMissingCells option to table-column-count #434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion docs/rules/table-column-count.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Examples of **correct** code for this rule:
<!-- Rows with fewer cells are valid because they render correctly and no data is lost -->
| Header | Header | Header |
| ------ | ------ | ------ |
| Cell | Cell | |
| Cell | Cell |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
| Cell | Cell |
| Cell | Cell | |

I think the original intention of this example is to show that an empty cell is okay. The following example already covers the change you made.

image

Could you revert the example?

Copy link
Contributor Author

@TKDev7 TKDev7 Jun 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its description says: <!-- Data row with fewer cells than header (VALID for this rule) -->. Without this change, the number of cells would have matched the header.


<!-- Table with some empty cells (VALID for this rule) -->
<!-- Missing cells are treated as empty and don't cause rendering issues -->
Expand All @@ -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
<!-- eslint markdown/table-column-count: ["error", { checkMissingCells: true }] -->

<!-- Data row with fewer cells than header -->
| 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.
Expand Down
52 changes: 46 additions & 6 deletions src/rules/table-column-count.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/

//-----------------------------------------------------------------------------
Expand All @@ -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) {
Expand All @@ -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),
Expand Down
187 changes: 177 additions & 10 deletions tests/rules/table-column-count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -99,7 +117,7 @@ ruleTester.run("table-column-count", rule, {
`,
errors: [
{
messageId: "inconsistentColumnCount",
messageId: "extraCells",
data: { actualCells: "3", expectedCells: "2" },
line: 3,
column: 17,
Expand All @@ -116,7 +134,7 @@ ruleTester.run("table-column-count", rule, {
`,
errors: [
{
messageId: "inconsistentColumnCount",
messageId: "extraCells",
data: { actualCells: "4", expectedCells: "2" },
line: 3,
column: 17,
Expand All @@ -133,7 +151,7 @@ ruleTester.run("table-column-count", rule, {
`,
errors: [
{
messageId: "inconsistentColumnCount",
messageId: "extraCells",
data: { actualCells: "2", expectedCells: "1" },
line: 3,
column: 5,
Expand All @@ -155,7 +173,7 @@ ruleTester.run("table-column-count", rule, {
`,
errors: [
{
messageId: "inconsistentColumnCount",
messageId: "extraCells",
data: { actualCells: "3", expectedCells: "2" },
line: 5,
column: 21,
Expand All @@ -173,7 +191,7 @@ ruleTester.run("table-column-count", rule, {
`,
errors: [
{
messageId: "inconsistentColumnCount",
messageId: "extraCells",
data: { actualCells: "3", expectedCells: "2" },
line: 4,
column: 11,
Expand All @@ -191,7 +209,7 @@ ruleTester.run("table-column-count", rule, {
`,
errors: [
{
messageId: "inconsistentColumnCount",
messageId: "extraCells",
data: { actualCells: "3", expectedCells: "2" },
line: 3,
column: 13,
Expand All @@ -209,15 +227,15 @@ ruleTester.run("table-column-count", rule, {
`,
errors: [
{
messageId: "inconsistentColumnCount",
messageId: "extraCells",
data: { actualCells: "3", expectedCells: "2" },
line: 3,
column: 13,
endLine: 3,
endColumn: 23,
},
{
messageId: "inconsistentColumnCount",
messageId: "extraCells",
data: { actualCells: "3", expectedCells: "2" },
line: 4,
column: 13,
Expand All @@ -236,15 +254,15 @@ ruleTester.run("table-column-count", rule, {
`,
errors: [
{
messageId: "inconsistentColumnCount",
messageId: "extraCells",
data: { actualCells: "3", expectedCells: "2" },
line: 3,
column: 13,
endLine: 3,
endColumn: 23,
},
{
messageId: "inconsistentColumnCount",
messageId: "extraCells",
data: { actualCells: "3", expectedCells: "2" },
line: 5,
column: 13,
Expand All @@ -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,
},
],
},
],
});