Skip to content

Commit 1afbc51

Browse files
lrhnCommit Queue
authored andcommitted
Add type support to data-fix replacedBy.
Still missing extension type support. Change-Id: Ida582e6f5832d2ee0a648f0aecb9e8c50c0ee8e1 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/432982 Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Morgan :) <[email protected]> Commit-Queue: Lasse Nielsen <[email protected]>
1 parent 28c833c commit 1afbc51

File tree

19 files changed

+1304
-266
lines changed

19 files changed

+1304
-266
lines changed

pkg/_fe_analyzer_shared/lib/src/base/errors.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ typedef ErrorType = DiagnosticType;
3232
@AnalyzerPublicApi(message: 'exported by package:analyzer/error/error.dart')
3333
abstract class DiagnosticCode {
3434
/// Regular expression for identifying positional arguments in error messages.
35-
static final RegExp _positionalArgumentRegExp = new RegExp(r'{(\d+)\}');
35+
static final RegExp _positionalArgumentRegExp = new RegExp(r'\{(\d+)\}');
3636

3737
/**
3838
* The name of the error code.

pkg/analysis_server/lib/src/services/completion/yaml/fix_data_generator.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class FixDataGenerator extends YamlCompletionGenerator {
6565
'constructor': EmptyProducer(),
6666
'enum': EmptyProducer(),
6767
'extension': EmptyProducer(),
68+
'extensionType': EmptyProducer(),
6869
'field': EmptyProducer(),
6970
'function': EmptyProducer(),
7071
'getter': EmptyProducer(),
@@ -76,6 +77,7 @@ class FixDataGenerator extends YamlCompletionGenerator {
7677
'inClass': EmptyProducer(),
7778
'inEnum': EmptyProducer(),
7879
'inExtension': EmptyProducer(),
80+
'inExtensionType': EmptyProducer(),
7981
'inMixin': EmptyProducer(),
8082
});
8183

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

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,16 @@ class ElementDescriptor {
4747
// TODO(brianwilkerson): Check the resolved element, if one exists, for more
4848
// accurate results.
4949
return switch (kind) {
50-
ElementKind.classKind =>
51-
// TODO(brianwilkerson): Handle this case.
52-
false,
50+
ElementKind.classKind => _matchesType(node),
5351
ElementKind.constantKind =>
5452
// TODO(brianwilkerson): Handle this case.
5553
false,
5654
ElementKind.constructorKind => _matchesConstructor(node),
57-
ElementKind.enumKind =>
58-
// TODO(brianwilkerson): Handle this case.
59-
false,
55+
ElementKind.enumKind => _matchesType(node),
6056
ElementKind.extensionKind =>
6157
// TODO(brianwilkerson): Handle this case.
6258
false,
59+
ElementKind.extensionTypeKind => _matchesType(node),
6360
ElementKind.fieldKind =>
6461
// TODO(brianwilkerson): Handle this case.
6562
false,
@@ -68,15 +65,11 @@ class ElementDescriptor {
6865
// TODO(brianwilkerson): Handle this case.
6966
false,
7067
ElementKind.methodKind => _matchesMethod(node),
71-
ElementKind.mixinKind =>
72-
// TODO(brianwilkerson): Handle this case.
73-
false,
68+
ElementKind.mixinKind => _matchesType(node),
7469
ElementKind.setterKind =>
7570
// TODO(brianwilkerson): Handle this case.
7671
false,
77-
ElementKind.typedefKind =>
78-
// TODO(brianwilkerson): Handle this case.
79-
false,
72+
ElementKind.typedefKind => _matchesType(node),
8073
ElementKind.variableKind =>
8174
// TODO(brianwilkerson): Handle this case.
8275
false,
@@ -167,6 +160,22 @@ class ElementDescriptor {
167160
return false;
168161
}
169162

163+
bool _matchesType(AstNode node) {
164+
if (components.length > 1) {
165+
return false;
166+
}
167+
var name = components[0];
168+
if (node is Identifier) {
169+
// A plain or prefixed identifier with the correct name.
170+
var typeName = _nameFromIdentifier(node);
171+
return name == typeName;
172+
}
173+
if (node is NamedType) {
174+
return name == node.name.lexeme;
175+
}
176+
return false;
177+
}
178+
170179
String _nameFromIdentifier(Identifier identifier) {
171180
if (identifier is SimpleIdentifier) {
172181
return identifier.name;

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

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,29 @@
44

55
/// An indication of the kind of an element.
66
enum ElementKind {
7-
classKind,
8-
constantKind,
9-
constructorKind,
10-
enumKind,
11-
extensionKind,
12-
fieldKind,
13-
functionKind,
14-
getterKind,
15-
methodKind,
16-
mixinKind,
17-
setterKind,
18-
typedefKind,
19-
variableKind,
20-
}
7+
classKind('class'),
8+
constantKind('constant'),
9+
constructorKind('constructor'),
10+
enumKind('enum'),
11+
extensionKind('extension'),
12+
extensionTypeKind('extensionType'),
13+
fieldKind('field'),
14+
functionKind('function'),
15+
getterKind('getter'),
16+
methodKind('method'),
17+
mixinKind('mixin'),
18+
setterKind('setter'),
19+
typedefKind('typedef'),
20+
variableKind('variable');
2121

22-
extension ElementKindUtilities on ElementKind {
23-
/// Return a human readable name for the kind.
24-
String get displayName {
25-
return switch (this) {
26-
ElementKind.classKind => 'class',
27-
ElementKind.constantKind => 'constant',
28-
ElementKind.constructorKind => 'constructor',
29-
ElementKind.enumKind => 'enum',
30-
ElementKind.extensionKind => 'extension',
31-
ElementKind.fieldKind => 'field',
32-
ElementKind.functionKind => 'function',
33-
ElementKind.getterKind => 'getter',
34-
ElementKind.methodKind => 'method',
35-
ElementKind.mixinKind => 'mixin',
36-
ElementKind.setterKind => 'setter',
37-
ElementKind.typedefKind => 'typedef',
38-
ElementKind.variableKind => 'variable',
39-
};
40-
}
22+
/// A human readable name for the kind.
23+
final String displayName;
24+
const ElementKind(this.displayName);
4125

42-
/// Return the element kind corresponding to the given [name].
26+
/// The element kind corresponding to the given [name].
4327
static ElementKind? fromName(String name) {
44-
for (var kind in ElementKind.values) {
45-
if (kind.toString() == 'ElementKind.${name}Kind') {
28+
for (var kind in values) {
29+
if (kind.displayName == name) {
4630
return kind;
4731
}
4832
}

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

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class ElementMatcher {
2828
final List<String> components;
2929

3030
/// A list of the kinds of elements that are appropriate for some given
31-
/// location in the code An empty list represents all kinds rather than no
31+
/// location in the code. An empty list represents all kinds rather than no
3232
/// kinds.
3333
final List<ElementKind> validKinds;
3434

@@ -291,12 +291,21 @@ class _MatcherBuilder {
291291
// get a more exact matcher.
292292
// TODO(brianwilkerson): Use 'new' for the name of the unnamed constructor.
293293
var constructorName = node.name?.name ?? ''; // ?? 'new';
294-
var className = node.type.name.lexeme;
294+
var typeName = node.type.name.lexeme;
295295
_addMatcher(
296-
components: [constructorName, className],
296+
components: [constructorName, typeName],
297297
kinds: const [ElementKind.constructorKind],
298298
);
299-
_addMatcher(components: [className], kinds: const [ElementKind.classKind]);
299+
_addMatcher(
300+
components: [typeName],
301+
kinds: const [
302+
ElementKind.classKind,
303+
ElementKind.enumKind,
304+
ElementKind.extensionTypeKind,
305+
ElementKind.typedefKind,
306+
ElementKind.mixinKind, // Can't *yet* have factory constructors.
307+
],
308+
);
300309
}
301310

302311
/// Build a matcher for the extension.
@@ -369,10 +378,14 @@ class _MatcherBuilder {
369378
kinds: [
370379
ElementKind.classKind,
371380
ElementKind.constructorKind,
381+
ElementKind.enumKind,
372382
ElementKind.extensionKind,
383+
ElementKind.extensionTypeKind,
373384
ElementKind.functionKind,
374385
ElementKind.getterKind,
375386
ElementKind.methodKind,
387+
ElementKind.mixinKind,
388+
ElementKind.typedefKind,
376389
],
377390
);
378391
}
@@ -391,6 +404,7 @@ class _MatcherBuilder {
391404
kinds: const [
392405
ElementKind.classKind,
393406
ElementKind.enumKind,
407+
ElementKind.extensionTypeKind,
394408
ElementKind.mixinKind,
395409
ElementKind.typedefKind,
396410
],
@@ -413,20 +427,17 @@ class _MatcherBuilder {
413427
// get a more exact matcher.
414428
var prefix = node.prefix;
415429
if (prefix.element is PrefixElement) {
416-
var parent = node.parent;
417-
if ((parent is NamedType && parent.parent is! ConstructorName) ||
418-
(parent is PropertyAccess && parent.target == node)) {
419-
_addMatcher(
420-
components: [node.identifier.name],
421-
kinds: const [
422-
ElementKind.classKind,
423-
ElementKind.enumKind,
424-
ElementKind.extensionKind,
425-
ElementKind.mixinKind,
426-
ElementKind.typedefKind,
427-
],
428-
);
429-
}
430+
_addMatcher(
431+
components: [node.identifier.name],
432+
kinds: const [
433+
ElementKind.classKind,
434+
ElementKind.enumKind,
435+
ElementKind.extensionKind,
436+
ElementKind.extensionTypeKind,
437+
ElementKind.mixinKind,
438+
ElementKind.typedefKind,
439+
],
440+
);
430441
_addMatcher(
431442
components: [node.identifier.name],
432443
kinds: const [

0 commit comments

Comments
 (0)