Skip to content
This repository has been archived by the owner on May 21, 2019. It is now read-only.

Add a lint rule for unnecessary parentheses in conditionals #15

Open
nex3 opened this issue Sep 17, 2018 · 7 comments
Open

Add a lint rule for unnecessary parentheses in conditionals #15

nex3 opened this issue Sep 17, 2018 · 7 comments

Comments

@nex3
Copy link

nex3 commented Sep 17, 2018

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
Copy link

mik01aj commented Sep 28, 2018

I could tackle this in the future, but for now I just need help getting the project running. 🤷‍♂️

@srawlins
Copy link
Collaborator

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
Copy link
Author

nex3 commented Oct 17, 2018

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
Copy link
Collaborator

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
Copy link
Author

nex3 commented Oct 17, 2018

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

@srawlins
Copy link
Collaborator

@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
Copy link

mik01aj commented Nov 9, 2018

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 :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants