Skip to content

Commit

Permalink
Elements. Migrate lib/src/dart/ast/element_locator.dart
Browse files Browse the repository at this point in the history
Change-Id: I7b57a239cd21737292224d05b38b86a0dbd4643c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/403642
Reviewed-by: Brian Wilkerson <[email protected]>
Commit-Queue: Konstantin Shcheglov <[email protected]>
  • Loading branch information
scheglov authored and Commit Queue committed Jan 8, 2025
1 parent 9452875 commit 810eeb4
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 863 deletions.
14 changes: 13 additions & 1 deletion pkg/analysis_server/lib/src/analysis_server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,19 @@ abstract class AnalysisServer {
if (node is StringLiteral && node.parent is UriBasedDirective) {
return null;
}
var element = ElementLocator.locate(node);

Element? element;
switch (node) {
case ExportDirective():
element = node.element;
case ImportDirective():
element = node.element;
case PartOfDirective():
element = node.element;
default:
element = ElementLocator.locate2(node).asElement;
}

if (node is SimpleIdentifier && element is PrefixElement) {
element = getImportElement(node);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/src/dart/ast/element_locator.dart';
import 'package:analyzer/src/utilities/extensions/element.dart';
import 'package:analyzer_plugin/protocol/protocol_common.dart' hide Element;
import 'package:test/test.dart';

Expand Down Expand Up @@ -41,11 +42,24 @@ class RenameRefactoringTest extends RefactoringTest {
/// Creates a new [RenameRefactoring] in [refactoring] for the [Element] of
/// the [SimpleIdentifier] at the given [search] pattern.
void createRenameRefactoringAtString(String search) {
var identifier = findNode.any(search);
var element = ElementLocator.locate(identifier);
if (identifier is SimpleIdentifier && element is PrefixElement) {
element = getImportElement(identifier);
var node = findNode.any(search);

Element? element;
switch (node) {
case ExportDirective():
element = node.element;
case ImportDirective():
element = node.element;
case PartOfDirective():
element = node.element;
default:
element = ElementLocator.locate2(node).asElement;
}

if (node is SimpleIdentifier && element is PrefixElement) {
element = getImportElement(node);
}

createRenameRefactoringForElement(element);
}

Expand Down
1 change: 0 additions & 1 deletion pkg/analyzer/analyzer_use_new_elements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ lib/src/dart/analysis/search.dart
lib/src/dart/analysis/session.dart
lib/src/dart/analysis/session_helper.dart
lib/src/dart/ast/ast.dart
lib/src/dart/ast/element_locator.dart
lib/src/dart/ast/extensions.dart
lib/src/dart/ast/utilities.dart
lib/src/dart/constant/constant_verifier.dart
Expand Down
283 changes: 1 addition & 282 deletions pkg/analyzer/lib/src/dart/ast/element_locator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,11 @@

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/element2.dart';
import 'package:analyzer/src/dart/ast/extensions.dart';

/// An object used to locate the [Element] associated with a given [AstNode].
/// An object used to locate the [Element2] associated with a given [AstNode].
class ElementLocator {
/// Return the element associated with the given [node], or `null` if there
/// is no element associated with the node.
static Element? locate(AstNode? node) {
if (node == null) return null;

var mapper = _ElementMapper();
return node.accept(mapper);
}

/// Return the element associated with the given [node], or `null` if there
/// is no element associated with the node.
static Element2? locate2(AstNode? node) {
Expand All @@ -29,277 +19,6 @@ class ElementLocator {
}
}

/// Visitor that maps nodes to elements.
class _ElementMapper extends GeneralizingAstVisitor<Element> {
@override
Element? visitAnnotation(Annotation node) {
return node.element;
}

@override
Element? visitAssignedVariablePattern(AssignedVariablePattern node) {
return node.element;
}

@override
Element? visitAssignmentExpression(AssignmentExpression node) {
return node.staticElement;
}

@override
Element? visitBinaryExpression(BinaryExpression node) {
return node.staticElement;
}

@override
Element? visitClassDeclaration(ClassDeclaration node) {
return node.declaredElement;
}

@override
Element? visitClassTypeAlias(ClassTypeAlias node) {
return node.declaredElement;
}

@override
Element? visitCompilationUnit(CompilationUnit node) {
return node.declaredElement;
}

@override
Element? visitConstructorDeclaration(ConstructorDeclaration node) {
return node.declaredElement;
}

@override
Element? visitConstructorSelector(ConstructorSelector node) {
var parent = node.parent;
if (parent is EnumConstantArguments) {
var parent2 = parent.parent;
if (parent2 is EnumConstantDeclaration) {
return parent2.constructorElement;
}
}
return null;
}

@override
Element? visitDeclaredIdentifier(DeclaredIdentifier node) {
return node.declaredElement;
}

@override
Element? visitDeclaredVariablePattern(DeclaredVariablePattern node) {
return node.declaredElement;
}

@override
Element? visitEnumConstantDeclaration(EnumConstantDeclaration node) {
return node.declaredElement;
}

@override
Element? visitEnumDeclaration(EnumDeclaration node) {
return node.declaredElement;
}

@override
Element? visitExportDirective(ExportDirective node) {
return node.element;
}

@override
Element? visitExtensionDeclaration(ExtensionDeclaration node) {
return node.declaredElement;
}

@override
Element? visitExtensionOverride(ExtensionOverride node) {
return node.element;
}

@override
Element? visitExtensionTypeDeclaration(ExtensionTypeDeclaration node) {
return node.declaredElement;
}

@override
Element? visitFormalParameter(FormalParameter node) {
return node.declaredElement;
}

@override
Element? visitFunctionDeclaration(FunctionDeclaration node) {
return node.declaredElement;
}

@override
Element? visitFunctionTypeAlias(FunctionTypeAlias node) {
return node.declaredElement;
}

@override
Element? visitGenericTypeAlias(GenericTypeAlias node) {
return node.declaredElement;
}

@override
Element? visitIdentifier(Identifier node) {
var parent = node.parent;
if (parent is Annotation) {
// Type name in Annotation
if (identical(parent.name, node) && parent.constructorName == null) {
return parent.element;
}
} else if (parent is ConstructorDeclaration) {
// Extra work to map Constructor Declarations to their associated
// Constructor Elements
var returnType = parent.returnType;
if (identical(returnType, node)) {
var name = parent.name;
if (name != null) {
return parent.declaredElement;
}
var element = node.staticElement;
if (element is InterfaceElement) {
return element.unnamedConstructor;
}
} else if (parent.name == node.endToken) {
return parent.declaredElement;
}
} else if (parent is LibraryIdentifier) {
var grandParent = parent.parent;
if (grandParent is PartOfDirective) {
var element = grandParent.element;
if (element is LibraryElement) {
return element.definingCompilationUnit;
}
} else if (grandParent is LibraryDirective) {
return grandParent.element;
}
}
return node.writeOrReadElement;
}

@override
Element? visitImportDirective(ImportDirective node) {
return node.element;
}

@override
Element? visitImportPrefixReference(ImportPrefixReference node) {
return node.element;
}

@override
Element? visitIndexExpression(IndexExpression node) {
return node.staticElement;
}

@override
Element? visitInstanceCreationExpression(InstanceCreationExpression node) {
return node.constructorName.staticElement;
}

@override
Element? visitLibraryDirective(LibraryDirective node) {
return node.element;
}

@override
Element? visitMethodDeclaration(MethodDeclaration node) {
return node.declaredElement;
}

@override
Element? visitMethodInvocation(MethodInvocation node) {
return node.methodName.staticElement;
}

@override
Element? visitMixinDeclaration(MixinDeclaration node) {
return node.declaredElement;
}

@override
Element? visitNamedType(NamedType node) {
return node.element;
}

@override
Element? visitPartOfDirective(PartOfDirective node) {
return node.element;
}

@override
Element? visitPatternField(PatternField node) {
return node.element;
}

@override
Element? visitPatternFieldName(PatternFieldName node) {
var parent = node.parent;
if (parent is PatternField) {
return parent.element;
} else {
return null;
}
}

@override
Element? visitPostfixExpression(PostfixExpression node) {
return node.staticElement;
}

@override
Element? visitPrefixedIdentifier(PrefixedIdentifier node) {
return node.staticElement;
}

@override
Element? visitPrefixExpression(PrefixExpression node) {
return node.staticElement;
}

@override
Element? visitRepresentationConstructorName(
RepresentationConstructorName node) {
var representation = node.parent as RepresentationDeclaration;
return representation.constructorElement;
}

@override
Element? visitRepresentationDeclaration(RepresentationDeclaration node) {
return node.fieldElement;
}

@override
Element? visitStringLiteral(StringLiteral node) {
var parent = node.parent;
if (parent is ExportDirective) {
return parent.element?.exportedLibrary;
} else if (parent is ImportDirective) {
return parent.element?.importedLibrary;
} else if (parent is PartDirective) {
var elementUri = parent.element?.uri;
if (elementUri is DirectiveUriWithUnit) {
return elementUri.unit;
}
}
return null;
}

@override
Element? visitTypeParameter(TypeParameter node) {
return node.declaredElement;
}

@override
Element? visitVariableDeclaration(VariableDeclaration node) {
return node.declaredElement;
}
}

/// Visitor that maps nodes to elements.
class _ElementMapper2 extends GeneralizingAstVisitor<Element2> {
@override
Expand Down
15 changes: 14 additions & 1 deletion pkg/analyzer/lib/src/dart/micro/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,19 @@ Element? getElementOfNode(AstNode? node) {
if (node is StringLiteral && node.parent is UriBasedDirective) {
return null;
}
var element = ElementLocator.locate(node);

Element? element;
switch (node) {
case ExportDirective():
element = node.element;
case ImportDirective():
element = node.element;
case PartOfDirective():
element = node.element;
default:
element = ElementLocator.locate2(node).asElement;
}

if (node is SimpleIdentifier && element is PrefixElement) {
var parent = node.parent;
if (parent is ImportDirective) {
Expand All @@ -33,6 +45,7 @@ Element? getElementOfNode(AstNode? node) {
element = _getImportElementInfo(node);
}
}

return element;
}

Expand Down
Loading

0 comments on commit 810eeb4

Please sign in to comment.