From eb3b04adb9611d249f8587f03b302182e81c5494 Mon Sep 17 00:00:00 2001 From: Jennifer Thakar Date: Mon, 31 Aug 2020 11:25:01 -0700 Subject: [PATCH] Avoid combining disable comments with a blank line Fixes #4911. --- lib/__tests__/disableRanges.test.js | 48 +++++++++++++++++++++++++++++ lib/assignDisabledRanges.js | 6 ++++ 2 files changed, 54 insertions(+) diff --git a/lib/__tests__/disableRanges.test.js b/lib/__tests__/disableRanges.test.js index d30a669b82..cef906a581 100644 --- a/lib/__tests__/disableRanges.test.js +++ b/lib/__tests__/disableRanges.test.js @@ -781,6 +781,54 @@ it('SCSS // disable next-line comment (with multi-line description)', () => { }); }); +it('SCSS // disable comment (with // comment after blank line)', () => { + const scssSource = `a { + // stylelint-disable declaration-no-important + + // Unrelated + color: pink !important; + }`; + + return postcss() + .use(assignDisabledRanges) + .process(scssSource, { syntax: scss, from: undefined }) + .then((result) => { + expect(result.stylelint.disabledRanges).toEqual({ + all: [], + 'declaration-no-important': [ + { + start: 2, + strictStart: true, + }, + ], + }); + }); +}); + +it('SCSS /* disable comment (with // comment after blank line)', () => { + const scssSource = `a { + /* stylelint-disable declaration-no-important */ + + // Unrelated + color: pink !important; + }`; + + return postcss() + .use(assignDisabledRanges) + .process(scssSource, { syntax: scss, from: undefined }) + .then((result) => { + expect(result.stylelint.disabledRanges).toEqual({ + all: [], + 'declaration-no-important': [ + { + start: 2, + strictStart: true, + }, + ], + }); + }); +}); + it('Less // line-disabling comment (with description)', () => { const lessSource = `a { color: pink !important; // stylelint-disable-line declaration-no-important -- Description diff --git a/lib/assignDisabledRanges.js b/lib/assignDisabledRanges.js index 9405207f00..d57043e059 100644 --- a/lib/assignDisabledRanges.js +++ b/lib/assignDisabledRanges.js @@ -69,6 +69,7 @@ module.exports = function (root, result) { } else if (isInlineComment(comment)) { const fullComment = comment.clone(); let next = comment.next(); + let lastLine = (comment.source && comment.source.end && comment.source.end.line) || 0; while (next && next.type === 'comment') { /** @type {PostcssComment} */ @@ -76,6 +77,10 @@ module.exports = function (root, result) { if (!isInlineComment(current)) break; + const currentLine = (current.source && current.source.end && current.source.end.line) || 0; + + if (lastLine + 1 !== currentLine) break; + fullComment.text += `\n${current.text}`; if (fullComment.source && current.source) { @@ -84,6 +89,7 @@ module.exports = function (root, result) { inlineEnd = current; next = current.next(); + lastLine = currentLine; } checkComment(fullComment); } else {