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

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

Issue 1419373009: Lint to prefer x.isNotEmpty vs. !x.isEmpty (#143). (Closed) Base URL: https://github.com/dart-lang/linter.git@master
Patch Set: Created 5 years, 1 month 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') | lib/src/sdk.dart » ('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) 2015, 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.prefer_is_not_empty;
6
7 import 'package:analyzer/src/generated/ast.dart'
8 show
9 AstNode,
10 AstVisitor,
11 PrefixExpression,
12 PrefixedIdentifier,
13 PropertyAccess,
14 SimpleAstVisitor,
15 SimpleIdentifier;
16 import 'package:analyzer/src/generated/element.dart' show Element;
17 import 'package:analyzer/src/generated/scanner.dart' show TokenType;
18 import 'package:linter/src/ast.dart';
19 import 'package:linter/src/linter.dart';
20
21 const desc = 'Use isNotEmpty for Iterables and Maps.';
22
23 const details = '''
24 **PREFER** `x.isNotEmpty` to `!x.isEmpty` for Iterables and Maps.
25
26 When testing whether an iterable or map is empty, prefer `isNotEmpty` over
27 `!isEmpty`.
28
29 **GOOD:**
30 ```
31 if (todo.isEmpty) {
scheglov 2015/11/05 04:49:07 Do you mean this line to be? if (todo.isNotEmpty)
pquitslund 2015/11/05 04:52:41 Ha! Absolutely, yes. :)
32 sendResults(request, todo.isEmpty);
33 }
34 ```
35
36 **BAD:**
37 ```
38 if (!sources.isEmpty) {
39 process(sources);
40 }
41 ```
42 ''';
43
44 class PreferIsNotEmpty extends LintRule {
45 PreferIsNotEmpty()
46 : super(
47 name: 'prefer_is_not_empty',
48 description: desc,
49 details: details,
50 group: Group.style);
51
52 @override
53 AstVisitor getVisitor() => new Visitor(this);
54 }
55
56 class Visitor extends SimpleAstVisitor {
57 final LintRule rule;
58 Visitor(this.rule);
59
60 @override
61 visitSimpleIdentifier(SimpleIdentifier identifier) {
62 AstNode isEmptyAccess = null;
63 SimpleIdentifier isEmptyIdentifier = null;
64
65 AstNode parent = identifier.parent;
66 if (parent is PropertyAccess) {
67 isEmptyIdentifier = parent.propertyName;
68 isEmptyAccess = parent;
69 } else if (parent is PrefixedIdentifier) {
70 isEmptyIdentifier = parent.identifier;
71 isEmptyAccess = parent;
72 }
73
74 if (isEmptyIdentifier == null) {
75 return;
76 }
77
78 // Should be "isEmpty".
79 Element propertyElement = isEmptyIdentifier.bestElement;
80 if (propertyElement == null || 'isEmpty' != propertyElement.name) {
81 return;
82 }
83 // Should have "isNotEmpty".
84 Element propertyTarget = propertyElement.enclosingElement;
85 if (propertyTarget == null ||
86 getChildren(propertyTarget, 'isNotEmpty').isEmpty) {
87 return;
88 }
89 // Should be in PrefixExpression.
90 if (isEmptyAccess.parent is! PrefixExpression) {
91 return;
92 }
93 PrefixExpression prefixExpression =
94 isEmptyAccess.parent as PrefixExpression;
95 // Should be !
96 if (prefixExpression.operator.type != TokenType.BANG) {
97 return;
98 }
99
100 rule.reportLint(prefixExpression);
101 }
102 }
OLDNEW
« no previous file with comments | « lib/src/rules.dart ('k') | lib/src/sdk.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698