Skip to content
Merged
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
18 changes: 18 additions & 0 deletions docs/rules/table-column-count.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,24 @@ 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 -->
| Col A | Col B | Col C |
| ----- | ----- | ----- |
| 1 | | 3 |
| 4 | 5 |
```

## 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
51 changes: 44 additions & 7 deletions src/rules/table-column-count.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

/**
* @import { MarkdownRuleDefinition } from "../types.js";
* @typedef {"inconsistentColumnCount"} TableColumnCountMessageIds
* @typedef {[]} TableColumnCountOptions
* @typedef {"extraCells" | "missingCells"} TableColumnCountMessageIds
* @typedef {[{ checkMissingCells?: boolean }]} TableColumnCountOptions
* @typedef {MarkdownRuleDefinition<{ RuleOptions: TableColumnCountOptions, MessageIds: TableColumnCountMessageIds }>} TableColumnCountRuleDefinition
*/

Expand All @@ -31,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 @@ -49,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,
},
],
},
],
});
Loading