|
2 | 2 | // for details. All rights reserved. Use of this source code is governed by a |
3 | 3 | // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
|
5 | | -// ignore_for_file: analyzer_use_new_elements |
6 | | - |
7 | 5 | import 'package:analyzer/dart/analysis/results.dart'; |
8 | 6 | import 'package:analyzer/dart/analysis/session.dart'; |
9 | | -import 'package:analyzer/dart/element/element.dart'; |
10 | 7 | import 'package:analyzer/dart/element/element2.dart'; |
11 | | -import 'package:analyzer/src/utilities/extensions/element.dart'; |
12 | 8 |
|
13 | 9 | /// A wrapper around [AnalysisSession] that provides additional utilities. |
14 | 10 | /// |
@@ -58,7 +54,7 @@ class AnalysisSessionHelper { |
58 | 54 | Future<EnumElement2?> getEnum(String libraryUri, String className) async { |
59 | 55 | var libraryResult = await session.getLibraryByUri(libraryUri); |
60 | 56 | if (libraryResult is LibraryElementResult) { |
61 | | - var element = libraryResult.element.exportNamespace.get2(className); |
| 57 | + var element = libraryResult.element2.exportNamespace.get2(className); |
62 | 58 | if (element is EnumElement2) { |
63 | 59 | return element; |
64 | 60 | } |
@@ -86,12 +82,17 @@ class AnalysisSessionHelper { |
86 | 82 | return null; |
87 | 83 | } |
88 | 84 |
|
89 | | - /// Return the resolved unit that declares the given [element2]. |
90 | | - Future<ResolvedUnitResult?> getResolvedUnitByElement( |
91 | | - Element2 element2) async { |
92 | | - var element = element2.asElement; |
93 | | - if (element == null) return null; |
94 | | - return await _getResolvedUnitByElement(element); |
| 85 | + /// Returns the resolved unit that declares the given [element]. |
| 86 | + Future<ResolvedUnitResult?> getResolvedUnitByElement(Element2 element) async { |
| 87 | + var libraryPath = element.library2!.firstFragment.source.fullName; |
| 88 | + var resolvedLibrary = await _getResolvedLibrary(libraryPath); |
| 89 | + if (resolvedLibrary == null) { |
| 90 | + return null; |
| 91 | + } |
| 92 | + |
| 93 | + var unitPath = element.firstFragment.libraryFragment!.source.fullName; |
| 94 | + return resolvedLibrary.units |
| 95 | + .singleWhere((resolvedUnit) => resolvedUnit.path == unitPath); |
95 | 96 | } |
96 | 97 |
|
97 | 98 | /// Returns the [PropertyAccessorElement2] with the given [name] that is |
@@ -122,18 +123,4 @@ class AnalysisSessionHelper { |
122 | 123 | } |
123 | 124 | return result; |
124 | 125 | } |
125 | | - |
126 | | - /// Return the resolved unit that declares the given [element]. |
127 | | - Future<ResolvedUnitResult?> _getResolvedUnitByElement(Element element) async { |
128 | | - var libraryPath = element.library!.source.fullName; |
129 | | - var resolvedLibrary = await _getResolvedLibrary(libraryPath); |
130 | | - if (resolvedLibrary == null) { |
131 | | - return null; |
132 | | - } |
133 | | - |
134 | | - var unitPath = element.source!.fullName; |
135 | | - return resolvedLibrary.units.singleWhere((resolvedUnit) { |
136 | | - return resolvedUnit.path == unitPath; |
137 | | - }); |
138 | | - } |
139 | 126 | } |
0 commit comments