22// for details. All rights reserved. Use of this source code is governed by a
33// BSD-style license that can be found in the LICENSE file.
44
5- // ignore_for_file: analyzer_use_new_elements
6-
75import 'package:analyzer/dart/ast/visitor.dart' ;
8- import 'package:analyzer/dart/element/element.dart' ;
96import 'package:analyzer/dart/element/element2.dart' ;
107import 'package:analyzer/src/dart/ast/ast.dart' ;
118import 'package:analyzer/src/dart/ast/element_locator.dart' ;
129import 'package:analyzer/src/dart/element/element.dart' ;
1310import 'package:analyzer/src/utilities/extensions/element.dart' ;
1411
15- /// Return the [Element ] of the given [node] , or `null` if [node] is `null` or
12+ /// Return the [Element2 ] of the given [node] , or `null` if [node] is `null` or
1613/// does not have an element.
1714Element2 ? getElementOfNode2 (AstNode ? node) {
1815 if (node == null ) {
@@ -55,17 +52,17 @@ Element2? getElementOfNode2(AstNode? node) {
5552/// If the given [constructor] is a synthetic constructor created for a
5653/// [ClassTypeAlias] , return the actual constructor of a [ClassDeclaration]
5754/// which is invoked. Return `null` if a redirection cycle is detected.
58- ConstructorElement ? _getActualConstructorElement (
59- ConstructorElement ? constructor) {
60- var seenConstructors = < ConstructorElement ? > {};
61- while (constructor is ConstructorElementImpl && constructor.isSynthetic) {
62- var enclosing = constructor.enclosingElement3 ;
63- if (enclosing is ClassElementImpl && enclosing.isMixinApplication) {
55+ ConstructorElement2 ? _getActualConstructorElement (
56+ ConstructorElement2 ? constructor) {
57+ var seenConstructors = < ConstructorElement2 ? > {};
58+ while (constructor is ConstructorElementImpl2 && constructor.isSynthetic) {
59+ var enclosing = constructor.enclosingElement2 ;
60+ if (enclosing is ClassElementImpl2 && enclosing.isMixinApplication) {
6461 var superInvocation = constructor.constantInitializers
6562 .whereType <SuperConstructorInvocation >()
6663 .singleOrNull;
6764 if (superInvocation != null ) {
68- constructor = superInvocation.element? .asElement ;
65+ constructor = superInvocation.element;
6966 }
7067 } else {
7168 break ;
@@ -259,7 +256,7 @@ class MatchKind {
259256}
260257
261258class ReferencesCollector extends GeneralizingAstVisitor <void > {
262- final Element element;
259+ final Element2 element;
263260 final List <MatchInfo > references = [];
264261
265262 ReferencesCollector (this .element);
@@ -269,8 +266,7 @@ class ReferencesCollector extends GeneralizingAstVisitor<void> {
269266 var writeElement = node.writeElement2;
270267 if (writeElement is PropertyAccessorElement2 ) {
271268 var kind = MatchKind .WRITE ;
272- if (writeElement.variable3 == element.asElement2 ||
273- writeElement == element.asElement2) {
269+ if (writeElement.variable3 == element || writeElement == element) {
274270 if (node.leftHandSide is SimpleIdentifier ) {
275271 references.add (MatchInfo (
276272 node.leftHandSide.offset, node.leftHandSide.length, kind));
@@ -288,7 +284,7 @@ class ReferencesCollector extends GeneralizingAstVisitor<void> {
288284
289285 var readElement = node.readElement2;
290286 if (readElement is PropertyAccessorElement2 ) {
291- if (readElement.variable3 == element.asElement2 ) {
287+ if (readElement.variable3 == element) {
292288 references.add (MatchInfo (node.rightHandSide.offset,
293289 node.rightHandSide.length, MatchKind .READ ));
294290 }
@@ -299,8 +295,8 @@ class ReferencesCollector extends GeneralizingAstVisitor<void> {
299295 visitCommentReference (CommentReference node) {
300296 var expression = node.expression;
301297 if (expression is Identifier ) {
302- var element = expression.element? .asElement ;
303- if (element is ConstructorElement ) {
298+ var element = expression.element;
299+ if (element is ConstructorElement2 ) {
304300 if (expression is PrefixedIdentifier ) {
305301 var offset = expression.prefix.end;
306302 var length = expression.end - offset;
@@ -323,7 +319,7 @@ class ReferencesCollector extends GeneralizingAstVisitor<void> {
323319 @override
324320 visitConstructorDeclaration (covariant ConstructorDeclarationImpl node) {
325321 var fragment = node.declaredFragment;
326- if (fragment == element) {
322+ if (fragment? .element == element) {
327323 if (fragment! .name.isEmpty) {
328324 references.add (MatchInfo (fragment.nameOffset + fragment.nameLength, 0 ,
329325 MatchKind .DECLARATION ));
@@ -338,7 +334,7 @@ class ReferencesCollector extends GeneralizingAstVisitor<void> {
338334
339335 @override
340336 void visitConstructorName (ConstructorName node) {
341- var e = node.element? .baseElement.asElement ;
337+ var e = node.element? .baseElement;
342338 e = _getActualConstructorElement (e);
343339 MatchKind kind;
344340 int offset;
@@ -359,17 +355,17 @@ class ReferencesCollector extends GeneralizingAstVisitor<void> {
359355 length = 0 ;
360356 }
361357 references.add (MatchInfo (offset, length, kind));
362- } else if (e != null && e.enclosingElement3 == element) {
358+ } else if (e != null && e.enclosingElement2 == element) {
363359 kind = MatchKind .REFERENCE ;
364360 offset = node.offset;
365- length = element.nameLength ;
361+ length = element.name3 ? .length ?? 0 ;
366362 references.add (MatchInfo (offset, length, kind));
367363 }
368364 }
369365
370366 @override
371367 void visitEnumConstantDeclaration (EnumConstantDeclaration node) {
372- var constructorElement = node.constructorElement2.asElement ;
368+ var constructorElement = node.constructorElement2;
373369 if (constructorElement != null && constructorElement == element) {
374370 int offset;
375371 int length;
@@ -390,7 +386,7 @@ class ReferencesCollector extends GeneralizingAstVisitor<void> {
390386
391387 @override
392388 void visitNamedType (NamedType node) {
393- if (node.element2.asElement == element) {
389+ if (node.element2 == element) {
394390 references.add (
395391 MatchInfo (
396392 node.name2.offset,
@@ -407,7 +403,7 @@ class ReferencesCollector extends GeneralizingAstVisitor<void> {
407403 @override
408404 void visitRedirectingConstructorInvocation (
409405 RedirectingConstructorInvocation node) {
410- var e = node.element? .asElement ;
406+ var e = node.element;
411407 if (e == element) {
412408 if (node.constructorName != null ) {
413409 int offset = node.period! .offset;
@@ -425,10 +421,10 @@ class ReferencesCollector extends GeneralizingAstVisitor<void> {
425421 if (node.inDeclarationContext ()) {
426422 return ;
427423 }
428- var e = node.element? .asElement ;
424+ var e = node.element;
429425 if (e == element) {
430426 references.add (MatchInfo (node.offset, node.length, MatchKind .REFERENCE ));
431- } else if (e is PropertyAccessorElement && e.variable2 == element) {
427+ } else if (e is GetterElement && e.variable3 == element) {
432428 bool inGetterContext = node.inGetterContext ();
433429 bool inSetterContext = node.inSetterContext ();
434430 MatchKind kind;
@@ -445,7 +441,7 @@ class ReferencesCollector extends GeneralizingAstVisitor<void> {
445441
446442 @override
447443 void visitSuperConstructorInvocation (SuperConstructorInvocation node) {
448- var e = node.element? .asElement ;
444+ var e = node.element;
449445 if (e == element) {
450446 if (node.constructorName != null ) {
451447 int offset = node.period! .offset;
0 commit comments