Hide the content of inline configuration HTML comments from the linting process to avoid false positives.

This commit is contained in:
David Anson 2022-06-04 22:59:19 -07:00
parent 535aa1a2ee
commit bbec8c5c1e
7 changed files with 80 additions and 40 deletions

View file

@ -174,16 +174,10 @@ module.exports.clearHtmlCommentText = function clearHtmlCommentText(text) {
!content.endsWith("-") && !content.includes("--"));
// If a valid block/inline comment...
if (isValid) {
const inlineCommentIndex = text
.slice(i, j + htmlCommentEnd.length)
.search(inlineCommentStartRe);
// If not a markdownlint inline directive...
if (inlineCommentIndex !== 0) {
text =
text.slice(0, i + htmlCommentBegin.length) +
content.replace(/[^\r\n]/g, ".") +
text.slice(j);
}
text =
text.slice(0, i + htmlCommentBegin.length) +
content.replace(/[^\r\n]/g, ".") +
text.slice(j);
}
}
}
@ -1701,15 +1695,16 @@ function lintContent(ruleList, name, content, md, config, frontMatter, handleRul
content = content.replace(/^\uFEFF/, "");
// Remove front matter
const removeFrontMatterResult = removeFrontMatter(content, frontMatter);
const frontMatterLines = removeFrontMatterResult.frontMatterLines;
// Ignore the content of HTML comments
content = helpers.clearHtmlCommentText(removeFrontMatterResult.content);
const { frontMatterLines } = removeFrontMatterResult;
content = removeFrontMatterResult.content;
// Get enabled rules per line (with HTML comments present)
const { effectiveConfig, enabledRulesPerLineNumber } = getEnabledRulesPerLineNumber(ruleList, content.split(helpers.newLineRe), frontMatterLines, noInlineConfig, config, mapAliasToRuleNames(ruleList));
// Hide the content of HTML comments from rules, etc.
content = helpers.clearHtmlCommentText(content);
// Parse content into tokens and lines
const tokens = md.parse(content, {});
const lines = content.split(helpers.newLineRe);
annotateTokens(tokens, lines);
const aliasToRuleNames = mapAliasToRuleNames(ruleList);
const { effectiveConfig, enabledRulesPerLineNumber } = getEnabledRulesPerLineNumber(ruleList, lines, frontMatterLines, noInlineConfig, config, aliasToRuleNames);
// Create parameters for rules
const paramsBase = helpers.deepFreeze({
name,

View file

@ -161,16 +161,10 @@ module.exports.clearHtmlCommentText = function clearHtmlCommentText(text) {
!content.endsWith("-") && !content.includes("--"));
// If a valid block/inline comment...
if (isValid) {
const inlineCommentIndex = text
.slice(i, j + htmlCommentEnd.length)
.search(inlineCommentStartRe);
// If not a markdownlint inline directive...
if (inlineCommentIndex !== 0) {
text =
text.slice(0, i + htmlCommentBegin.length) +
content.replace(/[^\r\n]/g, ".") +
text.slice(j);
}
text =
text.slice(0, i + htmlCommentBegin.length) +
content.replace(/[^\r\n]/g, ".") +
text.slice(j);
}
}
}

View file

@ -474,23 +474,24 @@ function lintContent(
content = content.replace(/^\uFEFF/, "");
// Remove front matter
const removeFrontMatterResult = removeFrontMatter(content, frontMatter);
const frontMatterLines = removeFrontMatterResult.frontMatterLines;
// Ignore the content of HTML comments
content = helpers.clearHtmlCommentText(removeFrontMatterResult.content);
const { frontMatterLines } = removeFrontMatterResult;
content = removeFrontMatterResult.content;
// Get enabled rules per line (with HTML comments present)
const { effectiveConfig, enabledRulesPerLineNumber } =
getEnabledRulesPerLineNumber(
ruleList,
content.split(helpers.newLineRe),
frontMatterLines,
noInlineConfig,
config,
mapAliasToRuleNames(ruleList)
);
// Hide the content of HTML comments from rules, etc.
content = helpers.clearHtmlCommentText(content);
// Parse content into tokens and lines
const tokens = md.parse(content, {});
const lines = content.split(helpers.newLineRe);
annotateTokens(tokens, lines);
const aliasToRuleNames = mapAliasToRuleNames(ruleList);
const { effectiveConfig, enabledRulesPerLineNumber } =
getEnabledRulesPerLineNumber(
ruleList,
lines,
frontMatterLines,
noInlineConfig,
config,
aliasToRuleNames
);
// Create parameters for rules
const paramsBase = helpers.deepFreeze({
name,

View file

@ -0,0 +1,10 @@
# Inline Configure File Violations
Emphasis * with * spaces {MD037}
Trailing spaces:
<!-- markdownlint-configure-file {
"comment": "emphasis * with * spaces",
"no-trailing-spaces": false
} -->

View file

@ -155,9 +155,9 @@ test("clearHtmlCommentTextEmbedded", (t) => {
];
const embeddedResult = [
"text<!--....-->text",
"<!-- markdownlint-disable MD010 -->",
"<!--............................-->",
"text<!--....-->text",
"text<!-- markdownlint-disable MD010 -->text",
"text<!--............................-->text",
"text<!--....-->text"
];
const actual = helpers.clearHtmlCommentText(embeddedComments.join("\n"));

View file

@ -17397,6 +17397,46 @@ Generated by [AVA](https://avajs.dev).
`,
}
## inline-configure-file-violations.md
> Snapshot 1
{
errors: [
{
errorContext: '* with *',
errorDetail: null,
errorRange: [
10,
8,
],
fixInfo: {
deleteCount: 8,
editColumn: 10,
insertText: '*with*',
},
lineNumber: 3,
ruleDescription: 'Spaces inside emphasis markers',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md037',
ruleNames: [
'MD037',
'no-space-in-emphasis',
],
},
],
fixed: `# Inline Configure File Violations␊
Emphasis *with* spaces {MD037}␊
Trailing spaces: ␊
<!-- markdownlint-configure-file {␊
"comment": "emphasis * with * spaces",␊
"no-trailing-spaces": false␊
} -->␊
`,
}
## inline-disable-enable-file.md
> Snapshot 1