55import 'package:analyzer/dart/ast/ast.dart' ;
66import 'package:analyzer/dart/ast/visitor.dart' ;
77import 'package:analyzer/dart/element/element.dart' ;
8+ import 'package:analyzer/dart/element/element2.dart' ;
89import '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+ }
0 commit comments