forked from sass/linter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New rule: no_condition_parens_rule (sass#15)
- Loading branch information
Showing
3 changed files
with
122 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright 2018 Google Inc. Use of this source code is governed by an | ||
// MIT-style license that can be found in the LICENSE file or at | ||
// https://opensource.org/licenses/MIT. | ||
|
||
// The sass package's API is not necessarily stable. It is being imported with | ||
// the Sass team's explicit knowledge and approval. See | ||
// https://github.com/sass/dart-sass/issues/236. | ||
import 'package:sass/src/ast/sass.dart'; | ||
|
||
import '../lint.dart'; | ||
import '../rule.dart'; | ||
|
||
/// A lint rule that reports unnecessary parentheses in conditionals. | ||
/// | ||
/// 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 (...) {`). This rule suggests removing these | ||
/// parentheses. | ||
class NoConditionParensRule extends Rule { | ||
NoConditionParensRule() : super('no_condition_parens_rule'); | ||
|
||
@override | ||
List<Lint> visitIfRule(IfRule node) { | ||
var lint = <Lint>[]; | ||
for (var clause in node.clauses) { | ||
if (clause.expression is ParenthesizedExpression) { | ||
lint.add(Lint( | ||
rule: this, | ||
span: clause.expression.span, | ||
message: | ||
'Parentheses around ${clause.expression.span.text} are unnecessary')); | ||
} | ||
} | ||
return lint..addAll(super.visitIfRule(node)); | ||
} | ||
|
||
@override | ||
List<Lint> visitWhileRule(WhileRule node) { | ||
var lint = <Lint>[]; | ||
if (node.condition is ParenthesizedExpression) { | ||
lint.add(Lint( | ||
rule: this, | ||
span: node.condition.span, | ||
message: | ||
'Parentheses around ${node.condition.span.text} are unnecessary')); | ||
} | ||
return lint..addAll(super.visitWhileRule(node)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright 2018 Google Inc. Use of this source code is governed by an | ||
// MIT-style license that can be found in the LICENSE file or at | ||
// https://opensource.org/licenses/MIT. | ||
|
||
import 'package:sass_linter/src/rules/no_condition_parens.dart'; | ||
import 'package:sass_linter/src/lint.dart'; | ||
import 'package:sass_linter/src/linter.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
final url = 'a.scss'; | ||
final rule = new NoConditionParensRule(); | ||
|
||
void main() { | ||
test('does not report lint when there are no parens', () { | ||
var lints = getLints(r'@if 1 != 7 { @debug("quack"); }'); | ||
|
||
expect(lints, isEmpty); | ||
}); | ||
|
||
test('reports lint when there is a paren in @if', () { | ||
var lints = getLints(r'@if (1 != 7) { @debug("quack"); }'); | ||
|
||
expect(lints, hasLength(1)); | ||
|
||
var lint = lints.single; | ||
expect(lint.rule, rule); | ||
expect(lint.message, contains('Parentheses')); | ||
expect(lint.message, contains('(1 != 7)')); | ||
expect(lint.message, contains('unnecessary')); | ||
expect(lint.url, new Uri.file(url)); | ||
expect(lint.line, 0); | ||
expect(lint.column, 4); | ||
}); | ||
|
||
test('reports lint when there is a paren in @else if', () { | ||
var lints = getLints(r'@if 1 {} @else if (2 != 3) { @debug("quack"); }'); | ||
|
||
expect(lints, hasLength(1)); | ||
|
||
var lint = lints.single; | ||
expect(lint.rule, rule); | ||
expect(lint.message, contains('Parentheses')); | ||
expect(lint.message, contains('(2 != 3)')); | ||
expect(lint.message, contains('unnecessary')); | ||
expect(lint.url, new Uri.file(url)); | ||
expect(lint.line, 0); | ||
expect(lint.column, 18); | ||
}); | ||
|
||
test('reports lint when there is a paren in @while', () { | ||
var lints = getLints(r'@while (2 == 3) { @debug("quack"); }'); | ||
|
||
expect(lints, hasLength(1)); | ||
|
||
var lint = lints.single; | ||
expect(lint.rule, rule); | ||
expect(lint.message, contains('Parentheses')); | ||
expect(lint.message, contains('(2 == 3)')); | ||
expect(lint.message, contains('unnecessary')); | ||
expect(lint.url, new Uri.file(url)); | ||
expect(lint.line, 0); | ||
expect(lint.column, 18); | ||
}); | ||
} | ||
|
||
List<Lint> getLints(String source) => | ||
new Linter(source, [rule], url: url).run(); |