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

Improve inline comment merging #4950

Merged
merged 4 commits into from Sep 24, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
139 changes: 139 additions & 0 deletions lib/__tests__/disableRanges.test.js
Expand Up @@ -805,6 +805,29 @@ it('SCSS // disable comment (with // comment after blank line)', () => {
});
});

it('SCSS // disable comment (with // comment immediately after)', () => {
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 */
Expand All @@ -829,6 +852,122 @@ it('SCSS /* disable comment (with // comment after blank line)', () => {
});
});

it('SCSS // disable comment (with // comment immediately before)', () => {
const scssSource = `a {
// Unrelated
// stylelint-disable declaration-no-important
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: 3,
strictStart: true,
},
],
});
});
});

it('SCSS two adjacent // disable comments ', () => {
const scssSource = `a {
// stylelint-disable declaration-no-important
// stylelint-disable foo-bar
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,
},
],
'foo-bar': [
{
start: 3,
strictStart: true,
},
],
});
});
});

it('SCSS two adjacent // disable comments with multi-line descriptions ', () => {
const scssSource = `a {
// stylelint-disable declaration-no-important --
// Description 1
// stylelint-disable foo-bar
// --
// Description 2
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,
description: 'Description 1',
},
],
'foo-bar': [
{
start: 4,
strictStart: true,
description: 'Description 2',
},
],
});
});
});

it('SCSS two // disable comments with an unrelated comment between them', () => {
const scssSource = `a {
// stylelint-disable declaration-no-important
// Unrelated
// stylelint-disable foo-bar
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,
},
],
'foo-bar': [
{
start: 4,
strictStart: true,
},
],
});
});
});

it('Less // line-disabling comment (with description)', () => {
const lessSource = `a {
color: pink !important; // stylelint-disable-line declaration-no-important -- Description
Expand Down
65 changes: 45 additions & 20 deletions lib/assignDisabledRanges.js
Expand Up @@ -66,35 +66,53 @@ module.exports = function (root, result) {
if (inlineEnd) {
// Ignore comments already processed by grouping with a previous one.
if (inlineEnd === comment) inlineEnd = null;
} 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;
return;
}

if (!isInlineComment(current)) break;
const next = comment.next();

// If any of these conditions are not met, do not merge comments.
if (
!(
isInlineComment(comment) &&
isStylelintCommand(comment) &&
next &&
next.type === 'comment' &&
(comment.text.includes('--') || next.text.startsWith('--'))
)
) {
checkComment(comment);

const currentLine = (current.source && current.source.end && current.source.end.line) || 0;
return;
}

if (lastLine + 1 !== currentLine) break;
let lastLine = (comment.source && comment.source.end && comment.source.end.line) || 0;
const fullComment = comment.clone();

fullComment.text += `\n${current.text}`;
/** @type {PostcssComment} */
let current = next;

if (fullComment.source && current.source) {
fullComment.source.end = current.source.end;
}
while (isInlineComment(current) && !isStylelintCommand(current)) {
const currentLine = (current.source && current.source.end && current.source.end.line) || 0;

if (lastLine + 1 !== currentLine) break;

inlineEnd = current;
next = current.next();
lastLine = currentLine;
fullComment.text += `\n${current.text}`;

if (fullComment.source && current.source) {
fullComment.source.end = current.source.end;
}
checkComment(fullComment);
} else {
checkComment(comment);

inlineEnd = current;
const next = current.next();

if (!next || next.type !== 'comment') break;

current = next;
lastLine = currentLine;
}
checkComment(fullComment);
});

return result;
Expand All @@ -108,6 +126,13 @@ module.exports = function (root, result) {
return comment.inline || comment.raws.inline;
}

/**
* @param {PostcssComment} comment
*/
function isStylelintCommand(comment) {
return /^stylelint-(disable|enable)/.test(comment.text);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just remembered that we have disableCommand and enableCommand in this file. I think we should use them instead of hardcoded values. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that makes sense. Should I just call startsWith with both or should I put a compiled RegExp in a global variable and use that? (since a regex literal couldn't be used in this case)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would choose startsWith because it's easier to understand.

}

/**
* @param {PostcssComment} comment
*/
Expand Down