55import 'package:analysis_server/src/services/correction/fix/data_driven/element_descriptor.dart' ;
66import 'package:analysis_server/src/services/correction/fix/data_driven/element_kind.dart' ;
77import 'package:analyzer/dart/ast/token.dart' ;
8- import 'package:analyzer/dart/element/element .dart'
8+ import 'package:analyzer/dart/element/element2 .dart'
99 show
10- CompilationUnitElement,
11- ExtensionElement,
12- InterfaceElement,
13- PrefixElement,
14- PropertyAccessorElement;
10+ LibraryElement2,
11+ LibraryFragment,
12+ ExtensionElement2,
13+ GetterElement,
14+ InterfaceElement2,
15+ PrefixElement2,
16+ SetterElement;
1517import 'package:analyzer/dart/element/type.dart' ;
1618import 'package:analyzer/src/dart/ast/ast.dart' ;
1719
@@ -47,7 +49,7 @@ class ElementMatcher {
4749 }) : assert (components.isNotEmpty),
4850 validKinds = kinds;
4951
50- /// Return `true` if this matcher matches the given [element] .
52+ /// Returns `true` if this matcher matches the given [element] .
5153 bool matches (ElementDescriptor element) {
5254 //
5355 // Check that the components in the element's name match the node.
@@ -90,11 +92,12 @@ class ElementMatcher {
9092 while (parent != null && parent.parent is ! CompilationUnit ) {
9193 parent = parent.parent;
9294 }
93- var element = (parent as CompilationUnitMember ).declaredElement;
94- if (element is ! InterfaceElement ) {
95+ var element =
96+ (parent as CompilationUnitMember ).declaredFragment? .element;
97+ if (element is ! InterfaceElement2 ) {
9598 return false ;
9699 }
97- var types = element.allSupertypes.map ((e) => e.element.name );
100+ var types = element.allSupertypes.map ((e) => e.element3.name3 );
98101 for (var t in types) {
99102 if (elementComponents.contains (t)) {
100103 return true ;
@@ -134,9 +137,11 @@ class ElementMatcher {
134137 return false ;
135138 }
136139
137- /// Return a list of element matchers that will match the element that is, or
138- /// should be, associated with the given [node] . The list will be empty if
139- /// there are no appropriate matchers for the [node] .
140+ /// Returns a list of element matchers that will match the element that is, or
141+ /// should be, associated with the given [node] .
142+ ///
143+ /// The list will be empty if there are no appropriate matchers for the
144+ /// [node] .
140145 static List <ElementMatcher > matchersForNode (AstNode ? node, Token ? nameToken) {
141146 if (node == null ) {
142147 return const [];
@@ -150,26 +155,30 @@ class ElementMatcher {
150155 return builder.matchers.toList ();
151156 }
152157
153- /// Return the URIs of the imports in the library containing the [node] , or
154- /// `null` if the imports can't be determined.
158+ /// Returns the URIs of the imports in the library containing the [node] .
159+ ///
160+ /// Returns `null` if the imports can't be determined.
155161 static List <Uri >? _importElementsForNode (AstNode node) {
156162 var root = node.root;
157163 if (root is ! CompilationUnit ) {
158164 return null ;
159165 }
160166 var importedUris = < Uri > [];
161- var library = root.declaredElement ? .library ;
162- if (library == null ) {
167+ LibraryFragment ? part = root.declaredFragment ;
168+ if (part == null ) {
163169 return null ;
164170 }
165- for (var importElement in library.definingCompilationUnit.libraryImports) {
166- // TODO(brianwilkerson): Filter based on combinators to help avoid making
167- // invalid suggestions.
168- var uri = importElement.importedLibrary? .source.uri;
169- if (uri != null ) {
170- // The [uri] is `null` if the literal string is not a valid URI.
171- importedUris.add (uri);
171+ while (part != null ) {
172+ for (var libraryImport in part.libraryImports2) {
173+ // TODO(brianwilkerson): Filter based on combinators to help avoid making
174+ // invalid suggestions.
175+ var uri = libraryImport.importedLibrary2? .firstFragment.source.uri;
176+ if (uri != null ) {
177+ // The [uri] is `null` if the literal string is not a valid URI.
178+ importedUris.add (uri);
179+ }
172180 }
181+ part = part.enclosingFragment;
173182 }
174183 return importedUris;
175184 }
@@ -261,7 +270,7 @@ class _MatcherBuilder {
261270 );
262271 }
263272 } else if (parent is SuperConstructorInvocation ) {
264- var superclassName = parent.staticElement ? .enclosingElement3.name ;
273+ var superclassName = parent.element ? .enclosingElement2.name3 ;
265274 if (superclassName != null ) {
266275 _addMatcher (
267276 components: [parent.constructorName? .name ?? '' , superclassName],
@@ -273,8 +282,8 @@ class _MatcherBuilder {
273282
274283 /// Build a matcher for the operator being invoked.
275284 void _buildFromBinaryExpression (BinaryExpression node) {
276- // TODO(brianwilkerson): Implement this method in order to support changes to
277- // operators.
285+ // TODO(brianwilkerson): Implement this method in order to support changes
286+ // to operators.
278287 }
279288
280289 /// Build a matcher for the constructor being referenced.
@@ -404,7 +413,7 @@ class _MatcherBuilder {
404413 // TODO(brianwilkerson): Use the static element, if there is one, in order to
405414 // get a more exact matcher.
406415 var prefix = node.prefix;
407- if (prefix.staticElement is PrefixElement ) {
416+ if (prefix.element is PrefixElement2 ) {
408417 var parent = node.parent;
409418 if ((parent is NamedType && parent.parent is ! ConstructorName ) ||
410419 (parent is PropertyAccess && parent.target == node)) {
@@ -437,7 +446,7 @@ class _MatcherBuilder {
437446 var targetType = node.prefix.staticType;
438447 if (targetType is InterfaceType ) {
439448 _addMatcher (
440- components: [node.identifier.name, targetType.element.name ],
449+ components: [node.identifier.name, targetType.element3.name3 ! ],
441450 kinds: const [
442451 ElementKind .constantKind,
443452 ElementKind .fieldKind,
@@ -450,10 +459,10 @@ class _MatcherBuilder {
450459 }
451460 // It looks like we're accessing a member, but we don't know what kind of
452461 // member, so we include all of the member kinds.
453- var container = node.prefix.staticElement ;
454- if (container is InterfaceElement ) {
462+ var container = node.prefix.element ;
463+ if (container is InterfaceElement2 ) {
455464 _addMatcher (
456- components: [node.identifier.name, container.name ],
465+ components: [node.identifier.name, container.name3 ! ],
457466 kinds: const [
458467 ElementKind .constantKind,
459468 ElementKind .fieldKind,
@@ -463,7 +472,7 @@ class _MatcherBuilder {
463472 ElementKind .setterKind,
464473 ],
465474 );
466- } else if (container is ExtensionElement ) {
475+ } else if (container is ExtensionElement2 ) {
467476 _addMatcher (
468477 components: [node.identifier.name, container.displayName],
469478 kinds: const [
@@ -534,14 +543,14 @@ class _MatcherBuilder {
534543 if (node.staticType is InvalidType ) {
535544 _addMatcher (components: [node.name], kinds: [], node: node);
536545 } else {
537- var staticElement = node.staticElement ;
546+ var element = node.element ;
538547 // Add enclosing element to the matcher for non top level property
539548 // accessors when possible.
540- if (staticElement is PropertyAccessorElement ) {
541- var enclosingElement = staticElement.enclosingElement3 ;
542- if (enclosingElement is ! CompilationUnitElement ) {
549+ if (element is GetterElement || element is SetterElement ) {
550+ var enclosingElement = element ? .enclosingElement2 ;
551+ if (enclosingElement is ! LibraryElement2 ) {
543552 _addMatcher (
544- components: [node.name, enclosingElement.displayName],
553+ components: [node.name, ( enclosingElement? .displayName) ! ],
545554 kinds: [],
546555 );
547556 return ;
@@ -567,22 +576,22 @@ class _MatcherBuilder {
567576 }
568577 }
569578
570- /// Return the components associated with the [identifier] when there is no
579+ /// Returns the components associated with the [identifier] when there is no
571580 /// contextual information.
572581 static List <String > _componentsFromIdentifier (SimpleIdentifier identifier) {
573- var element = identifier.staticElement ;
582+ var element = identifier.element ;
574583 if (element == null ) {
575584 var parent = identifier.parent;
576585 if (parent is AssignmentExpression && identifier == parent.leftHandSide) {
577- element = parent.writeElement ;
586+ element = parent.writeElement2 ;
578587 }
579588 }
580589 if (element != null ) {
581- var enclosingElement = element.enclosingElement3 ;
582- if (enclosingElement is InterfaceElement ) {
583- return [identifier.name, enclosingElement.name ];
584- } else if (enclosingElement is ExtensionElement ) {
585- var name = enclosingElement.name ;
590+ var enclosingElement = element.enclosingElement2 ;
591+ if (enclosingElement is InterfaceElement2 ) {
592+ return [identifier.name, enclosingElement.name3 ! ];
593+ } else if (enclosingElement is ExtensionElement2 ) {
594+ var name = enclosingElement.name3 ;
586595 if (name != null ) {
587596 return [identifier.name, name];
588597 }
@@ -591,18 +600,18 @@ class _MatcherBuilder {
591600 return [identifier.name];
592601 }
593602
594- /// Return `true` if the [node] is a prefix
603+ /// Returns `true` if the [node] is a prefix.
595604 static bool _isPrefix (AstNode ? node) {
596- return node is SimpleIdentifier && node.staticElement is PrefixElement ;
605+ return node is SimpleIdentifier && node.element is PrefixElement2 ;
597606 }
598607
599- /// Return the name of the class associated with the given [target] .
608+ /// Returns the name of the class associated with the given [target] .
600609 static String ? _nameOfTarget (Expression ? target) {
601610 if (target is SimpleIdentifier ) {
602611 var type = target.staticType;
603612 if (type != null ) {
604613 if (type is InterfaceType ) {
605- return type.element.name ;
614+ return type.element3.name3 ;
606615 } else if (type is DynamicType ) {
607616 // The name is likely to be undefined.
608617 return target.name;
@@ -613,7 +622,7 @@ class _MatcherBuilder {
613622 } else if (target != null ) {
614623 var type = target.staticType;
615624 if (type is InterfaceType ) {
616- return type.element.name ;
625+ return type.element3.name3 ;
617626 }
618627 return null ;
619628 }
0 commit comments