OLD | NEW |
---|---|
(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 } | |
OLD | NEW |