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

Make scss/comment-no-empty treat multiline double-slash comments as blocks #607

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 14 additions & 0 deletions src/rules/comment-no-empty/README.md
Expand Up @@ -47,6 +47,13 @@ The following patterns are considered violations:
//
```

<!-- prettier-ignore -->
```scss
//
//
//
```

<!-- prettier-ignore -->
```scss
width: 10px; //
Expand All @@ -65,3 +72,10 @@ The following patterns are _not_ considered violations:
* Multi-line Comment
**/
```

<!-- prettier-ignore -->
```scss
// Multi-line double-slash comment...
//
// ...with paragraph breaks
```
55 changes: 55 additions & 0 deletions src/rules/comment-no-empty/__tests__/index.js
Expand Up @@ -17,6 +17,33 @@ testRule({
{
code: "/* comment */",
description: "Single line block comment"
},
{
code: `
// comment
//
// comment
`,
description: "Multiline double slash comment with paragraph break"
},
{
code: `
//
//
// comment
//
//
`,
description:
"Multiline double slash comment with leading and trailing empty lines"
},
{
code: `
/* comment

comment */
`,
description: "Multiline block comment with paragraph break"
}
],
reject: [
Expand All @@ -39,6 +66,34 @@ testRule({
line: 2,
column: 9
},
{
code: `
//
//
//
`,
description: "Empty multiline double slash comment",
message: messages.rejected,
line: 2 // note, should be just one error at first line
},
{
code: `
//
width: 100px; // comment
`,
description: "Empty double slash comment with trailing inline comment",
message: messages.rejected,
line: 2
},
{
code: `
width: 100px; // comment
//
`,
description: "Empty double slash comment with preceding inline comment",
message: messages.rejected,
line: 3
},
{
code: `
/*
Expand Down
57 changes: 49 additions & 8 deletions src/rules/comment-no-empty/index.js
@@ -1,5 +1,5 @@
import { rules, utils } from "stylelint";
import { namespace, ruleUrl } from "../../utils";
import { isInlineComment, namespace, ruleUrl } from "../../utils";

const coreRuleName = "comment-no-empty";

Expand All @@ -26,14 +26,44 @@ export default function rule(primary) {
return;
}

let doubleSlashCommentBlockState = null;

root.walkComments(comment => {
if (isEmptyComment(comment)) {
utils.report({
message: messages.rejected,
node: comment,
result,
ruleName
});
const isEmpty = isEmptyComment(comment);

// postcss considers multiple double-slash comments in a row to be separate comments,
// but for this rule's purposes we want to track them as one combined comment block.
if (isStandaloneDoubleSlashComment(comment)) {
if (doubleSlashCommentBlockState) {
doubleSlashCommentBlockState.isBlockEmptySoFar &= isEmpty;
} else {
doubleSlashCommentBlockState = {
firstCommentInBlock: comment,
isBlockEmptySoFar: isEmpty
};
}

if (!isStandaloneDoubleSlashComment(comment.next())) {
if (doubleSlashCommentBlockState.isBlockEmptySoFar) {
utils.report({
message: messages.rejected,
node: doubleSlashCommentBlockState.firstCommentInBlock,
result,
ruleName
});
}
doubleSlashCommentBlockState = null;
}
} else {
if (isEmptyComment(comment)) {
utils.report({
message: messages.rejected,
node: comment,
result,
ruleName
});
}
doubleSlashCommentBlockState = null;
}
});
};
Expand All @@ -43,6 +73,17 @@ rule.ruleName = ruleName;
rule.messages = messages;
rule.meta = meta;

function isStandaloneDoubleSlashComment(node) {
return (
node &&
node.type === "comment" &&
// Must be a double-slash comment
(node.raws.inline || node.inline) &&
// Must be standalone
!isInlineComment(node)
);
}

function isEmptyComment(comment) {
return comment.text === "";
}