From 00a3cdff40833b10a1bc00a639f3d653ffe2b5f4 Mon Sep 17 00:00:00 2001 From: Konstantin Shcheglov Date: Mon, 11 Nov 2024 23:39:29 +0000 Subject: [PATCH] Elements. Remove FragmentDeclaration, add 'declaredFragment' to Declaration. Change-Id: I33398358d1241114b04995767d63188bb314fd6a Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/394565 Reviewed-by: Brian Wilkerson Commit-Queue: Konstantin Shcheglov --- .../handlers/custom/handler_augmentation.dart | 4 +- .../handlers/custom/handler_augmented.dart | 4 +- .../correction/dart/remove_unused.dart | 2 +- .../dart/rename_method_parameter.dart | 2 +- pkg/analyzer/lib/src/dart/ast/ast.dart | 65 +++++++++---------- .../lib/src/rules/avoid_returning_this.dart | 2 +- .../lib/src/rules/unreachable_from_main.dart | 14 ++-- 7 files changed, 41 insertions(+), 52 deletions(-) diff --git a/pkg/analysis_server/lib/src/lsp/handlers/custom/handler_augmentation.dart b/pkg/analysis_server/lib/src/lsp/handlers/custom/handler_augmentation.dart index 7c68e7f7551..6e22becf5dd 100644 --- a/pkg/analysis_server/lib/src/lsp/handlers/custom/handler_augmentation.dart +++ b/pkg/analysis_server/lib/src/lsp/handlers/custom/handler_augmentation.dart @@ -7,7 +7,7 @@ import 'package:analysis_server/src/lsp/constants.dart'; import 'package:analysis_server/src/lsp/error_or.dart'; import 'package:analysis_server/src/lsp/handlers/handlers.dart'; import 'package:analysis_server/src/lsp/mapping.dart'; -import 'package:analyzer/src/dart/ast/ast.dart'; +import 'package:analyzer/src/dart/ast/ast.dart' as ast; import 'package:analyzer/src/utilities/extensions/ast.dart'; class AugmentationHandler @@ -44,7 +44,7 @@ class AugmentationHandler var node = unit.unit .nodeCovering(offset: offset) - ?.thisOrAncestorOfType(); + ?.thisOrAncestorOfType(); var location = fragmentToLocation( uriConverter, diff --git a/pkg/analysis_server/lib/src/lsp/handlers/custom/handler_augmented.dart b/pkg/analysis_server/lib/src/lsp/handlers/custom/handler_augmented.dart index 45631c09901..daf19dd0cba 100644 --- a/pkg/analysis_server/lib/src/lsp/handlers/custom/handler_augmented.dart +++ b/pkg/analysis_server/lib/src/lsp/handlers/custom/handler_augmented.dart @@ -7,7 +7,7 @@ import 'package:analysis_server/src/lsp/constants.dart'; import 'package:analysis_server/src/lsp/error_or.dart'; import 'package:analysis_server/src/lsp/handlers/handlers.dart'; import 'package:analysis_server/src/lsp/mapping.dart'; -import 'package:analyzer/src/dart/ast/ast.dart'; +import 'package:analyzer/src/dart/ast/ast.dart' as ast; import 'package:analyzer/src/utilities/extensions/ast.dart'; class AugmentedHandler @@ -44,7 +44,7 @@ class AugmentedHandler var node = unit.unit .nodeCovering(offset: offset) - ?.thisOrAncestorOfType(); + ?.thisOrAncestorOfType(); var location = fragmentToLocation( uriConverter, diff --git a/pkg/analysis_server/lib/src/services/correction/dart/remove_unused.dart b/pkg/analysis_server/lib/src/services/correction/dart/remove_unused.dart index e755492bfbb..5bc7a8af19e 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/remove_unused.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/remove_unused.dart @@ -38,7 +38,7 @@ class RemoveUnusedElement extends _RemoveUnused { } Element2? element; - if (node is FragmentDeclaration) { + if (node is Declaration) { element = node.declaredFragment?.element; } if (element == null) { diff --git a/pkg/analysis_server/lib/src/services/correction/dart/rename_method_parameter.dart b/pkg/analysis_server/lib/src/services/correction/dart/rename_method_parameter.dart index 69c82fc68d7..aa4b5655727 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/rename_method_parameter.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/rename_method_parameter.dart @@ -45,7 +45,7 @@ class RenameMethodParameter extends ResolvedCorrectionProducer { if (methodParameters == null) return; Fragment? declaredFragment; - if (method.parent case FragmentDeclaration declaration) { + if (method.parent case Declaration declaration) { declaredFragment = declaration.declaredFragment; } diff --git a/pkg/analyzer/lib/src/dart/ast/ast.dart b/pkg/analyzer/lib/src/dart/ast/ast.dart index 74c06d84c5d..86a1e1b7185 100644 --- a/pkg/analyzer/lib/src/dart/ast/ast.dart +++ b/pkg/analyzer/lib/src/dart/ast/ast.dart @@ -2684,8 +2684,7 @@ class ChildEntity { /// classModifiers ::= 'sealed' /// | 'abstract'? ('base' | 'interface' | 'final')? /// | 'abstract'? 'base'? 'mixin' -abstract final class ClassDeclaration - implements NamedCompilationUnitMember, FragmentDeclaration { +abstract final class ClassDeclaration implements NamedCompilationUnitMember { /// The `abstract` keyword, or `null` if the keyword was absent. Token? get abstractKeyword; @@ -2921,7 +2920,7 @@ sealed class ClassMemberImpl extends DeclarationImpl implements ClassMember { /// /// mixinApplication ::= /// [NamedType] [WithClause] [ImplementsClause]? ';' -abstract final class ClassTypeAlias implements TypeAlias, FragmentDeclaration { +abstract final class ClassTypeAlias implements TypeAlias { /// The token for the `abstract` keyword, or `null` if this isn't defining an /// abstract class. Token? get abstractKeyword; @@ -4062,8 +4061,7 @@ final class ConstantPatternImpl extends DartPatternImpl /// /// initializerList ::= /// ':' [ConstructorInitializer] (',' [ConstructorInitializer])* -abstract final class ConstructorDeclaration - implements ClassMember, FragmentDeclaration { +abstract final class ConstructorDeclaration implements ClassMember { /// The `augment` keyword, or `null` if the keyword was absent. Token? get augmentKeyword; @@ -4797,6 +4795,12 @@ abstract final class Declaration implements AnnotatedNode { /// node corresponds to a list of declarations or if the AST structure hasn't /// been resolved. Element? get declaredElement; + + /// The fragment declared by this declaration. + /// + /// Returns `null` if the AST structure hasn't been resolved. + @experimental + Fragment? get declaredFragment; } sealed class DeclarationImpl extends AnnotatedNodeImpl implements Declaration { @@ -4859,6 +4863,9 @@ final class DeclaredIdentifierImpl extends DeclarationImpl @override LocalVariableElementImpl? declaredElement; + @override + LocalVariableElementImpl? declaredFragment; + /// Initializes a newly created formal parameter. /// /// Either or both of the [comment] and [metadata] can be `null` if the @@ -5539,7 +5546,7 @@ final class EnumConstantArgumentsImpl extends AstNodeImpl } /// The declaration of an enum constant. -abstract final class EnumConstantDeclaration implements FragmentDeclaration { +abstract final class EnumConstantDeclaration implements Declaration { /// The explicit arguments (there are always implicit `index` and `name` /// leading arguments) to the invoked constructor, or `null` if this constant /// doesn't provide any explicit arguments. @@ -5641,8 +5648,7 @@ final class EnumConstantDeclarationImpl extends DeclarationImpl /// metadata 'enum' name [TypeParameterList]? /// [WithClause]? [ImplementsClause]? '{' [SimpleIdentifier] /// (',' [SimpleIdentifier])* (';' [ClassMember]+)? '}' -abstract final class EnumDeclaration - implements NamedCompilationUnitMember, FragmentDeclaration { +abstract final class EnumDeclaration implements NamedCompilationUnitMember { /// The `augment` keyword, or `null` if the keyword was absent. @experimental Token? get augmentKeyword; @@ -6364,8 +6370,7 @@ final class ExtendsClauseImpl extends AstNodeImpl implements ExtendsClause { /// 'extension' [SimpleIdentifier]? [TypeParameterList]? /// 'on' [TypeAnnotation] [ShowClause]? [HideClause]? /// '{' [ClassMember]* '}' -abstract final class ExtensionDeclaration - implements CompilationUnitMember, FragmentDeclaration { +abstract final class ExtensionDeclaration implements CompilationUnitMember { /// The `augment` keyword, or `null` if the keyword was absent. @experimental Token? get augmentKeyword; @@ -6694,7 +6699,7 @@ final class ExtensionOverrideImpl extends ExpressionImpl /// '}' @experimental abstract final class ExtensionTypeDeclaration - implements NamedCompilationUnitMember, FragmentDeclaration { + implements NamedCompilationUnitMember { /// The `augment` keyword, or `null` if the keyword was absent. @experimental Token? get augmentKeyword; @@ -6923,6 +6928,9 @@ final class FieldDeclarationImpl extends ClassMemberImpl @override Element? get declaredElement => null; + @override + Fragment? get declaredFragment => null; + @override Token get endToken => semicolon; @@ -8073,18 +8081,6 @@ final class ForStatementImpl extends StatementImpl } } -/// A declaration of a fragment of an element. -@experimental -abstract final class FragmentDeclaration implements Declaration { - // TODO(pq): move `declaredFragment` into `Declaration` and remove this class. - - /// The fragment declared by this declaration. - /// - /// Returns `null` if the AST structure hasn't been resolved. - @experimental - Fragment? get declaredFragment; -} - /// A node representing the body of a function or method. /// /// functionBody ::= @@ -8203,8 +8199,7 @@ sealed class FunctionBodyImpl extends AstNodeImpl implements FunctionBody { // augmented and declarations that can't be augmented. This results in getters // that are only sometimes applicable. Consider changing the class hierarchy so // that these two kinds of variables can be distinguished. -abstract final class FunctionDeclaration - implements NamedCompilationUnitMember, FragmentDeclaration { +abstract final class FunctionDeclaration implements NamedCompilationUnitMember { /// The `augment` keyword, or `null` if there is no `augment` keyword. @experimental Token? get augmentKeyword; @@ -8742,8 +8737,7 @@ final class FunctionReferenceImpl extends CommentReferableExpressionImpl /// /// functionPrefix ::= /// [TypeAnnotation]? [SimpleIdentifier] -abstract final class FunctionTypeAlias - implements TypeAlias, FragmentDeclaration { +abstract final class FunctionTypeAlias implements TypeAlias { @override TypeAliasElement? get declaredElement; @@ -9104,8 +9098,7 @@ final class GenericFunctionTypeImpl extends TypeAnnotationImpl /// functionTypeAlias ::= /// 'typedef' [SimpleIdentifier] [TypeParameterList]? = /// [FunctionType] ';' -abstract final class GenericTypeAlias - implements TypeAlias, FragmentDeclaration { +abstract final class GenericTypeAlias implements TypeAlias { /// The equal sign separating the name being defined from the function type. Token get equals; @@ -11798,8 +11791,7 @@ final class MapPatternImpl extends DartPatternImpl implements MapPattern { /// Prior to the 'extension-methods' experiment, these nodes were always /// children of a class declaration. When the experiment is enabled, these nodes /// can also be children of an extension declaration. -abstract final class MethodDeclaration - implements ClassMember, FragmentDeclaration { +abstract final class MethodDeclaration implements ClassMember { /// The token for the `augment` keyword. Token? get augmentKeyword; @@ -12195,8 +12187,7 @@ abstract final class MethodReferenceExpression implements Expression { /// mixinDeclaration ::= /// 'base'? 'mixin' name [TypeParameterList]? /// [OnClause]? [ImplementsClause]? '{' [ClassMember]* '}' -abstract final class MixinDeclaration - implements NamedCompilationUnitMember, FragmentDeclaration { +abstract final class MixinDeclaration implements NamedCompilationUnitMember { /// The `augment` keyword, or `null` if the keyword was absent. Token? get augmentKeyword; @@ -17652,6 +17643,9 @@ final class TopLevelVariableDeclarationImpl extends CompilationUnitMemberImpl @override Element? get declaredElement => null; + @override + Fragment? get declaredFragment => null; + @override Token get endToken => semicolon; @@ -18052,7 +18046,7 @@ final class TypeLiteralImpl extends CommentReferableExpressionImpl /// /// typeParameter ::= /// name ('extends' [TypeAnnotation])? -abstract final class TypeParameter implements Declaration, FragmentDeclaration { +abstract final class TypeParameter implements Declaration { /// The upper bound for legal arguments, or `null` if there's no explicit /// upper bound. TypeAnnotation? get bound; @@ -18302,8 +18296,7 @@ class UriValidationCode { // augmented and declarations that can't be augmented. This results in getters // that are only sometimes applicable. Consider changing the class hierarchy so // that these two kinds of variables can be distinguished. -abstract final class VariableDeclaration - implements Declaration, FragmentDeclaration { +abstract final class VariableDeclaration implements Declaration { /// The element declared by this declaration. /// /// Returns `null` if the AST structure hasn't been resolved or if this node diff --git a/pkg/linter/lib/src/rules/avoid_returning_this.dart b/pkg/linter/lib/src/rules/avoid_returning_this.dart index ef77d4d2dbf..830112ba097 100644 --- a/pkg/linter/lib/src/rules/avoid_returning_this.dart +++ b/pkg/linter/lib/src/rules/avoid_returning_this.dart @@ -83,7 +83,7 @@ class _Visitor extends SimpleAstVisitor { if (returnType is InterfaceType && returnType.element3 == // ignore: cast_nullable_to_non_nullable - (parent as FragmentDeclaration).declaredFragment?.element) { + (parent as Declaration).declaredFragment?.element) { } else { return; } diff --git a/pkg/linter/lib/src/rules/unreachable_from_main.dart b/pkg/linter/lib/src/rules/unreachable_from_main.dart index af153ee8cdf..2f9a481633e 100644 --- a/pkg/linter/lib/src/rules/unreachable_from_main.dart +++ b/pkg/linter/lib/src/rules/unreachable_from_main.dart @@ -53,10 +53,8 @@ class _DeclarationGatherer { if (declaration is TopLevelVariableDeclaration) { declarations.addAll(declaration.variables.variables); } else { - if (declaration is! FragmentDeclaration) continue; declarations.add(declaration); - var declaredElement = - (declaration as FragmentDeclaration).declaredFragment?.element; + var declaredElement = declaration.declaredFragment?.element; if (declaredElement == null || declaredElement.isPrivate) { continue; } @@ -142,7 +140,7 @@ class _DeclarationGatherer { /// "References" are most often [SimpleIdentifier]s, but can also be other /// nodes which refer to a declaration. class _ReferenceVisitor extends RecursiveAstVisitor { - Map declarationMap; + Map declarationMap; Set declarations = {}; @@ -463,10 +461,9 @@ class _Visitor extends SimpleAstVisitor { if (entryPoints.isEmpty) return; // Map each top-level and static element to its declaration. - var declarationByElement = {}; + var declarationByElement = {}; for (var declaration in declarations) { - var element = - (declaration as FragmentDeclaration).declaredFragment?.element; + var element = declaration.declaredFragment?.element; if (element != null) { declarationByElement[element] = declaration; if (element is TopLevelVariableElement2) { @@ -516,8 +513,7 @@ class _Visitor extends SimpleAstVisitor { var unitDeclarations = unitDeclarationGatherer.declarations; var unusedDeclarations = unitDeclarations.difference(usedMembers); var unusedMembers = unusedDeclarations.where((declaration) { - var element = - (declaration as FragmentDeclaration).declaredFragment?.element; + var element = declaration.declaredFragment?.element; return element != null && element.isPublic && !element.hasVisibleForTesting;