Skip to content

Commit 8fa66a0

Browse files
bwilkersonCommit Queue
authored andcommitted
Migrate data-driven fix support
Change-Id: I8ac392d47c105225b22a32651693c3caa515eda0 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/395040 Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent 9ea9386 commit 8fa66a0

File tree

4 files changed

+103
-89
lines changed

4 files changed

+103
-89
lines changed

pkg/analysis_server/analyzer_use_new_elements.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@ lib/src/services/correction/dart/add_extension_override.dart
5252
lib/src/services/correction/dart/create_extension_member.dart
5353
lib/src/services/correction/dart/import_library.dart
5454
lib/src/services/correction/dart/use_different_division_operator.dart
55-
lib/src/services/correction/fix/data_driven/element_matcher.dart
56-
lib/src/services/correction/fix/data_driven/rename_parameter.dart
57-
lib/src/services/correction/fix/data_driven/replaced_by.dart
5855
lib/src/services/correction/namespace.dart
5956
lib/src/services/correction/util.dart
6057
lib/src/services/flutter/class_description.dart
@@ -110,4 +107,3 @@ test/services/refactoring/legacy/rename_library_test.dart
110107
test/services/search/element_visitors_test.dart
111108
test/services/search/hierarchy_test.dart
112109
test/services/search/search_engine_test.dart
113-
tool/code_completion/try_hypothesis.dart

pkg/analysis_server/lib/src/services/correction/fix/data_driven/element_matcher.dart

Lines changed: 60 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
import 'package:analysis_server/src/services/correction/fix/data_driven/element_descriptor.dart';
66
import 'package:analysis_server/src/services/correction/fix/data_driven/element_kind.dart';
77
import '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;
1517
import 'package:analyzer/dart/element/type.dart';
1618
import '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
}

pkg/analysis_server/lib/src/services/correction/fix/data_driven/rename_parameter.dart

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import 'package:analysis_server/src/services/correction/dart/data_driven.dart';
66
import 'package:analysis_server/src/services/correction/fix/data_driven/change.dart';
77
import 'package:analyzer/dart/ast/ast.dart';
8-
import 'package:analyzer/dart/element/element.dart';
8+
import 'package:analyzer/dart/element/element2.dart';
99
import 'package:analyzer/src/dart/element/inheritance_manager3.dart';
1010
import 'package:analyzer_plugin/utilities/change_builder/change_builder_dart.dart';
1111
import 'package:analyzer_plugin/utilities/range_factory.dart';
@@ -33,7 +33,9 @@ class RenameParameter extends Change<_Data> {
3333
var declaration = data.methodDeclaration;
3434
var parameter = declaration.parameterNamed(oldName);
3535
if (parameter != null) {
36-
var overriddenMethod = declaration.overriddenElement();
36+
var overriddenMethod = declaration.overriddenElement(
37+
fix.inheritanceManager,
38+
);
3739
var overriddenParameter = overriddenMethod?.parameterNamed(oldName);
3840
if (overriddenParameter == null) {
3941
// If the overridden parameter has already been removed, then just
@@ -46,7 +48,7 @@ class RenameParameter extends Change<_Data> {
4648
// If the overridden parameter still exists, then mark the overriding
4749
// parameter as deprecated (if it isn't already and the overridden
4850
// parameter is) and add a declaration of the new parameter.
49-
var parameterElement = parameter.declaredElement;
51+
var parameterElement = parameter.declaredFragment?.element;
5052
if (parameterElement != null) {
5153
builder.addInsertion(parameter.offset, (builder) {
5254
builder.writeParameter(
@@ -56,8 +58,8 @@ class RenameParameter extends Change<_Data> {
5658
type: parameterElement.type,
5759
);
5860
builder.write(', ');
59-
if (overriddenParameter.hasDeprecated &&
60-
!parameterElement.hasDeprecated) {
61+
if (overriddenParameter.metadata2.hasDeprecated &&
62+
!parameterElement.metadata2.hasDeprecated) {
6163
builder.write('@deprecated ');
6264
}
6365
});
@@ -129,27 +131,34 @@ class _OverrideData extends _Data {
129131
}
130132

131133
extension on MethodDeclaration {
132-
/// Return the element that this method overrides, or `null` if this method
133-
/// doesn't override any inherited member.
134-
ExecutableElement? overriddenElement() {
135-
var element = declaredElement;
134+
/// Returns the element that this method overrides.
135+
///
136+
/// Returns `null` if this method doesn't override any inherited member.
137+
ExecutableElement2? overriddenElement(
138+
InheritanceManager3 inheritanceManager,
139+
) {
140+
var element = declaredFragment?.element;
136141
if (element != null) {
137-
var enclosingElement = element.enclosingElement3;
138-
if (enclosingElement is InterfaceElement) {
139-
var name = Name(enclosingElement.library.source.uri, element.name);
140-
return InheritanceManager3().getInherited2(enclosingElement, name);
142+
var enclosingElement = element.enclosingElement2;
143+
if (enclosingElement is InterfaceElement2) {
144+
var name = Name(
145+
enclosingElement.library2.firstFragment.source.uri,
146+
element.name3!,
147+
);
148+
return inheritanceManager.getInherited4(enclosingElement, name);
141149
}
142150
}
143151
return null;
144152
}
145153

146-
/// Return the parameter of this method whose name matches the given [name],
147-
/// or `null` if there is no such parameter.
154+
/// Returns the parameter of this method whose name matches the given [name].
155+
///
156+
/// Returns `null` if there is no such parameter.
148157
FormalParameter? parameterNamed(String name) {
149158
var parameters = this.parameters;
150159
if (parameters != null) {
151160
for (var parameter in parameters.parameters) {
152-
if (parameter.declaredElement?.name == name) {
161+
if (parameter.declaredFragment?.name2 == name) {
153162
return parameter;
154163
}
155164
}
@@ -158,12 +167,14 @@ extension on MethodDeclaration {
158167
}
159168
}
160169

161-
extension on ExecutableElement {
162-
/// Return the parameter of this executable element whose name matches the
163-
/// given [name], or `null` if there is no such parameter.
164-
ParameterElement? parameterNamed(String name) {
165-
for (var parameter in parameters) {
166-
if (parameter.name == name) {
170+
extension on ExecutableElement2 {
171+
/// Returns the parameter of this executable element whose name matches the
172+
/// given [name]
173+
///
174+
/// Returns `null` if there is no such parameter.
175+
FormalParameterElement? parameterNamed(String name) {
176+
for (var parameter in formalParameters) {
177+
if (parameter.name3 == name) {
167178
return parameter;
168179
}
169180
}

0 commit comments

Comments
 (0)