Skip to content

Commit

Permalink
Check for multiline eslint-disable-line comments
Browse files Browse the repository at this point in the history
  • Loading branch information
nzakas committed Sep 17, 2024
1 parent e8babf4 commit b0bc353
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
17 changes: 16 additions & 1 deletion src/language/markdown-source-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,22 @@ export class MarkdownSourceCode extends TextSourceCodeBase {
justification: justificationPart,
} = commentParser.parseDirective(comment.value);

// Step 2: Extract the directive value and create the Directive object
// Step 2: Validate the directive does not span multiple lines
if (
label === "eslint-disable-line" &&
comment.position.start.line !== comment.position.end.line
) {
const message = `${label} comment should not span multiple lines.`;

problems.push({
ruleId: null,
message,
loc: comment.position,
});
return;
}

// Step 3: Extract the directive value and create the Directive object
switch (label) {
case "eslint-disable":
case "eslint-enable":
Expand Down
31 changes: 28 additions & 3 deletions tests/language/markdown-source-code.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ This is a paragraph with an inline config comment. <!-- eslint-disable-line no-c
<!--
eslint-enable no-console -- ok to use console here
-->Something something<!-- eslint-disable semi -->`;
-->Something something<!-- eslint-disable semi -->
<!--
eslint-disable-line no-console
-->`;

const ast = fromMarkdown(markdownText);

Expand Down Expand Up @@ -89,7 +93,7 @@ describe("MarkdownSourceCode", () => {
describe("getInlineConfigNodes()", () => {
it("should return the inline config nodes", () => {
const nodes = sourceCode.getInlineConfigNodes();
assert.strictEqual(nodes.length, 4);
assert.strictEqual(nodes.length, 5);

/* eslint-disable no-restricted-properties -- Needed to avoid extra asserts. */

Expand Down Expand Up @@ -125,6 +129,14 @@ describe("MarkdownSourceCode", () => {
},
});

assert.deepEqual(nodes[4], {
value: "eslint-disable-line no-console",
position: {
start: { line: 21, column: 1, offset: 386 },
end: { line: 23, column: 4, offset: 427 },
},
});

/* eslint-enable no-restricted-properties -- Needed to avoid extra asserts. */
});
});
Expand All @@ -133,7 +145,18 @@ describe("MarkdownSourceCode", () => {
it("should return the disable directives", () => {
const { problems, directives } = sourceCode.getDisableDirectives();

assert.strictEqual(problems.length, 0);
assert.strictEqual(problems.length, 1);

assert.strictEqual(problems[0].ruleId, null);
assert.strictEqual(
problems[0].message,
"eslint-disable-line comment should not span multiple lines.",
);
assert.deepStrictEqual(problems[0].loc, {
start: { line: 21, column: 1, offset: 386 },
end: { line: 23, column: 4, offset: 427 },
});

assert.strictEqual(directives.length, 4);

assert.strictEqual(directives[0].type, "disable-next-line");
Expand Down Expand Up @@ -218,6 +241,8 @@ describe("MarkdownSourceCode", () => {
"html",
"<!--\neslint-enable no-console -- ok to use console here\n-->Something something<!-- eslint-disable semi -->",
],
[1, "html", "<!--\n eslint-disable-line no-console\n -->"],
[2, "html", "<!--\n eslint-disable-line no-console\n -->"],
[2, "root", void 0],
]);
});
Expand Down

0 comments on commit b0bc353

Please sign in to comment.