|
| 1 | +// Copyright (c) 2025, 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 | +import 'package:analyzer/dart/analysis/features.dart'; |
| 6 | +import 'package:analyzer/dart/ast/ast.dart'; |
| 7 | +import 'package:analyzer/dart/ast/token.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:analyzer/error/error.dart'; |
| 12 | + |
| 13 | +import '../analyzer.dart'; |
| 14 | +import '../extensions.dart'; |
| 15 | + |
| 16 | +const _desc = "Avoid switch statements on a 'Type'."; |
| 17 | + |
| 18 | +const _objectToStringName = 'toString'; |
| 19 | + |
| 20 | +class SwitchOnType extends LintRule { |
| 21 | + SwitchOnType() : super(name: LintNames.switch_on_type, description: _desc); |
| 22 | + |
| 23 | + @override |
| 24 | + DiagnosticCode get diagnosticCode => LinterLintCode.switch_on_type; |
| 25 | + |
| 26 | + @override |
| 27 | + LintCode get lintCode => LinterLintCode.switch_on_type; |
| 28 | + |
| 29 | + @override |
| 30 | + void registerNodeProcessors( |
| 31 | + NodeLintRegistry registry, |
| 32 | + LinterContext context, |
| 33 | + ) { |
| 34 | + if (!context.isEnabled(Feature.patterns)) return; |
| 35 | + var visitor = _Visitor(this, context); |
| 36 | + registry.addSwitchExpression(this, visitor); |
| 37 | + registry.addSwitchStatement(this, visitor); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +class _Visitor extends SimpleAstVisitor<void> { |
| 42 | + final LintRule rule; |
| 43 | + |
| 44 | + final LinterContext context; |
| 45 | + |
| 46 | + /// The node where the lint will be reported. |
| 47 | + late AstNode node; |
| 48 | + |
| 49 | + _Visitor(this.rule, this.context); |
| 50 | + |
| 51 | + /// A reference to the [Type] type. |
| 52 | + /// |
| 53 | + /// This is used to check if the type of the expression is assignable to |
| 54 | + /// [Type]. |
| 55 | + /// |
| 56 | + /// This shortens the code and avoids multiple calls to |
| 57 | + /// `context.typeProvider.typeType`. |
| 58 | + InterfaceType get _typeType => context.typeProvider.typeType; |
| 59 | + |
| 60 | + @override |
| 61 | + void visitSwitchExpression(SwitchExpression node) { |
| 62 | + this.node = node.expression; |
| 63 | + _processExpression(node.expression); |
| 64 | + } |
| 65 | + |
| 66 | + @override |
| 67 | + void visitSwitchStatement(SwitchStatement node) { |
| 68 | + this.node = node.expression; |
| 69 | + _processExpression(node.expression); |
| 70 | + } |
| 71 | + |
| 72 | + /// Returns `true` if the [type] is assignable to [Type]. |
| 73 | + bool _isAssignableToType(DartType? type) { |
| 74 | + if (type == null) return false; |
| 75 | + return context.typeSystem.isAssignableTo(type, _typeType); |
| 76 | + } |
| 77 | + |
| 78 | + /// Processes the [expression] of a [SwitchStatement] or [SwitchExpression]. |
| 79 | + /// |
| 80 | + /// Returns `true` if the lint was reportred and `false` otherwise. |
| 81 | + bool _processExpression(Expression expression) { |
| 82 | + if (expression case StringInterpolation(:var elements)) { |
| 83 | + return _processInterpolation(elements); |
| 84 | + } |
| 85 | + if (expression case ConditionalExpression( |
| 86 | + :var thenExpression, |
| 87 | + :var elseExpression, |
| 88 | + )) { |
| 89 | + return _processExpression(thenExpression) || |
| 90 | + _processExpression(elseExpression); |
| 91 | + } |
| 92 | + if (expression case SwitchExpression(:var cases)) { |
| 93 | + for (var caseClause in cases) { |
| 94 | + if (_processExpression(caseClause.expression)) { |
| 95 | + return true; |
| 96 | + } |
| 97 | + } |
| 98 | + return false; |
| 99 | + } |
| 100 | + if (expression case BinaryExpression( |
| 101 | + :var leftOperand, |
| 102 | + :var rightOperand, |
| 103 | + :var operator, |
| 104 | + ) when operator.lexeme == TokenType.PLUS.lexeme) { |
| 105 | + return _processExpression(leftOperand) || |
| 106 | + _processExpression(rightOperand); |
| 107 | + } |
| 108 | + var type = switch (expression) { |
| 109 | + PrefixedIdentifier(:var identifier) => identifier.staticType, |
| 110 | + PropertyAccess(:var propertyName) => propertyName.staticType, |
| 111 | + SimpleIdentifier(:var staticType) => staticType, |
| 112 | + MethodInvocation(:var methodName, :var realTarget?) => |
| 113 | + methodName.element.isToStringMethod ? realTarget.staticType : null, |
| 114 | + _ => null, |
| 115 | + }; |
| 116 | + return _reportIfAssignableToType(type); |
| 117 | + } |
| 118 | + |
| 119 | + /// Processes the [elements] of an [InterpolationExpression]. |
| 120 | + /// |
| 121 | + /// Returns `true` if the lint was reported and `false` otherwise. |
| 122 | + bool _processInterpolation(NodeList<InterpolationElement> elements) { |
| 123 | + for (var element in elements) { |
| 124 | + switch (element) { |
| 125 | + case InterpolationExpression(:var expression): |
| 126 | + var reported = _processExpression(expression); |
| 127 | + |
| 128 | + // This return is necessary to avoid multiple reporting of the lint |
| 129 | + if (reported) { |
| 130 | + return true; |
| 131 | + } |
| 132 | + case InterpolationString(): |
| 133 | + break; |
| 134 | + } |
| 135 | + } |
| 136 | + return false; |
| 137 | + } |
| 138 | + |
| 139 | + /// Reports the lint if the [type] is assignable to [Type]. |
| 140 | + /// |
| 141 | + /// Returns `true` if the lint was reported and `false` otherwise. |
| 142 | + bool _reportIfAssignableToType(DartType? type) { |
| 143 | + var reported = false; |
| 144 | + if (_isAssignableToType(type)) { |
| 145 | + rule.reportAtNode(node); |
| 146 | + reported = true; |
| 147 | + } |
| 148 | + return reported; |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +extension on Element? { |
| 153 | + /// Returns `true` if this element is the `toString` method. |
| 154 | + bool get isToStringMethod { |
| 155 | + var self = this; |
| 156 | + return self is MethodElement && self.name3 == _objectToStringName; |
| 157 | + } |
| 158 | +} |
0 commit comments