Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid combining disable comments with a blank line #4913

Merged
merged 1 commit into from Sep 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 48 additions & 0 deletions lib/__tests__/disableRanges.test.js
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions lib/assignDisabledRanges.js
Expand Up @@ -69,13 +69,18 @@ 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} */
const current = next;

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) {
Expand All @@ -84,6 +89,7 @@ module.exports = function (root, result) {

inlineEnd = current;
next = current.next();
lastLine = currentLine;
}
checkComment(fullComment);
} else {
Expand Down