Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(38)

Unified Diff: lib/src/rules/whitespace_around_ops.dart

Issue 2159123003: New `whitespace_around_ops` lint (#249). (Closed) Base URL: https://github.com/dart-lang/linter.git@master
Patch Set: Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/src/rules.dart ('k') | test/rules/whitespace_around_ops.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/rules/whitespace_around_ops.dart
diff --git a/lib/src/rules/whitespace_around_ops.dart b/lib/src/rules/whitespace_around_ops.dart
new file mode 100644
index 0000000000000000000000000000000000000000..5dbc06d628381d34f18d6abb76e5afa9f61bf657
--- /dev/null
+++ b/lib/src/rules/whitespace_around_ops.dart
@@ -0,0 +1,73 @@
+// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+library linter.src.rules.whitespace_around_ops;
+
+import 'package:analyzer/dart/ast/ast.dart';
+import 'package:analyzer/dart/ast/token.dart';
+import 'package:analyzer/dart/ast/visitor.dart';
+import 'package:linter/src/linter.dart';
+
+const desc = r'Use proper whitespace around operators.';
+
+const details = r'''
+**DO** ensure that there are spaces around binary operators and before any
+unary ones.
+
+Improper whitespace can create confusion, especially when applied to operators
+where it's possible to get a binary operator when you mean a unary one. For
+example, the mistyping of `5 /~ 10` when you mean `5 ~/ 10` is hidden by the
+improper spacing. (Properly spaced, the mistake is more clear: `5 / ~10`.)
+Whenever possible, use the formatter to cleanup whitespace. Otherwise, take
+care to ensure that there are spaces around binary operators and before any
+unary ones.
+
+
+**BAD:**
+```
+print(5 /~ 10); //whoops
+```
+
+**GOOD:**
+```
+print(5 / ~10); //aha!
+```
+''';
+
+class Visitor extends SimpleAstVisitor {
+ final LintRule rule;
+ Visitor(this.rule);
+
+ @override
+ visitBinaryExpression(BinaryExpression node) {
+ if (!spaced(node.leftOperand.endToken, node.operator) ||
+ !spaced(node.operator, node.rightOperand.beginToken)) {
+ rule.reportLintForToken(node.operator);
+ }
+ }
+
+ @override
+ visitPrefixExpression(PrefixExpression node) {
+ if (spaced(node.operator, node.operand.beginToken)) {
+ rule.reportLintForToken(node.operator);
+ }
+ }
+
+ static bool spaced(Token first, Token second) =>
+ first != null &&
+ second != null &&
+ first.offset + (first.length - 1) != second.offset - 1;
Brian Wilkerson 2016/07/19 16:47:51 Perhaps just "first.end != second.offset".
+}
+
+class WhitespaceAroundOps extends LintRule {
+ WhitespaceAroundOps()
+ : super(
+ name: 'whitespace_around_ops',
+ description: desc,
+ details: details,
+ group: Group.style);
+
+ @override
+ AstVisitor getVisitor() => new Visitor(this);
+}
« no previous file with comments | « lib/src/rules.dart ('k') | test/rules/whitespace_around_ops.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698