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

Side by Side Diff: lib/src/rules/whitespace_around_ops.dart

Issue 2189993002: Remove `whitespace_around_ops` pending re-name and re-design (#249). (Closed) Base URL: https://github.com/dart-lang/linter.git@master
Patch Set: Created 4 years, 4 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 unified diff | Download patch
« no previous file with comments | « lib/src/rules.dart ('k') | pubspec.yaml » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 library linter.src.rules.whitespace_around_ops;
6
7 import 'package:analyzer/dart/ast/ast.dart';
8 import 'package:analyzer/dart/ast/token.dart';
9 import 'package:analyzer/dart/ast/visitor.dart';
10 import 'package:linter/src/linter.dart';
11
12 const desc = r'Use proper whitespace around operators.';
13
14 const details = r'''
15 **DO** ensure that there are spaces around binary operators and before any
16 unary ones.
17
18 Improper whitespace can create confusion, especially when applied to operators
19 where it's possible to get a binary operator when you mean a unary one. For
20 example, the mistyping of `5 /~ 10` when you mean `5 ~/ 10` is hidden by the
21 improper spacing. (Properly spaced, the mistake is more clear: `5 / ~10`.)
22 Whenever possible, use the formatter to cleanup whitespace. Otherwise, take
23 care to ensure that there are spaces around binary operators and before any
24 unary ones.
25
26
27 **BAD:**
28 ```
29 print(5 /~ 10); //whoops
30 ```
31
32 **GOOD:**
33 ```
34 print(5 / ~10); //aha!
35 ```
36 ''';
37
38 class Visitor extends SimpleAstVisitor {
39 final LintRule rule;
40 Visitor(this.rule);
41
42 @override
43 visitBinaryExpression(BinaryExpression node) {
44 if (!spaced(node.leftOperand.endToken, node.operator) ||
45 !spaced(node.operator, node.rightOperand.beginToken)) {
46 rule.reportLintForToken(node.operator);
47 }
48 }
49
50 @override
51 visitPrefixExpression(PrefixExpression node) {
52 if (spaced(node.operator, node.operand.beginToken)) {
53 rule.reportLintForToken(node.operator);
54 }
55 }
56
57 static bool spaced(Token first, Token second) =>
58 first != null && second != null && first.end != second.offset;
59 }
60
61 class WhitespaceAroundOps extends LintRule {
62 WhitespaceAroundOps()
63 : super(
64 name: 'whitespace_around_ops',
65 description: desc,
66 details: details,
67 group: Group.style);
68
69 @override
70 AstVisitor getVisitor() => new Visitor(this);
71 }
OLDNEW
« no previous file with comments | « lib/src/rules.dart ('k') | pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698