Skip to content

Commit 074e9c3

Browse files
scheglovCommit Queue
authored andcommitted
Elements. Migrate lib/src/utilities/extensions/ast.dart
Change-Id: I14c051881037ce161df63b4e37428396940ec2e8 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/398946 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]> Reviewed-by: Phil Quitslund <[email protected]>
1 parent 83c9f85 commit 074e9c3

File tree

9 files changed

+59
-80
lines changed

9 files changed

+59
-80
lines changed

pkg/analysis_server/analyzer_use_new_elements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ lib/src/services/search/element_visitors.dart
6363
lib/src/services/search/hierarchy.dart
6464
lib/src/services/search/search_engine.dart
6565
lib/src/services/search/search_engine_internal.dart
66-
lib/src/utilities/extensions/ast.dart
6766
lib/src/utilities/extensions/element.dart
6867
test/abstract_single_unit.dart
6968
test/plugin/protocol_dart_test.dart

pkg/analysis_server/lib/src/services/completion/dart/in_scope_completion_pass.dart

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2018,11 +2018,12 @@ class InScopeCompletionPass extends SimpleAstVisitor<void> {
20182018
if (!usedNames.contains(parameter.name)) {
20192019
var matcherScore = state.matcher.score(parameter.displayName);
20202020
if (matcherScore != -1) {
2021-
var isWidget = isFlutterWidgetParameter(parameter.asElement2);
2021+
var isWidget = isFlutterWidgetParameter(
2022+
parameter.asElement2,
2023+
);
20222024
collector.addSuggestion(
20232025
NamedArgumentSuggestion(
2024-
parameter:
2025-
parameter.asElement2,
2026+
parameter: parameter.asElement2,
20262027
matcherScore: matcherScore,
20272028
appendColon: appendColon,
20282029
appendComma: false,
@@ -3313,7 +3314,7 @@ class InScopeCompletionPass extends SimpleAstVisitor<void> {
33133314
if (directive is! NamespaceDirective) {
33143315
return;
33153316
}
3316-
var library = directive.referencedLibrary;
3317+
var library = directive.referencedLibrary?.asElement;
33173318
if (library == null) {
33183319
return;
33193320
}

pkg/analysis_server/lib/src/services/correction/dart/create_field.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ class CreateField extends CreateFieldOrGetter {
167167
staticModifier = targetElement.kind == ElementKind.CLASS;
168168
}
169169
} else {
170-
targetClassElement = node.enclosingInterfaceElement2;
170+
targetClassElement = node.enclosingInterfaceElement;
171171
staticModifier = inStaticContext;
172172
}
173173

pkg/analysis_server/lib/src/services/correction/dart/create_getter.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ class CreateGetter extends CreateFieldOrGetter {
163163
}
164164
} else {
165165
targetElement =
166-
nameNode.enclosingInterfaceElement2 ??
167-
nameNode.enclosingExtensionElement2;
166+
nameNode.enclosingInterfaceElement ??
167+
nameNode.enclosingExtensionElement;
168168
if (targetElement == null) {
169169
return;
170170
}

pkg/analysis_server/lib/src/services/correction/dart/create_method_or_function.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class CreateMethodOrFunction extends ResolvedCorrectionProducer {
5252
return;
5353
}
5454
} else {
55-
targetElement = node.enclosingInterfaceElement2;
55+
targetElement = node.enclosingInterfaceElement;
5656
argument = nameNode;
5757
}
5858
argument = stepUpNamedExpression(argument);

pkg/analysis_server/lib/src/services/correction/dart/create_setter.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class CreateSetter extends ResolvedCorrectionProducer {
7171
}
7272
} else {
7373
targetElement =
74-
node.enclosingInterfaceElement2 ?? node.enclosingExtensionElement2;
74+
node.enclosingInterfaceElement ?? node.enclosingExtensionElement;
7575
if (targetElement == null) {
7676
return;
7777
}

pkg/analysis_server/lib/src/services/refactoring/legacy/inline_method.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ Set<String> _getNamesConflictingAt(AstNode node) {
175175
}
176176
// fields
177177
{
178-
var enclosingInterfaceElement = node.enclosingInterfaceElement2;
178+
var enclosingInterfaceElement = node.enclosingInterfaceElement;
179179
if (enclosingInterfaceElement != null) {
180180
var elements = [
181181
...enclosingInterfaceElement.allSupertypes.map((type) => type.element3),

pkg/analysis_server/lib/src/utilities/extensions/ast.dart

Lines changed: 33 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import 'package:analysis_server/src/utilities/extensions/element.dart';
66
import 'package:analyzer/dart/ast/ast.dart';
77
import 'package:analyzer/dart/ast/token.dart';
88
import 'package:analyzer/dart/ast/visitor.dart';
9-
import 'package:analyzer/dart/element/element.dart';
109
import 'package:analyzer/dart/element/element2.dart';
1110
import 'package:analyzer/source/source.dart';
1211
import 'package:analyzer/src/dart/ast/token.dart';
@@ -73,18 +72,8 @@ extension AnnotatedNodeExtension on AnnotatedNode {
7372
}
7473

7574
extension AstNodeExtension on AstNode {
76-
/// Returns [ExtensionElement] declared by an enclosing node.
77-
ExtensionElement? get enclosingExtensionElement {
78-
for (var node in withParents) {
79-
if (node is ExtensionDeclaration) {
80-
return node.declaredElement;
81-
}
82-
}
83-
return null;
84-
}
85-
8675
/// Returns [ExtensionElement2] declared by an enclosing node.
87-
ExtensionElement2? get enclosingExtensionElement2 {
76+
ExtensionElement2? get enclosingExtensionElement {
8877
for (var node in withParents) {
8978
if (node is ExtensionDeclaration) {
9079
return node.declaredFragment?.element;
@@ -105,20 +94,8 @@ extension AstNodeExtension on AstNode {
10594
return null;
10695
}
10796

108-
/// Returns [InterfaceElement] declared by an enclosing node.
109-
InterfaceElement? get enclosingInterfaceElement {
110-
for (var node in withParents) {
111-
if (node is ClassDeclaration) {
112-
return node.declaredElement;
113-
} else if (node is MixinDeclaration) {
114-
return node.declaredElement;
115-
}
116-
}
117-
return null;
118-
}
119-
12097
/// Returns [InterfaceElement2] declared by an enclosing node.
121-
InterfaceElement2? get enclosingInterfaceElement2 {
98+
InterfaceElement2? get enclosingInterfaceElement {
12299
for (var node in withParents) {
123100
if (node is ClassDeclaration) {
124101
return node.declaredFragment?.element;
@@ -200,10 +177,10 @@ extension AstNodeExtension on AstNode {
200177
ExpressionStatement? findSimplePrintInvocation() {
201178
var parent = this.parent;
202179
var grandparent = parent?.parent;
203-
if (this case SimpleIdentifier(:var staticElement)) {
204-
if (staticElement is FunctionElement &&
205-
staticElement.name == 'print' &&
206-
staticElement.library.isDartCore &&
180+
if (this case SimpleIdentifier(:var element)) {
181+
if (element is TopLevelFunctionElement &&
182+
element.name3 == 'print' &&
183+
element.library2.isDartCore &&
207184
parent is MethodInvocation &&
208185
grandparent is ExpressionStatement) {
209186
return grandparent;
@@ -291,15 +268,16 @@ extension DeclaredVariablePatternExtension on DeclaredVariablePattern {
291268
}
292269

293270
extension DirectiveExtension on Directive {
294-
/// If the target imports or exports a [LibraryElement], returns it.
295-
LibraryElement? get referencedLibrary {
296-
var element = this.element;
297-
if (element is LibraryExportElement) {
298-
return element.exportedLibrary;
299-
} else if (element is LibraryImportElement) {
300-
return element.importedLibrary;
271+
/// If the target imports or exports a [LibraryElement2], returns it.
272+
LibraryElement2? get referencedLibrary {
273+
switch (this) {
274+
case ExportDirective directive:
275+
return directive.libraryExport?.exportedLibrary2;
276+
case ImportDirective directive:
277+
return directive.libraryImport?.importedLibrary2;
278+
default:
279+
return null;
301280
}
302-
return null;
303281
}
304282

305283
/// If [referencedUri] is a [DirectiveUriWithSource], returns the [Source]
@@ -314,45 +292,46 @@ extension DirectiveExtension on Directive {
314292

315293
/// Returns the [DirectiveUri] from the element.
316294
DirectiveUri? get referencedUri {
317-
var self = this;
318-
if (self is ExportDirective) {
319-
return self.element?.uri;
320-
} else if (self is ImportDirective) {
321-
return self.element?.uri;
322-
} else if (self is PartDirective) {
323-
return self.element?.uri;
295+
switch (this) {
296+
case ExportDirective directive:
297+
return directive.libraryExport?.uri;
298+
case ImportDirective directive:
299+
return directive.libraryImport?.uri;
300+
case PartDirective directive:
301+
return directive.partInclude?.uri;
302+
default:
303+
return null;
324304
}
325-
return null;
326305
}
327306
}
328307

329308
extension ExpressionExtension on Expression {
330309
/// Return `true` if this expression is an invocation of the method `cast`
331310
/// from either Iterable`, `List`, `Map`, or `Set`.
332311
bool get isCastMethodInvocation {
333-
if (this is MethodInvocation) {
334-
var element = (this as MethodInvocation).methodName.staticElement;
335-
return element is MethodElement && element.isCastMethod;
312+
if (this case MethodInvocation self) {
313+
var element = self.methodName.element;
314+
return element is MethodElement2 && element.isCastMethod;
336315
}
337316
return false;
338317
}
339318

340319
/// Return `true` if this expression is an invocation of the method `toList`
341320
/// from `Iterable`.
342321
bool get isToListMethodInvocation {
343-
if (this is MethodInvocation) {
344-
var element = (this as MethodInvocation).methodName.staticElement;
345-
return element is MethodElement && element.isToListMethod;
322+
if (this case MethodInvocation self) {
323+
var element = self.methodName.element;
324+
return element is MethodElement2 && element.isToListMethod;
346325
}
347326
return false;
348327
}
349328

350329
/// Return `true` if this expression is an invocation of the method `toSet`
351330
/// from `Iterable`.
352331
bool get isToSetMethodInvocation {
353-
if (this is MethodInvocation) {
354-
var element = (this as MethodInvocation).methodName.staticElement;
355-
return element is MethodElement && element.isToSetMethod;
332+
if (this case MethodInvocation self) {
333+
var element = self.methodName.element;
334+
return element is MethodElement2 && element.isToSetMethod;
356335
}
357336
return false;
358337
}

pkg/analysis_server/lib/src/utilities/extensions/element.dart

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@
55
import 'package:analyzer/dart/element/element.dart';
66
import 'package:analyzer/dart/element/element2.dart';
77

8-
extension ClassElementExtensions on ClassElement {
8+
extension ClassElementExtensions on ClassElement2 {
99
/// Return `true` if this element represents the class `Iterable` from
1010
/// `dart:core`.
11-
bool get isDartCoreIterable => name == 'Iterable' && library.isDartCore;
11+
bool get isDartCoreIterable => name3 == 'Iterable' && library2.isDartCore;
1212

1313
/// Return `true` if this element represents the class `List` from
1414
/// `dart:core`.
15-
bool get isDartCoreList => name == 'List' && library.isDartCore;
15+
bool get isDartCoreList => name3 == 'List' && library2.isDartCore;
1616

1717
/// Return `true` if this element represents the class `Map` from
1818
/// `dart:core`.
19-
bool get isDartCoreMap => name == 'Map' && library.isDartCore;
19+
bool get isDartCoreMap => name3 == 'Map' && library2.isDartCore;
2020

2121
/// Return `true` if this element represents the class `Set` from
2222
/// `dart:core`.
23-
bool get isDartCoreSet => name == 'Set' && library.isDartCore;
23+
bool get isDartCoreSet => name3 == 'Set' && library2.isDartCore;
2424
}
2525

2626
extension Element2Extension on Element2 {
@@ -110,15 +110,15 @@ extension LibraryElementExtensions2 on LibraryElement2 {
110110
}
111111
}
112112

113-
extension MethodElementExtensions on MethodElement {
113+
extension MethodElementExtensions on MethodElement2 {
114114
/// Return `true` if this element represents the method `cast` from either
115115
/// `Iterable`, `List`, `Map`, or `Set`.
116116
bool get isCastMethod {
117-
if (name != 'cast') {
117+
if (name3 != 'cast') {
118118
return false;
119119
}
120-
var definingClass = enclosingElement3;
121-
if (definingClass is! ClassElement) {
120+
var definingClass = enclosingElement2;
121+
if (definingClass is! ClassElement2) {
122122
return false;
123123
}
124124
return definingClass.isDartCoreIterable ||
@@ -130,11 +130,11 @@ extension MethodElementExtensions on MethodElement {
130130
/// Return `true` if this element represents the method `toList` from
131131
/// `Iterable`.
132132
bool get isToListMethod {
133-
if (name != 'toList') {
133+
if (name3 != 'toList') {
134134
return false;
135135
}
136-
var definingClass = enclosingElement3;
137-
if (definingClass is! ClassElement) {
136+
var definingClass = enclosingElement2;
137+
if (definingClass is! ClassElement2) {
138138
return false;
139139
}
140140
return definingClass.isDartCoreIterable;
@@ -143,11 +143,11 @@ extension MethodElementExtensions on MethodElement {
143143
/// Return `true` if this element represents the method `toSet` from
144144
/// `Iterable`.
145145
bool get isToSetMethod {
146-
if (name != 'toSet') {
146+
if (name3 != 'toSet') {
147147
return false;
148148
}
149-
var definingClass = enclosingElement3;
150-
if (definingClass is! ClassElement) {
149+
var definingClass = enclosingElement2;
150+
if (definingClass is! ClassElement2) {
151151
return false;
152152
}
153153
return definingClass.isDartCoreIterable;

0 commit comments

Comments
 (0)