Skip to content

Commit

Permalink
Fix comment-no-loud only catching first line
Browse files Browse the repository at this point in the history
This changes the approach to looking up comments for the comment-no-loud
rule. This rule had an issue where it would only identify comments if
they were the first node within an SCSS file.

Previously this check matched a regex against `source.input.css` of the
comment node. `source.input.css` is actually the full input of the
source file and not the input of the node (as I imagine was expected
when this was initially authored). This meant that only comments that
were not preceded by any other nodes matched the rule. The tests all
succeeded for this as they all checked against the first node.

This new approach instead takes the source input and splits into an array
based on a `\n` separator. It then looks up the first line of the
comment with `source.start.line` variable. This provides the first
line of the comment input as was given in the source file.

I did also try using `comment.toString()` as a simpler means to get the
original source value, however this approach didn't provide an exact
copy of the original input and instead converted SCSS comments (`//`)
into CSS ones (`/*`) meaning that all SCSS comments were identified as
loud.
  • Loading branch information
kevindew committed May 26, 2020
1 parent 5178638 commit d90852d
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/rules/comment-no-loud/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ function rule(primary) {
function isLoudComment(comment) {
const regex = new RegExp(/^[ \t\n]*\/\*/);

return regex.test(comment.source.input.css);
const splitComment = comment.source.input.css.split("\n");
const commentFirstLine = splitComment[comment.source.start.line - 1];

return regex.test(commentFirstLine);
}

export default rule;

0 comments on commit d90852d

Please sign in to comment.