|
| 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/analysis_rule/analysis_rule.dart'; |
| 6 | +import 'package:analyzer/analysis_rule/rule_context.dart'; |
| 7 | +import 'package:analyzer/analysis_rule/rule_visitor_registry.dart'; |
| 8 | +import 'package:analyzer/dart/ast/ast.dart'; |
| 9 | +import 'package:analyzer/dart/ast/visitor.dart'; |
| 10 | +import 'package:analyzer/dart/element/type_system.dart'; |
| 11 | +import 'package:analyzer/error/error.dart'; |
| 12 | + |
| 13 | +import '../utilities.dart'; |
| 14 | + |
| 15 | +class UseIsEmptyMatcherRule extends MultiAnalysisRule { |
| 16 | + static const LintCode useIsEmptyMatcherCode = LintCode( |
| 17 | + 'use_is_empty_matcher', |
| 18 | + "Use the 'isEmpty' matcher.", |
| 19 | + ); |
| 20 | + |
| 21 | + static const LintCode useIsNotEmptyMatcherCode = LintCode( |
| 22 | + 'use_is_not_empty_matcher', |
| 23 | + "Use the 'isNotEmpty' matcher.", |
| 24 | + ); |
| 25 | + |
| 26 | + UseIsEmptyMatcherRule() |
| 27 | + : super( |
| 28 | + name: 'use_is_empty_matcher', |
| 29 | + description: "Use the built-in 'isEmpty' and 'isNotEmpty' matchers.", |
| 30 | + ); |
| 31 | + |
| 32 | + @override |
| 33 | + List<LintCode> get diagnosticCodes => [ |
| 34 | + useIsEmptyMatcherCode, |
| 35 | + useIsNotEmptyMatcherCode, |
| 36 | + ]; |
| 37 | + |
| 38 | + @override |
| 39 | + void registerNodeProcessors( |
| 40 | + RuleVisitorRegistry registry, |
| 41 | + RuleContext context, |
| 42 | + ) { |
| 43 | + var visitor = _Visitor(this, context.typeSystem); |
| 44 | + registry.addMethodInvocation(this, visitor); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +class _Visitor extends SimpleAstVisitor<void> { |
| 49 | + final MultiAnalysisRule rule; |
| 50 | + |
| 51 | + final TypeSystem typeSystem; |
| 52 | + |
| 53 | + _Visitor(this.rule, this.typeSystem); |
| 54 | + |
| 55 | + @override |
| 56 | + void visitMethodInvocation(MethodInvocation node) { |
| 57 | + if (!node.methodName.isExpect) return; |
| 58 | + |
| 59 | + var arguments = node.argumentList.arguments; |
| 60 | + if (arguments.isEmpty || arguments.length > 2) return; |
| 61 | + var [actual, matcher] = arguments; |
| 62 | + |
| 63 | + bool actualIsIsEmpty; |
| 64 | + if (actual is PrefixedIdentifier) { |
| 65 | + if (actual.identifier.name == 'isEmpty') { |
| 66 | + actualIsIsEmpty = true; |
| 67 | + } else if (actual.identifier.name == 'isNotEmpty') { |
| 68 | + actualIsIsEmpty = false; |
| 69 | + } else { |
| 70 | + return; |
| 71 | + } |
| 72 | + } else if (actual is PropertyAccess) { |
| 73 | + if (actual.propertyName.name == 'isEmpty') { |
| 74 | + actualIsIsEmpty = true; |
| 75 | + } else if (actual.propertyName.name == 'isNotEmpty') { |
| 76 | + actualIsIsEmpty = false; |
| 77 | + } else { |
| 78 | + return; |
| 79 | + } |
| 80 | + } else { |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + bool matcherValue; |
| 85 | + if (matcher is BooleanLiteral) { |
| 86 | + matcherValue = matcher.value; |
| 87 | + } else if (matcher is SimpleIdentifier && matcher.isIsFalse) { |
| 88 | + matcherValue = false; |
| 89 | + } else if (matcher is SimpleIdentifier && matcher.isIsTrue) { |
| 90 | + matcherValue = true; |
| 91 | + } else { |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + if (actualIsIsEmpty == matcherValue) { |
| 96 | + // Either `expect(a.isEmpty, isTrue|true)` or |
| 97 | + // `expect(a.isNotEmpty, isFalse|false)`. |
| 98 | + rule.reportAtNode( |
| 99 | + matcher, |
| 100 | + diagnosticCode: UseIsEmptyMatcherRule.useIsEmptyMatcherCode, |
| 101 | + ); |
| 102 | + } else { |
| 103 | + // Either `expect(a.isEmpty, isFalse|false)` or |
| 104 | + // `expect(a.isNotEmpty, isTrue|true)`. |
| 105 | + rule.reportAtNode( |
| 106 | + matcher, |
| 107 | + diagnosticCode: UseIsEmptyMatcherRule.useIsNotEmptyMatcherCode, |
| 108 | + ); |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments