blob: 3560ad120a7d4d5268af16ccaece36323b46eb8a [file]
// Copyright (c) 2022, 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_visitor_registry.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/error/error.dart';
import 'package:linter/src/diagnostic.dart' as diag;
class VisitRegisteredNodes extends AnalysisRule {
static const DiagnosticCode code = diag.visitRegisteredNodes;
VisitRegisteredNodes()
: super(
name: 'visit_registered_nodes',
description: "Declare 'visit' methods for all registered node types.",
);
@override
DiagnosticCode get diagnosticCode => code;
@override
void registerNodeProcessors(
RuleVisitorRegistry registry,
RuleContext context,
) {
var visitor = _Visitor(this);
registry.addMethodDeclaration(this, visitor);
}
}
class _BodyVisitor extends RecursiveAstVisitor<void> {
final AnalysisRule rule;
_BodyVisitor(this.rule);
bool implements(ClassElement visitor, String methodName) {
var member = visitor.lookUpConcreteMethod(methodName, visitor.library);
// In general lint visitors should only inherit from [SimpleAstVisitor]s
// (and the method implementations inherited from there are only stubs).
// (We might consider enforcing this since it's harder to ensure that
// Unifying and Generalizing visitors are doing the right thing.)
// For now we flag methods inherited from SimpleAstVisitor since they
// surely don't do anything.
return member?.enclosingElement?.name != 'SimpleAstVisitor';
}
@override
void visitMethodInvocation(MethodInvocation node) {
var targetType = node.target?.staticType;
if (targetType is! InterfaceType) return;
if (targetType.element.name != 'RuleVisitorRegistry') return;
var methodName = node.methodName.name;
if (!methodName.startsWith('add')) return;
var nodeType = methodName.substring(3);
var args = node.argumentList.arguments;
var argType = args[1].argumentExpression.staticType;
if (argType is! InterfaceType) return;
var visitor = argType.element;
if (visitor is! ClassElement) return;
if (implements(visitor, 'visit$nodeType')) return;
rule.reportAtNode(node.methodName);
}
}
class _Visitor extends SimpleAstVisitor<void> {
final AnalysisRule rule;
_Visitor(this.rule);
@override
void visitMethodDeclaration(MethodDeclaration node) {
if (node.name.lexeme == 'registerNodeProcessors') {
node.body.accept(_BodyVisitor(rule));
}
}
}