Skip to content

Commit 1a9082c

Browse files
DanTupCommit Queue
authored andcommitted
[analyzer] Add ElementLocator2, an Element2 version of ElementLocator
Change-Id: I47fb85791ef9a077ac2d46b67d39adb9e75e45cd Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/391140 Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent e9d03a0 commit 1a9082c

File tree

5 files changed

+740
-22
lines changed

5 files changed

+740
-22
lines changed

pkg/analyzer/lib/src/dart/ast/element_locator.dart

Lines changed: 273 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'package:analyzer/dart/ast/ast.dart';
66
import 'package:analyzer/dart/ast/visitor.dart';
77
import 'package:analyzer/dart/element/element.dart';
8+
import 'package:analyzer/dart/element/element2.dart';
89
import 'package:analyzer/src/dart/ast/extensions.dart';
910

1011
/// An object used to locate the [Element] associated with a given [AstNode].
@@ -17,6 +18,15 @@ class ElementLocator {
1718
var mapper = _ElementMapper();
1819
return node.accept(mapper);
1920
}
21+
22+
/// Return the element associated with the given [node], or `null` if there
23+
/// is no element associated with the node.
24+
static Element2? locate2(AstNode? node) {
25+
if (node == null) return null;
26+
27+
var mapper = _ElementMapper2();
28+
return node.accept(mapper);
29+
}
2030
}
2131

2232
/// Visitor that maps nodes to elements.
@@ -289,3 +299,266 @@ class _ElementMapper extends GeneralizingAstVisitor<Element> {
289299
return node.declaredElement;
290300
}
291301
}
302+
303+
/// Visitor that maps nodes to elements.
304+
class _ElementMapper2 extends GeneralizingAstVisitor<Element2> {
305+
@override
306+
Element2? visitAnnotation(Annotation node) {
307+
return node.element2;
308+
}
309+
310+
@override
311+
Element2? visitAssignedVariablePattern(AssignedVariablePattern node) {
312+
return node.element2;
313+
}
314+
315+
@override
316+
Element2? visitAssignmentExpression(AssignmentExpression node) {
317+
return node.element;
318+
}
319+
320+
@override
321+
Element2? visitBinaryExpression(BinaryExpression node) {
322+
return node.element;
323+
}
324+
325+
@override
326+
Element2? visitClassDeclaration(ClassDeclaration node) {
327+
return node.declaredFragment?.element;
328+
}
329+
330+
@override
331+
Element2? visitClassTypeAlias(ClassTypeAlias node) {
332+
return node.declaredFragment?.element;
333+
}
334+
335+
@override
336+
Element2? visitCompilationUnit(CompilationUnit node) {
337+
return node.declaredFragment?.element;
338+
}
339+
340+
@override
341+
Element2? visitConstructorDeclaration(ConstructorDeclaration node) {
342+
return node.declaredFragment?.element;
343+
}
344+
345+
@override
346+
Element2? visitConstructorSelector(ConstructorSelector node) {
347+
var parent = node.parent;
348+
if (parent is EnumConstantArguments) {
349+
var parent2 = parent.parent;
350+
if (parent2 is EnumConstantDeclaration) {
351+
return parent2.constructorElement2;
352+
}
353+
}
354+
return null;
355+
}
356+
357+
@override
358+
Element2? visitDeclaredIdentifier(DeclaredIdentifier node) {
359+
return node.declaredElement2;
360+
}
361+
362+
@override
363+
Element2? visitDeclaredVariablePattern(DeclaredVariablePattern node) {
364+
return node.declaredElement2;
365+
}
366+
367+
@override
368+
Element2? visitEnumConstantDeclaration(EnumConstantDeclaration node) {
369+
return node.declaredFragment?.element;
370+
}
371+
372+
@override
373+
Element2? visitEnumDeclaration(EnumDeclaration node) {
374+
return node.declaredFragment?.element;
375+
}
376+
377+
@override
378+
Element2? visitExportDirective(ExportDirective node) {
379+
return node.libraryExport?.exportedLibrary2;
380+
}
381+
382+
@override
383+
Element2? visitExtensionDeclaration(ExtensionDeclaration node) {
384+
return node.declaredFragment?.element;
385+
}
386+
387+
@override
388+
Element2? visitExtensionOverride(ExtensionOverride node) {
389+
return node.element2;
390+
}
391+
392+
@override
393+
Element2? visitExtensionTypeDeclaration(ExtensionTypeDeclaration node) {
394+
return node.declaredFragment?.element;
395+
}
396+
397+
@override
398+
Element2? visitFormalParameter(FormalParameter node) {
399+
return node.declaredFragment?.element;
400+
}
401+
402+
@override
403+
Element2? visitFunctionDeclaration(FunctionDeclaration node) {
404+
return node.declaredFragment?.element;
405+
}
406+
407+
@override
408+
Element2? visitFunctionTypeAlias(FunctionTypeAlias node) {
409+
return node.declaredFragment?.element;
410+
}
411+
412+
@override
413+
Element2? visitGenericTypeAlias(GenericTypeAlias node) {
414+
return node.declaredFragment?.element;
415+
}
416+
417+
@override
418+
Element2? visitIdentifier(Identifier node) {
419+
var parent = node.parent;
420+
if (parent is Annotation) {
421+
// Map the type name in an annotation.
422+
if (identical(parent.name, node) && parent.constructorName == null) {
423+
return parent.element2;
424+
}
425+
} else if (parent is ConstructorDeclaration) {
426+
// Map a constructor declarations to its associated constructor element.
427+
var returnType = parent.returnType;
428+
if (identical(returnType, node)) {
429+
var name = parent.name;
430+
if (name != null) {
431+
return parent.declaredFragment?.element;
432+
}
433+
var element = node.element;
434+
if (element is InterfaceElement2) {
435+
return element.unnamedConstructor2;
436+
}
437+
} else if (parent.name == node.endToken) {
438+
return parent.declaredFragment?.element;
439+
}
440+
} else if (parent is LibraryIdentifier) {
441+
var grandParent = parent.parent;
442+
if (grandParent is LibraryDirective) {
443+
return grandParent.element2;
444+
}
445+
return null;
446+
}
447+
return node.writeOrReadElement2;
448+
}
449+
450+
@override
451+
Element2? visitImportDirective(ImportDirective node) {
452+
return node.libraryImport?.importedLibrary2;
453+
}
454+
455+
@override
456+
Element2? visitImportPrefixReference(ImportPrefixReference node) {
457+
return node.element2;
458+
}
459+
460+
@override
461+
Element2? visitIndexExpression(IndexExpression node) {
462+
return node.element;
463+
}
464+
465+
@override
466+
Element2? visitInstanceCreationExpression(InstanceCreationExpression node) {
467+
return node.constructorName.element;
468+
}
469+
470+
@override
471+
Element2? visitLibraryDirective(LibraryDirective node) {
472+
return node.element2;
473+
}
474+
475+
@override
476+
Element2? visitMethodDeclaration(MethodDeclaration node) {
477+
return node.declaredFragment?.element;
478+
}
479+
480+
@override
481+
Element2? visitMethodInvocation(MethodInvocation node) {
482+
return node.methodName.element;
483+
}
484+
485+
@override
486+
Element2? visitMixinDeclaration(MixinDeclaration node) {
487+
return node.declaredFragment?.element;
488+
}
489+
490+
@override
491+
Element2? visitNamedType(NamedType node) {
492+
return node.element2;
493+
}
494+
495+
@override
496+
Element2? visitPartOfDirective(PartOfDirective node) {
497+
return node.libraryName?.element;
498+
}
499+
500+
@override
501+
Element2? visitPatternField(PatternField node) {
502+
return node.element2;
503+
}
504+
505+
@override
506+
Element2? visitPatternFieldName(PatternFieldName node) {
507+
var parent = node.parent;
508+
if (parent is PatternField) {
509+
return parent.element2;
510+
} else {
511+
return null;
512+
}
513+
}
514+
515+
@override
516+
Element2? visitPostfixExpression(PostfixExpression node) {
517+
return node.element;
518+
}
519+
520+
@override
521+
Element2? visitPrefixedIdentifier(PrefixedIdentifier node) {
522+
return node.element;
523+
}
524+
525+
@override
526+
Element2? visitPrefixExpression(PrefixExpression node) {
527+
return node.element;
528+
}
529+
530+
@override
531+
Element2? visitRepresentationConstructorName(
532+
RepresentationConstructorName node) {
533+
var representation = node.parent as RepresentationDeclaration;
534+
return representation.constructorFragment?.element;
535+
}
536+
537+
@override
538+
Element2? visitRepresentationDeclaration(RepresentationDeclaration node) {
539+
return node.fieldFragment?.element;
540+
}
541+
542+
@override
543+
Element2? visitStringLiteral(StringLiteral node) {
544+
var parent = node.parent;
545+
if (parent is ExportDirective) {
546+
return parent.libraryExport?.exportedLibrary2;
547+
} else if (parent is ImportDirective) {
548+
return parent.libraryImport?.importedLibrary2;
549+
} else if (parent is PartDirective) {
550+
return null;
551+
}
552+
return null;
553+
}
554+
555+
@override
556+
Element2? visitTypeParameter(TypeParameter node) {
557+
return node.declaredFragment?.element;
558+
}
559+
560+
@override
561+
Element2? visitVariableDeclaration(VariableDeclaration node) {
562+
return node.declaredFragment?.element;
563+
}
564+
}

pkg/analyzer/lib/src/dart/element/element.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6937,7 +6937,8 @@ mixin MaybeAugmentedExtensionTypeElementMixin
69376937
ConstructorElement2 get primaryConstructor2 => primaryConstructor.element;
69386938

69396939
@override
6940-
FieldElement2 get representation2 => representation as FieldElement2;
6940+
FieldElement2 get representation2 =>
6941+
representation.asElement2 as FieldElement2;
69416942

69426943
@override
69436944
T? accept2<T>(ElementVisitor2<T> visitor) {

pkg/analyzer/lib/src/test_utilities/find_element2.dart

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:analyzer/dart/ast/ast.dart';
6-
import 'package:analyzer/dart/element/element.dart';
76
import 'package:analyzer/dart/element/element2.dart';
87
import 'package:analyzer/src/test_utilities/function_ast_visitor.dart';
98
import 'package:analyzer/src/utilities/extensions/element.dart';
@@ -38,8 +37,8 @@ class FindElement2 extends _FindElementBase {
3837
throw StateError('Not found: $targetUri');
3938
}
4039

41-
FieldFormalParameterElement fieldFormalParameter(String name) {
42-
return parameter(name) as FieldFormalParameterElement;
40+
FieldFormalParameterElement2 fieldFormalParameter(String name) {
41+
return parameter(name) as FieldFormalParameterElement2;
4342
}
4443

4544
TopLevelFunctionElement function(String name) {
@@ -79,11 +78,11 @@ class FindElement2 extends _FindElementBase {
7978
return ImportFindElement(import);
8079
}
8180

82-
LabelElement label(String name) {
83-
LabelElement? result;
81+
LabelElement2 label(String name) {
82+
LabelElement2? result;
8483

85-
void updateResult(Element element) {
86-
if (element is LabelElement && element.name == name) {
84+
void updateResult(Element2 element) {
85+
if (element is LabelElement2 && element.name == name) {
8786
if (result != null) {
8887
throw StateError('Not unique: $name');
8988
}
@@ -93,7 +92,7 @@ class FindElement2 extends _FindElementBase {
9392

9493
unit.accept(FunctionAstVisitor(
9594
label: (node) {
96-
updateResult(node.label.staticElement!);
95+
updateResult(node.label.element!);
9796
},
9897
));
9998

@@ -103,13 +102,13 @@ class FindElement2 extends _FindElementBase {
103102
return result!;
104103
}
105104

106-
FunctionElement localFunction(String name) {
107-
FunctionElement? result;
105+
LocalFunctionElement localFunction(String name) {
106+
LocalFunctionElement? result;
108107

109108
unit.accept(FunctionAstVisitor(
110109
functionDeclarationStatement: (node) {
111-
var element = node.functionDeclaration.declaredElement;
112-
if (element is FunctionElement && element.name == name) {
110+
var element = node.functionDeclaration.declaredElement2;
111+
if (element is LocalFunctionElement && element.name == name) {
113112
if (result != null) {
114113
throw StateError('Not unique: $name');
115114
}
@@ -124,11 +123,11 @@ class FindElement2 extends _FindElementBase {
124123
return result!;
125124
}
126125

127-
LocalVariableElement localVar(String name) {
128-
LocalVariableElement? result;
126+
LocalVariableElement2 localVar(String name) {
127+
LocalVariableElement2? result;
129128

130-
void updateResult(Element element) {
131-
if (element is LocalVariableElement && element.name == name) {
129+
void updateResult(Element2 element) {
130+
if (element is LocalVariableElement2 && element.name == name) {
132131
if (result != null) {
133132
throw StateError('Not unique: $name');
134133
}
@@ -138,16 +137,16 @@ class FindElement2 extends _FindElementBase {
138137

139138
unit.accept(FunctionAstVisitor(
140139
catchClauseParameter: (node) {
141-
updateResult(node.declaredElement!);
140+
updateResult(node.declaredElement2!);
142141
},
143142
declaredIdentifier: (node) {
144-
updateResult(node.declaredElement!);
143+
updateResult(node.declaredElement2!);
145144
},
146145
declaredVariablePattern: (node) {
147-
updateResult(node.declaredElement!);
146+
updateResult(node.declaredElement2!);
148147
},
149148
variableDeclaration: (node) {
150-
updateResult(node.declaredElement!);
149+
updateResult(node.declaredElement2!);
151150
},
152151
));
153152

0 commit comments

Comments
 (0)