|
| 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.annotate_types; |
| 6 | + |
| 7 | +import 'package:analyzer/dart/ast/ast.dart'; |
| 8 | +import 'package:analyzer/dart/ast/visitor.dart'; |
| 9 | +import 'package:analyzer/dart/element/element.dart'; |
| 10 | +import 'package:analyzer/dart/element/type.dart'; |
| 11 | +import 'package:linter/src/linter.dart'; |
| 12 | +import 'package:linter/src/util.dart'; |
| 13 | + |
| 14 | +const desc = 'Implicit use of dynamic.'; |
| 15 | + |
| 16 | +const details = ''' |
| 17 | +**AVOID** using "implicitly dynamic" values. |
| 18 | +
|
| 19 | +Untyped / dynamic invocations may fail or be slower at runtime, but dynamic |
| 20 | +types often creep up unintentionally. Explicitly mark variables or return types |
| 21 | +as `dynamic` (instead of `var`) to express your intent unequivocally. |
| 22 | +
|
| 23 | +Note: this works best with the --strong command-line flag and after disabling |
| 24 | +both `always_specify_types` and `always_declare_return_types` lints. |
| 25 | +
|
| 26 | +**GOOD:** |
| 27 | +```dart |
| 28 | +String trim(String s) => s.trim(); |
| 29 | +
|
| 30 | +main() { |
| 31 | + var s = trim(' a ').toUpperCase(); |
| 32 | +
|
| 33 | + dynamic x; |
| 34 | + x = ... ; |
| 35 | + x.reallyNotSureThisExists(); |
| 36 | +} |
| 37 | +``` |
| 38 | +
|
| 39 | +**BAD:** |
| 40 | +```dart |
| 41 | +trim(s) => s.trim(); |
| 42 | +
|
| 43 | +main() { |
| 44 | + var s = trim(1).toUpperCase(); |
| 45 | +
|
| 46 | + var x; |
| 47 | + x = ... ; |
| 48 | + x.reallyNotSureThisExists(); |
| 49 | +} |
| 50 | +``` |
| 51 | +'''; |
| 52 | + |
| 53 | +class NoImplicitDynamic extends LintRule { |
| 54 | + NoImplicitDynamic() |
| 55 | + : super( |
| 56 | + name: 'no_implicit_dynamic', |
| 57 | + description: desc, |
| 58 | + details: details, |
| 59 | + group: Group.style); |
| 60 | + |
| 61 | + @override |
| 62 | + AstVisitor getVisitor() => new Visitor(this); |
| 63 | +} |
| 64 | + |
| 65 | +// TODO(ochafik): Handle implicit return types of method declarations (vs. overrides). |
| 66 | +class Visitor extends SimpleAstVisitor { |
| 67 | + final LintRule rule; |
| 68 | + Visitor(this.rule); |
| 69 | + |
| 70 | + Element _getBestElement(Expression node) { |
| 71 | + if (node is SimpleIdentifier) return node.bestElement; |
| 72 | + if (node is PrefixedIdentifier) return node.bestElement; |
| 73 | + if (node is PropertyAccess) return node.propertyName.bestElement; |
| 74 | + return null; |
| 75 | + } |
| 76 | + |
| 77 | + bool _isImplicitDynamic(Expression node) { |
| 78 | + if (node == null) return false; |
| 79 | + while (node is ParenthesizedExpression) { |
| 80 | + node = node.expression; |
| 81 | + } |
| 82 | + |
| 83 | + if (node is AsExpression || node is Literal) return false; |
| 84 | + var t = node.bestType; |
| 85 | + if (!t.isDynamic && !t.isObject) return false; |
| 86 | + |
| 87 | + var e = _getBestElement(node); |
| 88 | + if (e is PropertyAccessorElement) e = e.variable; |
| 89 | + if (e is VariableElement) return e.hasImplicitType; |
| 90 | + |
| 91 | + if (node is ConditionalExpression) { |
| 92 | + return !node.thenExpression.bestType.isDynamic || |
| 93 | + !node.elseExpression.bestType.isDynamic; |
| 94 | + } |
| 95 | + if (node is MethodInvocation) { |
| 96 | + return node.methodName.bestElement?.hasImplicitReturnType != false; |
| 97 | + } |
| 98 | + |
| 99 | + return true; |
| 100 | + } |
| 101 | + |
| 102 | + void _checkTarget(Expression target, [token]) { |
| 103 | + if (_isImplicitDynamic(target)) { |
| 104 | + // Avoid double taxation (if `x` is dynamic, only lint `x.y.z` once). |
| 105 | + Expression subTarget; |
| 106 | + if (target is PropertyAccess) subTarget = target.realTarget; |
| 107 | + else if (target is MethodInvocation) subTarget = target.realTarget; |
| 108 | + else if (target is IndexExpression) subTarget = target.realTarget; |
| 109 | + else if (target is PrefixedIdentifier) subTarget = target.prefix; |
| 110 | + |
| 111 | + if (_isImplicitDynamic(subTarget)) return; |
| 112 | + |
| 113 | + _reportNodeOrToken(target, token); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + _reportNodeOrToken(AstNode node, token) { |
| 118 | + if (token != null) { |
| 119 | + rule.reportLintForToken(token); |
| 120 | + } else { |
| 121 | + rule.reportLint(node); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + @override |
| 126 | + visitPrefixedIdentifier(PrefixedIdentifier node) { |
| 127 | + if (_isObjectProperty(node.identifier)) return; |
| 128 | + _checkTarget(node.prefix, node.period); |
| 129 | + } |
| 130 | + |
| 131 | + @override |
| 132 | + visitPropertyAccess(PropertyAccess node) { |
| 133 | + if (_isObjectProperty(node.propertyName)) return; |
| 134 | + _checkTarget(node.realTarget, node.operator); |
| 135 | + } |
| 136 | + |
| 137 | + bool _isObjectProperty(SimpleIdentifier node) { |
| 138 | + var name = node.name; |
| 139 | + return name == 'runtimeType' || name == 'hashCode'; |
| 140 | + } |
| 141 | + |
| 142 | + @override |
| 143 | + visitIndexExpression(IndexExpression node) { |
| 144 | + _checkTarget(node.realTarget, node.leftBracket); |
| 145 | + } |
| 146 | + |
| 147 | + @override |
| 148 | + visitAssignmentExpression(AssignmentExpression node) { |
| 149 | + var rhs = node.rightHandSide; |
| 150 | + _checkAssignment(rhs, |
| 151 | + rhs.bestParameterElement ?? _getBestElement(node.leftHandSide)); |
| 152 | + } |
| 153 | + |
| 154 | + @override |
| 155 | + visitMethodInvocation(MethodInvocation node) { |
| 156 | + var methodName = node.methodName; |
| 157 | + _checkMethodInvocation(node.realTarget, methodName.bestElement, methodName.name, node.argumentList.arguments, node.operator); |
| 158 | + } |
| 159 | + |
| 160 | + _checkMethodInvocation(Expression target, ExecutableElement methodElement, String methodName, List<Expression> arguments, token) { |
| 161 | + for (var arg in arguments) { |
| 162 | + _checkAssignment(arg, arg.bestParameterElement); |
| 163 | + } |
| 164 | + |
| 165 | + if (methodElement != null) return; |
| 166 | + |
| 167 | + if (methodName == 'toString' && arguments.isEmpty || |
| 168 | + methodName == 'noSuchMethod' && arguments.size == 1) { |
| 169 | + return; |
| 170 | + } |
| 171 | + _checkTarget(target, token); |
| 172 | + } |
| 173 | + |
| 174 | + @override |
| 175 | + visitBinaryExpression(BinaryExpression node) { |
| 176 | + _checkMethodInvocation(node.leftOperand, node.bestElement, node.operator.toString(), [node.rightOperand], node.operator); |
| 177 | + } |
| 178 | + |
| 179 | + _checkAssignment(Expression arg, Element toElement) { |
| 180 | + if (!_isImplicitDynamic(arg)) return; |
| 181 | + |
| 182 | + if (toElement == null) return; |
| 183 | + |
| 184 | + if (_isDynamicOrObject(toElement.type)) return; |
| 185 | + |
| 186 | + rule.reportLint(arg); |
| 187 | + } |
| 188 | + |
| 189 | + _isDynamicOrObject(DartType t) => t.isDynamic || t.isObject; |
| 190 | + |
| 191 | + @override |
| 192 | + void visitConditionalExpression(ConditionalExpression node) { |
| 193 | + _checkTarget(node.condition); |
| 194 | + } |
| 195 | + |
| 196 | + @override |
| 197 | + visitDeclaredIdentifier(DeclaredIdentifier node) { |
| 198 | + if (node.type == null && node.identifier.bestType.isDynamic && node.element.type.isDynamic) { |
| 199 | + rule.reportLintForToken(node.keyword); |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + |
| 204 | + // bool _isImplicitDynamicVariable(VariableDeclaration node) => |
| 205 | + // node.element.hasImplicitType && |
| 206 | + // node.name.bestType.isDynamic && |
| 207 | + // (node.initializer == null || _isImplicitDynamic(node.initializer)); |
| 208 | + // |
| 209 | + // @override |
| 210 | + // visitVariableDeclarationList(VariableDeclarationList node) { |
| 211 | + // if (node.type == null && node.variables.any(_isImplicitDynamicVariable)) { |
| 212 | + // rule.reportLintForToken(node.keyword); |
| 213 | + // } |
| 214 | + // } |
| 215 | +} |
0 commit comments