mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2026-01-26 08:56:09 +01:00
Update MD055/table-pipe-style to support "consistent" and other styles, use micromark positioning, report ranges, add more tests.
This commit is contained in:
parent
a563c082a5
commit
7d2248d211
28 changed files with 4413 additions and 185 deletions
26
lib/configuration.d.ts
vendored
26
lib/configuration.d.ts
vendored
|
|
@ -1048,6 +1048,28 @@ export interface Configuration {
|
|||
*/
|
||||
url_inline?: boolean;
|
||||
};
|
||||
/**
|
||||
* MD055/table-pipe-style : Table pipe style : https://github.com/DavidAnson/markdownlint/blob/v0.32.1/doc/md055.md
|
||||
*/
|
||||
MD055?:
|
||||
| boolean
|
||||
| {
|
||||
/**
|
||||
* Table pipe style
|
||||
*/
|
||||
style?: "consistent" | "leading_only" | "trailing_only" | "leading_and_trailing" | "no_leading_or_trailing";
|
||||
};
|
||||
/**
|
||||
* MD055/table-pipe-style : Table pipe style : https://github.com/DavidAnson/markdownlint/blob/v0.32.1/doc/md055.md
|
||||
*/
|
||||
"table-pipe-style"?:
|
||||
| boolean
|
||||
| {
|
||||
/**
|
||||
* Table pipe style
|
||||
*/
|
||||
style?: "consistent" | "leading_only" | "trailing_only" | "leading_and_trailing" | "no_leading_or_trailing";
|
||||
};
|
||||
/**
|
||||
* headings : MD001, MD003, MD018, MD019, MD020, MD021, MD022, MD023, MD024, MD025, MD026, MD036, MD041, MD043
|
||||
*/
|
||||
|
|
@ -1140,5 +1162,9 @@ export interface Configuration {
|
|||
* images : MD045, MD052, MD053, MD054
|
||||
*/
|
||||
images?: boolean;
|
||||
/**
|
||||
* table : MD055
|
||||
*/
|
||||
table?: boolean;
|
||||
[k: string]: unknown;
|
||||
}
|
||||
|
|
|
|||
79
lib/md055.js
79
lib/md055.js
|
|
@ -2,26 +2,71 @@
|
|||
|
||||
"use strict";
|
||||
|
||||
const { addErrorContext } = require("../helpers");
|
||||
const { addErrorDetailIf } = require("../helpers");
|
||||
const { filterByTypes } = require("../helpers/micromark.cjs");
|
||||
|
||||
const whitespaceTypes = new Set([ "linePrefix", "whitespace" ]);
|
||||
const ignoreWhitespace = (tokens) => tokens.filter(
|
||||
(token) => !whitespaceTypes.has(token.type)
|
||||
);
|
||||
const firstOrNothing = (items) => items[0];
|
||||
const lastOrNothing = (items) => items[items.length - 1];
|
||||
const makeRange = (start, end) => [ start, end - start + 1 ];
|
||||
|
||||
module.exports = {
|
||||
"names": [ "MD055", "table-missing-border" ],
|
||||
"description": "Table is missing leading or trailing pipe character",
|
||||
"tags": [ "table" ],
|
||||
"function": function MD055(params, onError) {
|
||||
const tables = filterByTypes(params.parsers.micromark.tokens, [ "table" ]);
|
||||
for (const table of tables) {
|
||||
const rows = filterByTypes(table.children, [ "tableRow", "tableDelimiterRow" ]);
|
||||
for (const row of rows) {
|
||||
const { startLine, text } = row;
|
||||
if (!text.startsWith("|")) {
|
||||
addErrorContext(onError, startLine, text, true);
|
||||
}
|
||||
if (!text.endsWith("|")) {
|
||||
addErrorContext(onError, startLine, text, false, true);
|
||||
}
|
||||
}
|
||||
"names": [ "MD055", "table-pipe-style" ],
|
||||
"description": "Table pipe style",
|
||||
"tags": [ "table" ],
|
||||
"function": function MD055(params, onError) {
|
||||
const style = String(params.config.style || "consistent");
|
||||
let expectedStyle = style;
|
||||
let expectedLeadingPipe =
|
||||
((expectedStyle !== "no_leading_or_trailing") && (expectedStyle !== "trailing_only"));
|
||||
let expectedTrailingPipe =
|
||||
((expectedStyle !== "no_leading_or_trailing") && (expectedStyle !== "leading_only"));
|
||||
const tables = filterByTypes(params.parsers.micromark.tokens, [ "table" ]);
|
||||
for (const table of tables) {
|
||||
const rows = filterByTypes(table.children, [ "tableDelimiterRow", "tableRow" ]);
|
||||
for (const row of rows) {
|
||||
// The following uses of first/lastOrNothing lack fallback handling
|
||||
// because it seems not to be possible (i.e., 0% coverage)
|
||||
const firstCell = firstOrNothing(row.children);
|
||||
const leadingToken = firstOrNothing(ignoreWhitespace(firstCell.children));
|
||||
const actualLeadingPipe = (leadingToken.type === "tableCellDivider");
|
||||
const lastCell = lastOrNothing(row.children);
|
||||
const trailingToken = lastOrNothing(ignoreWhitespace(lastCell.children));
|
||||
const actualTrailingPipe = (trailingToken.type === "tableCellDivider");
|
||||
const actualStyle = actualLeadingPipe ?
|
||||
(actualTrailingPipe ? "leading_and_trailing" : "leading_only") :
|
||||
(actualTrailingPipe ? "trailing_only" : "no_leading_or_trailing");
|
||||
if (expectedStyle === "consistent") {
|
||||
expectedStyle = actualStyle;
|
||||
expectedLeadingPipe = actualLeadingPipe;
|
||||
expectedTrailingPipe = actualTrailingPipe;
|
||||
}
|
||||
if (actualLeadingPipe !== expectedLeadingPipe) {
|
||||
addErrorDetailIf(
|
||||
onError,
|
||||
firstCell.startLine,
|
||||
expectedStyle,
|
||||
actualStyle,
|
||||
`${expectedLeadingPipe ? "Missing" : "Unexpected"} leading pipe`,
|
||||
undefined,
|
||||
makeRange(row.startColumn, firstCell.startColumn)
|
||||
);
|
||||
}
|
||||
if (actualTrailingPipe !== expectedTrailingPipe) {
|
||||
addErrorDetailIf(
|
||||
onError,
|
||||
lastCell.endLine,
|
||||
expectedStyle,
|
||||
actualStyle,
|
||||
`${expectedTrailingPipe ? "Missing" : "Unexpected"} trailing pipe`,
|
||||
undefined,
|
||||
makeRange(lastCell.endColumn - 1, row.endColumn - 1)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue