From fb3574e8ae9ad7344524d48acbcf5300b257e8c2 Mon Sep 17 00:00:00 2001 From: Ruben Lopez Date: Mon, 26 Aug 2024 20:21:21 -0400 Subject: [PATCH] Skip named types used in Widget State class declarations Added a check in visitNamedType to skip named types if they are used in Widget State class declarations. This prevents these specific named types from being recorded as used. --- .../unused_code_analyzer/used_code_visitor.dart | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/src/analyzers/unused_code_analyzer/used_code_visitor.dart b/lib/src/analyzers/unused_code_analyzer/used_code_visitor.dart index 0138a1493..a3e0fc092 100644 --- a/lib/src/analyzers/unused_code_analyzer/used_code_visitor.dart +++ b/lib/src/analyzers/unused_code_analyzer/used_code_visitor.dart @@ -97,6 +97,9 @@ class UsedCodeVisitor extends RecursiveAstVisitor { @override void visitNamedType(NamedType node) { + if (_isUsedInWidgetStateClassDeclaration(node)) { + return; + } _recordPrefixedElement(node.importPrefix, node.element); super.visitNamedType(node); } @@ -244,4 +247,14 @@ class UsedCodeVisitor extends RecursiveAstVisitor { return grandGrandParent is NamedType && isWidgetStateOrSubclass(grandGrandParent.type); } + + bool _isUsedInWidgetStateClassDeclaration(NamedType namedType) { + final parent = namedType.parent; + if (parent is TypeArgumentList) { + final grandParent = parent.parent; + return grandParent is NamedType && + isWidgetStateOrSubclass(grandParent.type); + } + return false; + } }