|
| 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.prefer_contains; |
| 6 | + |
| 7 | +import 'package:analyzer/analyzer.dart'; |
| 8 | +import 'package:analyzer/context/declared_variables.dart'; |
| 9 | +import 'package:analyzer/dart/ast/ast.dart'; |
| 10 | +import 'package:analyzer/dart/ast/token.dart'; |
| 11 | +import 'package:analyzer/dart/ast/visitor.dart'; |
| 12 | +import 'package:analyzer/dart/element/element.dart'; |
| 13 | +import 'package:analyzer/dart/element/type.dart'; |
| 14 | +import 'package:analyzer/error/listener.dart'; |
| 15 | +import 'package:linter/src/analyzer.dart'; |
| 16 | +import 'package:linter/src/util/dart_type_utilities.dart'; |
| 17 | + |
| 18 | +const alwaysFalse = |
| 19 | + 'Always false because indexOf is always greater or equal -1.'; |
| 20 | +const alwaysTrue = 'Always true because indexOf is always greater or equal -1.'; |
| 21 | + |
| 22 | +const desc = 'Use contains for Lists and Strings.'; |
| 23 | +const details = ''' |
| 24 | +**DO NOT** use `.indexOf` to see if a collection contains an element. |
| 25 | +
|
| 26 | +The `Iterable` contract does not require that a collection know its length or be |
| 27 | +able to provide it in constant time. Calling `.length` just to see if the |
| 28 | +collection contains anything can be painfully slow. |
| 29 | +
|
| 30 | +Instead, there are faster and more readable getters: `.isEmpty` and |
| 31 | +`.isNotEmpty`. Use the one that doesn’t require you to negate the result. |
| 32 | +
|
| 33 | +**GOOD:** |
| 34 | +``` |
| 35 | +if (lunchBox.isEmpty) return 'so hungry...'; |
| 36 | +if (words.isNotEmpty) return words.join(' '); |
| 37 | +``` |
| 38 | +
|
| 39 | +**BAD:** |
| 40 | +``` |
| 41 | +if (lunchBox.length == 0) return 'so hungry...'; |
| 42 | +if (words.length != 0) return words.join(' '); |
| 43 | +``` |
| 44 | +'''; |
| 45 | + |
| 46 | +const useContains = 'Use contains instead of indexOf'; |
| 47 | + |
| 48 | +class PreferContainsOverIndexOf extends LintRule { |
| 49 | + PreferContainsOverIndexOf() |
| 50 | + : super( |
| 51 | + name: 'prefer_contains', |
| 52 | + description: desc, |
| 53 | + details: details, |
| 54 | + group: Group.style); |
| 55 | + |
| 56 | + @override |
| 57 | + AstVisitor getVisitor() => new _Visitor(this); |
| 58 | + |
| 59 | + void reportLintWithDescription(AstNode node, String description) { |
| 60 | + if (node != null) { |
| 61 | + reporter.reportErrorForNode(new _LintCode(name, description), node, []); |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +class _Visitor extends SimpleAstVisitor { |
| 67 | + final PreferContainsOverIndexOf rule; |
| 68 | + |
| 69 | + _Visitor(this.rule); |
| 70 | + |
| 71 | + @override |
| 72 | + visitSimpleIdentifier(SimpleIdentifier identifier) { |
| 73 | + // Should be "indexOf". |
| 74 | + final Element propertyElement = identifier.bestElement; |
| 75 | + if (propertyElement?.name != 'indexOf') { |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + AstNode indexOfAccess; |
| 80 | + InterfaceType type; |
| 81 | + |
| 82 | + final AstNode parent = identifier.parent; |
| 83 | + if (parent is MethodInvocation && identifier == parent.methodName) { |
| 84 | + indexOfAccess = parent; |
| 85 | + if (parent.target?.bestType is! InterfaceType) { |
| 86 | + return; |
| 87 | + } |
| 88 | + type = parent.target?.bestType; |
| 89 | + } else { |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + if (!DartTypeUtilities |
| 94 | + .implementsAnyInterface(type, <InterfaceTypeDefinition>[ |
| 95 | + new InterfaceTypeDefinition('Iterable', 'dart.core'), |
| 96 | + new InterfaceTypeDefinition('String', 'dart.core'), |
| 97 | + ])) { |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + // Going up in AST structure to find binary comparison operator for this |
| 102 | + // `indexOf` access. Most of the time it will be a parent, but sometimes |
| 103 | + // it can be wrapped in parentheses or `as` operator. |
| 104 | + AstNode search = indexOfAccess; |
| 105 | + while ( |
| 106 | + search != null && search is Expression && search is! BinaryExpression) { |
| 107 | + search = search.parent; |
| 108 | + } |
| 109 | + |
| 110 | + if (search is! BinaryExpression) { |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + final BinaryExpression binaryExpression = search; |
| 115 | + final Token operator = binaryExpression.operator; |
| 116 | + |
| 117 | + final AnalysisContext context = propertyElement.context; |
| 118 | + final TypeProvider typeProvider = context.typeProvider; |
| 119 | + final TypeSystem typeSystem = context.typeSystem; |
| 120 | + |
| 121 | + final DeclaredVariables declaredVariables = context.declaredVariables; |
| 122 | + |
| 123 | + // Comparing constants with result of indexOf. |
| 124 | + |
| 125 | + final ConstantVisitor visitor = new ConstantVisitor( |
| 126 | + new ConstantEvaluationEngine(typeProvider, declaredVariables, |
| 127 | + typeSystem: typeSystem), |
| 128 | + new ErrorReporter( |
| 129 | + AnalysisErrorListener.NULL_LISTENER, rule.reporter.source)); |
| 130 | + |
| 131 | + final DartObjectImpl rightValue = binaryExpression.rightOperand.accept(visitor); |
| 132 | + if (rightValue?.type?.name == "int") { |
| 133 | + // Constant is on right side of comparison operator |
| 134 | + _checkConstant(binaryExpression, rightValue.toIntValue(), operator.type); |
| 135 | + return; |
| 136 | + } |
| 137 | + |
| 138 | + final DartObjectImpl leftValue = binaryExpression.leftOperand.accept(visitor); |
| 139 | + if (leftValue?.type?.name == "int") { |
| 140 | + // Constants is on left side of comparison operator |
| 141 | + _checkConstant(binaryExpression, leftValue.toIntValue(), |
| 142 | + _invertedTokenType(operator.type)); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + void _checkConstant(Expression expression, int value, TokenType type) { |
| 147 | + if (value == -1) { |
| 148 | + if (type == TokenType.EQ_EQ || |
| 149 | + type == TokenType.BANG_EQ || |
| 150 | + type == TokenType.LT_EQ || |
| 151 | + type == TokenType.GT) { |
| 152 | + rule.reportLintWithDescription(expression, useContains); |
| 153 | + } else if (type == TokenType.LT) { |
| 154 | + // indexOf < -1 is always false |
| 155 | + rule.reportLintWithDescription(expression, alwaysFalse); |
| 156 | + } else if (type == TokenType.GT_EQ) { |
| 157 | + // indexOf >= -1 is always true |
| 158 | + rule.reportLintWithDescription(expression, alwaysTrue); |
| 159 | + } |
| 160 | + } else if (value == 0) { |
| 161 | + // 'indexOf >= 0' is same as 'contains', |
| 162 | + // and 'indexOf < 0' is same as '!contains' |
| 163 | + if (type == TokenType.GT_EQ || type == TokenType.LT) { |
| 164 | + rule.reportLintWithDescription(expression, useContains); |
| 165 | + } |
| 166 | + } else if (value < -1) { |
| 167 | + // 'indexOf' is always >= -1, so comparing with lesser values makes |
| 168 | + // no sense. |
| 169 | + if (type == TokenType.EQ_EQ || |
| 170 | + type == TokenType.LT_EQ || |
| 171 | + type == TokenType.LT) { |
| 172 | + rule.reportLintWithDescription(expression, alwaysFalse); |
| 173 | + } else if (type == TokenType.BANG_EQ || |
| 174 | + type == TokenType.GT_EQ || |
| 175 | + type == TokenType.GT) { |
| 176 | + rule.reportLintWithDescription(expression, alwaysTrue); |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + TokenType _invertedTokenType(TokenType type) { |
| 182 | + switch (type) { |
| 183 | + case TokenType.LT_EQ: |
| 184 | + return TokenType.GT_EQ; |
| 185 | + |
| 186 | + case TokenType.LT: |
| 187 | + return TokenType.GT; |
| 188 | + |
| 189 | + case TokenType.GT: |
| 190 | + return TokenType.LT; |
| 191 | + |
| 192 | + case TokenType.GT_EQ: |
| 193 | + return TokenType.LT_EQ; |
| 194 | + |
| 195 | + default: |
| 196 | + return type; |
| 197 | + } |
| 198 | + } |
| 199 | +} |
| 200 | + |
| 201 | +// TODO create common MultiMessageLintCode class |
| 202 | +class _LintCode extends LintCode { |
| 203 | + static final registry = <String, LintCode>{}; |
| 204 | + |
| 205 | + factory _LintCode(String name, String message) => registry.putIfAbsent( |
| 206 | + name + message, () => new _LintCode._(name, message)); |
| 207 | + |
| 208 | + _LintCode._(String name, String message) : super(name, message); |
| 209 | +} |
0 commit comments