sass / linter

An experimental Sass linter written using the Dart Sass AST
MIT License
39 stars 6 forks source link

Add a lint rule for unnecessary parentheses in conditionals #15

Open nex3 opened 6 years ago

nex3 commented 6 years ago

Sometimes users who are familiar with other languages forget that Sass doesn't need parentheses around its conditionals (as in, @if (...) {, or @else if (...) {, or @while (...) {). The linter should suggest that these parentheses be removed.

mik01aj commented 6 years ago

I could tackle this in the future, but for now I just need help getting the project running. :man_shrugging:

srawlins commented 6 years ago

Hi @nex3 , this one is pretty tricky, as Expression has no isParenthesized or anything. Looks to me like this would require a change to dart-sass, to track such things. WDYT?

nex3 commented 6 years ago

That's possible, but it would add a lot of boilerplate to the parsing code. Could you use something like expression.span.text.codeUnitAt(0) == $lparen?

srawlins commented 6 years ago

The parens are actually not included in the expression's span either. Here's a little debug print:

Given IfNode: "@if 1 == 2 { @warn('uh oh'); }",
clause span is:   "1 == 2"

Given IfNode: "@if (1 == 2) { @warn('uh oh'); }",
clause span is:   "1 == 2"

Given IfNode: "@if (1+1)*2 == 2 { @warn('uh oh'); }",
clause span is:   "1+1)*2 == 2"

Given IfNode: "@if ((1+1)*2) - 1 == 2 { @warn('uh oh'); }",
clause span is:   "1+1)*2) - 1 == 2"

Since nested parethesized expressions also don't include their parens, I can't do some hack where I just subtract one from the leftmost character or something.

I ran into this when I was writing the formatter as well.

nex3 commented 6 years ago

Oh, that's not good. I've filed https://github.com/sass/dart-sass/pull/503 to fix it, which should also make detecting parens much easier.

srawlins commented 6 years ago

@mik01aj Want to tackle this one? I've got the project fixed up for Dart 2. This should be a fairly straight-forward rule; the existing ones would make fine examples, like use_falsey_null.

mik01aj commented 5 years ago

Sorry for the late reply - only now I had the time to setup the project, learn some Dart basics, and make a PR (#30). Please have a look :)