| // Copyright (c) 2026, the Dart project authors. Please see the AUTHORS file | |
| // for details. All rights reserved. Use of this source code is governed by a | |
| // BSD-style license that can be found in the LICENSE file. | |
| import 'package:analyzer/analysis_rule/analysis_rule.dart'; | |
| import 'package:analyzer/analysis_rule/rule_context.dart'; | |
| import 'package:analyzer/analysis_rule/rule_handler.dart'; | |
| import 'package:analyzer/analysis_rule/rule_visitor_registry.dart'; | |
| import 'package:analyzer/dart/ast/ast.dart'; | |
| import 'package:analyzer/dart/ast/visitor.dart'; | |
| import 'package:analyzer/error/error.dart'; | |
| import 'package:analyzer/src/lint/analysis_rule_timers.dart'; | |
| import 'package:analyzer/src/lint/linter_visitor.dart'; | |
| import 'package:analyzer/src/test_utilities/lint_registration_mixin.dart'; | |
| import 'package:analyzer_testing/utilities/utilities.dart'; | |
| import 'package:test/test.dart'; | |
| import 'package:test_reflective_loader/test_reflective_loader.dart'; | |
| import '../dart/resolution/context_collection_resolution.dart'; | |
| main() { | |
| defineReflectiveSuite(() { | |
| defineReflectiveTests(RuleHandlerTest); | |
| }); | |
| } | |
| @reflectiveTest | |
| class RuleHandlerTest extends PubPackageResolutionTest | |
| with LintRegistrationMixin { | |
| final List<String> log = []; | |
| Future<void> resolveWithRule( | |
| RuleHandler? Function(List<String> log) createHandler, | |
| String code, { | |
| bool propagateLinterExceptions = true, | |
| bool withVisitor = false, | |
| }) async { | |
| writeTestPackageAnalysisOptionsFile( | |
| analysisOptionsContent( | |
| propagateLinterExceptions: propagateLinterExceptions, | |
| rules: [_LogRule.code.lowerCaseName], | |
| ), | |
| ); | |
| registerLintRule( | |
| _LogRule(() => createHandler(log), visitorLog: withVisitor ? log : null), | |
| ); | |
| await resolveTestCode(code); | |
| } | |
| @override | |
| Future<void> tearDown() { | |
| unregisterLintRules(); | |
| return super.tearDown(); | |
| } | |
| test_category_assignmentTarget_compoundAssignment() async { | |
| await resolveWithRule(_AssignmentTargetHandler.new, r''' | |
| void f(int x) { | |
| x += 1; | |
| } | |
| '''); | |
| expect(log, ['AssignmentTarget x']); | |
| } | |
| test_category_assignmentTarget_directAssignment() async { | |
| await resolveWithRule(_AssignmentTargetHandler.new, r''' | |
| void f(int x) { | |
| x = 0; | |
| } | |
| '''); | |
| expect(log, ['AssignmentTarget x']); | |
| } | |
| test_category_assignmentTarget_forEachPartsWithIdentifier() async { | |
| await resolveWithRule(_AssignmentTargetHandler.new, r''' | |
| void f(int x, List<int> l) { | |
| for (x in l) {} | |
| } | |
| '''); | |
| expect(log, ['AssignmentTarget x']); | |
| } | |
| test_category_assignmentTarget_ifNullAssignment() async { | |
| await resolveWithRule(_AssignmentTargetHandler.new, r''' | |
| void f(int? x) { | |
| x ??= 0; | |
| } | |
| '''); | |
| expect(log, ['AssignmentTarget x']); | |
| } | |
| test_category_assignmentTarget_incrementOrDecrement() async { | |
| await resolveWithRule(_AssignmentTargetHandler.new, r''' | |
| void f(int x) { | |
| x++; | |
| } | |
| '''); | |
| expect(log, ['AssignmentTarget x']); | |
| } | |
| test_category_assignmentTarget_index() async { | |
| await resolveWithRule(_AssignmentTargetHandler.new, r''' | |
| void f(List<int> a) { | |
| a[0] = 0; | |
| } | |
| '''); | |
| expect(log, ['AssignmentTarget []']); | |
| } | |
| test_category_leafFirst_thenMostSpecific_invocation() async { | |
| await resolveWithRule(_InvocationHandler.new, r''' | |
| void f(Object a) { | |
| a.foo(); | |
| } | |
| '''); | |
| expect(log, [ | |
| 'ReceiverMethodInvocation foo', | |
| 'NamedFunctionInvocation foo', | |
| 'FunctionInvocation', | |
| ]); | |
| } | |
| test_category_leafFirst_thenMostSpecific_propertyExtraction() async { | |
| await resolveWithRule(_PropertyExtractionHandler.new, r''' | |
| void f() { | |
| ''.hashCode; | |
| } | |
| '''); | |
| expect(log, [ | |
| 'ReceiverPropertyExtraction hashCode', | |
| 'PropertyExtraction hashCode', | |
| 'NameExpression hashCode', | |
| ]); | |
| } | |
| test_category_otherLeaf() async { | |
| await resolveWithRule(_InvocationHandler.new, r''' | |
| void f(void Function() a) { | |
| a(); | |
| } | |
| '''); | |
| expect(log, ['FunctionInvocation']); | |
| } | |
| test_createHandler_null() async { | |
| await resolveWithRule((_) => null, r''' | |
| void f() {} | |
| '''); | |
| expect(log, isEmpty); | |
| } | |
| test_exception_handlingContinues() async { | |
| await resolveWithRule(_ThrowingHandler.new, r''' | |
| int f(bool b) { | |
| if (b) return 0; | |
| return 1; | |
| } | |
| ''', propagateLinterExceptions: false); | |
| expect(log, ['ReturnStatement', 'ReturnStatement', 'LibraryEnd']); | |
| } | |
| test_exception_propagated_libraryEnd() async { | |
| var rule = _LogRule(() => null); | |
| var registry = RuleVisitorRegistryImpl2(enableTiming: true); | |
| registry.addHandler(rule, _ThrowingHandler(log)); | |
| var visitor = AnalysisRuleVisitor2( | |
| registry, | |
| shouldPropagateExceptions: true, | |
| ); | |
| expect(visitor.afterLibrary, throwsStateError); | |
| expect(log, ['LibraryEnd']); | |
| expect(analysisRuleTimers.getTimer(rule).isRunning, isFalse); | |
| } | |
| test_exception_propagated_node() async { | |
| var result = await resolveTestCode(r''' | |
| int f() { | |
| return 0; | |
| } | |
| '''); | |
| var rule = _LogRule(() => null); | |
| var registry = RuleVisitorRegistryImpl2(enableTiming: true); | |
| registry.addHandler(rule, _ThrowingHandler(log)); | |
| var visitor = AnalysisRuleVisitor2( | |
| registry, | |
| shouldPropagateExceptions: true, | |
| ); | |
| expect(() => result.unit.accept2(visitor), throwsStateError); | |
| expect(log, ['ReturnStatement']); | |
| expect(analysisRuleTimers.getTimer(rule).isRunning, isFalse); | |
| } | |
| test_libraryEnd_afterAllUnits() async { | |
| newFile('$testPackageLibPath/a.dart', r''' | |
| part of 'test.dart'; | |
| '''); | |
| await resolveWithRule(_UnitHandler.new, r''' | |
| part 'a.dart'; | |
| '''); | |
| expect(log, ['CompilationUnit', 'CompilationUnit', 'LibraryEnd']); | |
| } | |
| test_libraryEnd_withoutNodeHandlers() async { | |
| await resolveWithRule(_LibraryEndHandler.new, r''' | |
| int f() { | |
| return 0; | |
| } | |
| '''); | |
| expect(log, ['LibraryEnd']); | |
| } | |
| test_registerNodeProcessors2_beforeHandlers() async { | |
| await resolveWithRule(_ReturnStatementHandler.new, r''' | |
| int f() { | |
| return 0; | |
| } | |
| ''', withVisitor: true); | |
| expect(log, ['visitReturnStatement', 'ReturnStatement']); | |
| } | |
| test_visitor_visitMethodsNotCalled() async { | |
| await resolveWithRule(_WalkingHandler.new, r''' | |
| int f() { | |
| return 0; | |
| } | |
| '''); | |
| expect(log, ['CompilationUnit']); | |
| } | |
| } | |
| class _AssignmentTargetHandler implements AssignmentTargetHandler { | |
| final List<String> log; | |
| new(this.log); | |
| @override | |
| void handleAssignmentTarget(AssignmentTarget node) { | |
| var name = switch (node) { | |
| NamedAssignmentTarget(:var name) => name.lexeme, | |
| _ => '[]', | |
| }; | |
| log.add('AssignmentTarget $name'); | |
| } | |
| } | |
| class _InvocationHandler | |
| implements | |
| FunctionInvocationHandler, | |
| NamedFunctionInvocationHandler, | |
| ReceiverMethodInvocationHandler { | |
| final List<String> log; | |
| new(this.log); | |
| @override | |
| void handleFunctionInvocation(FunctionInvocation node) { | |
| log.add('FunctionInvocation'); | |
| } | |
| @override | |
| void handleNamedFunctionInvocation(NamedFunctionInvocation node) { | |
| log.add('NamedFunctionInvocation ${node.name.lexeme}'); | |
| } | |
| @override | |
| void handleReceiverMethodInvocation(ReceiverMethodInvocation node) { | |
| log.add('ReceiverMethodInvocation ${node.name.lexeme}'); | |
| } | |
| } | |
| class _LibraryEndHandler implements LibraryEndHandler { | |
| final List<String> log; | |
| new(this.log); | |
| @override | |
| void handleLibraryEnd() { | |
| log.add('LibraryEnd'); | |
| } | |
| } | |
| class _LogRule extends AnalysisRule { | |
| static const LintCode code = LintCode( | |
| 'log_rule', | |
| 'Log rule.', | |
| uniqueName: 'log_rule', | |
| ); | |
| final RuleHandler? Function() _createHandler; | |
| /// If not `null`, the rule also registers a visitor that logs here. | |
| final List<String>? visitorLog; | |
| new(this._createHandler, {this.visitorLog}) | |
| : super(name: code.lowerCaseName, description: ''); | |
| @override | |
| DiagnosticCode get diagnosticCode => code; | |
| @override | |
| RuleHandler? createHandler(RuleContext context) => _createHandler(); | |
| @override | |
| void registerNodeProcessors2( | |
| RuleVisitorRegistry2 registry, | |
| RuleContext context, | |
| ) { | |
| if (visitorLog case var visitorLog?) { | |
| registry.addReturnStatement(this, _ReturnStatementVisitor(visitorLog)); | |
| } | |
| } | |
| } | |
| class _PropertyExtractionHandler | |
| implements | |
| NameExpressionHandler, | |
| PropertyExtractionHandler, | |
| ReceiverPropertyExtractionHandler { | |
| final List<String> log; | |
| new(this.log); | |
| @override | |
| void handleNameExpression(NameExpression node) { | |
| log.add('NameExpression ${node.name.lexeme}'); | |
| } | |
| @override | |
| void handlePropertyExtraction(PropertyExtraction node) { | |
| log.add('PropertyExtraction ${node.name.lexeme}'); | |
| } | |
| @override | |
| void handleReceiverPropertyExtraction(ReceiverPropertyExtraction node) { | |
| log.add('ReceiverPropertyExtraction ${node.name.lexeme}'); | |
| } | |
| } | |
| class _ReturnStatementHandler implements ReturnStatementHandler { | |
| final List<String> log; | |
| new(this.log); | |
| @override | |
| void handleReturnStatement(ReturnStatement node) { | |
| log.add('ReturnStatement'); | |
| } | |
| } | |
| class _ReturnStatementVisitor extends SimpleAstVisitor2<void> { | |
| final List<String> log; | |
| new(this.log); | |
| @override | |
| void visitReturnStatement(ReturnStatement node) { | |
| log.add('visitReturnStatement'); | |
| } | |
| } | |
| class _ThrowingHandler implements LibraryEndHandler, ReturnStatementHandler { | |
| final List<String> log; | |
| new(this.log); | |
| @override | |
| void handleLibraryEnd() { | |
| log.add('LibraryEnd'); | |
| throw StateError('LibraryEnd'); | |
| } | |
| @override | |
| void handleReturnStatement(ReturnStatement node) { | |
| log.add('ReturnStatement'); | |
| throw StateError('ReturnStatement'); | |
| } | |
| } | |
| class _UnitHandler implements CompilationUnitHandler, LibraryEndHandler { | |
| final List<String> log; | |
| new(this.log); | |
| @override | |
| void handleCompilationUnit(CompilationUnit node) { | |
| log.add('CompilationUnit'); | |
| } | |
| @override | |
| void handleLibraryEnd() { | |
| log.add('LibraryEnd'); | |
| } | |
| } | |
| class _WalkingHandler extends RecursiveAstVisitor2<void> | |
| implements CompilationUnitHandler { | |
| final List<String> log; | |
| new(this.log); | |
| @override | |
| void handleCompilationUnit(CompilationUnit node) { | |
| log.add('CompilationUnit'); | |
| } | |
| @override | |
| void visitCompilationUnit(CompilationUnit node) { | |
| log.add('visitCompilationUnit'); | |
| } | |
| @override | |
| void visitReturnStatement(ReturnStatement node) { | |
| log.add('visitReturnStatement'); | |
| } | |
| } |