From b0bc353e901ef65ee70b79ed8ce75a9d0860d584 Mon Sep 17 00:00:00 2001 From: "Nicholas C. Zakas" Date: Tue, 17 Sep 2024 12:17:05 -0400 Subject: [PATCH] Check for multiline eslint-disable-line comments --- src/language/markdown-source-code.js | 17 ++++++++++- tests/language/markdown-source-code.test.js | 31 +++++++++++++++++++-- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/src/language/markdown-source-code.js b/src/language/markdown-source-code.js index cc8049a6..94b68b6e 100644 --- a/src/language/markdown-source-code.js +++ b/src/language/markdown-source-code.js @@ -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": diff --git a/tests/language/markdown-source-code.test.js b/tests/language/markdown-source-code.test.js index 620288cc..8b89d00b 100644 --- a/tests/language/markdown-source-code.test.js +++ b/tests/language/markdown-source-code.test.js @@ -33,7 +33,11 @@ This is a paragraph with an inline config comment. Something something`; +-->Something something + +`; const ast = fromMarkdown(markdownText); @@ -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. */ @@ -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. */ }); }); @@ -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"); @@ -218,6 +241,8 @@ describe("MarkdownSourceCode", () => { "html", "Something something", ], + [1, "html", ""], + [2, "html", ""], [2, "root", void 0], ]); });