Skip to content

Commit c1a26df

Browse files
kallentuCommit Queue
authored andcommitted
[analysis_server] Dot shorthands: Update data driven fixes.
Updated data-driven fixes such as fixing undefined methods, getters, fields, constructors, and adding missing type parameters on dot shorthand invocations. Added unit tests for element matching and the fixes. Bug: #60994 Change-Id: I2ba2aa82b32e6be0a8684f5c5eaf87c55363c2be Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/445109 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Kallen Tu <[email protected]>
1 parent e40618d commit c1a26df

File tree

8 files changed

+1244
-70
lines changed

8 files changed

+1244
-70
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,6 +1382,16 @@ extension on ArgumentList {
13821382
if (type is FunctionType) {
13831383
return type;
13841384
}
1385+
} else if (parent is DotShorthandInvocation) {
1386+
var type = parent.staticInvokeType;
1387+
if (type is FunctionType) {
1388+
return type;
1389+
}
1390+
} else if (parent is DotShorthandConstructorInvocation) {
1391+
if (parent.constructorName.element
1392+
case ConstructorElement constructorElement) {
1393+
return constructorElement.type;
1394+
}
13851395
}
13861396
return null;
13871397
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ class BulkFixProcessor {
9393
nonLintMultiProducerMap = {
9494
CompileTimeErrorCode.argumentTypeNotAssignable: [DataDriven.new],
9595
CompileTimeErrorCode.castToNonType: [DataDriven.new],
96+
CompileTimeErrorCode.dotShorthandUndefinedGetter: [DataDriven.new],
97+
CompileTimeErrorCode.dotShorthandUndefinedInvocation: [DataDriven.new],
9698
CompileTimeErrorCode.extendsNonClass: [DataDriven.new],
9799
// TODO(brianwilkerson): The following fix fails if an invocation of the
98100
// function is the argument that needs to be removed.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class DataDriven extends MultiCorrectionProducer {
3636
importedUris.add(uri.relativeUri);
3737
}
3838
}
39-
var matchers = ElementMatcher.matchersForNode(node, token);
39+
var matchers = ElementMatcher.matchersForNode(node, token, library);
4040
if (matchers.isEmpty) {
4141
// The node doesn't represent any element that can be transformed.
4242
return const [];

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

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

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';
7+
import 'package:analysis_server/src/services/correction/util.dart';
78
import 'package:analyzer/dart/ast/token.dart';
89
import 'package:analyzer/dart/element/element.dart'
910
show
@@ -141,15 +142,19 @@ class ElementMatcher {
141142
///
142143
/// The list will be empty if there are no appropriate matchers for the
143144
/// [node].
144-
static List<ElementMatcher> matchersForNode(AstNode? node, Token? nameToken) {
145+
static List<ElementMatcher> matchersForNode(
146+
AstNode? node,
147+
Token? nameToken,
148+
LibraryElement libraryElement,
149+
) {
145150
if (node == null) {
146151
return const [];
147152
}
148153
var importedUris = _importElementsForNode(node);
149154
if (importedUris == null) {
150155
return const [];
151156
}
152-
var builder = _MatcherBuilder(importedUris);
157+
var builder = _MatcherBuilder(importedUris, libraryElement);
153158
builder.buildMatchersForNode(node, nameToken);
154159
return builder.matchers.toList();
155160
}
@@ -189,7 +194,9 @@ class _MatcherBuilder {
189194

190195
final List<Uri> importedUris;
191196

192-
_MatcherBuilder(this.importedUris);
197+
final LibraryElement libraryElement;
198+
199+
_MatcherBuilder(this.importedUris, this.libraryElement);
193200

194201
void buildMatchersForNode(AstNode? node, Token? nameToken) {
195202
if (node is ArgumentList) {
@@ -251,6 +258,14 @@ class _MatcherBuilder {
251258
// } else if (parent is ExtensionOverride) {
252259
// // `TODO`(brianwilkerson) Determine whether this branch can be reached.
253260
// _buildFromExtensionOverride(parent);
261+
} else if (parent is DotShorthandConstructorInvocation) {
262+
_buildFromDotShorthand(parent, parent.constructorName.name, [
263+
ElementKind.constructorKind,
264+
]);
265+
} else if (parent is DotShorthandInvocation) {
266+
_buildFromDotShorthand(parent, parent.memberName.name, [
267+
ElementKind.methodKind,
268+
]);
254269
} else if (parent is FunctionExpressionInvocation) {
255270
_buildFromFunctionExpressionInvocation(parent);
256271
} else if (parent is InstanceCreationExpression) {
@@ -308,6 +323,22 @@ class _MatcherBuilder {
308323
);
309324
}
310325

326+
/// Build a matcher for the dot shorthand [node] being accessed.
327+
void _buildFromDotShorthand(
328+
AstNode node,
329+
String memberName,
330+
List<ElementKind> kinds,
331+
) {
332+
var typeElement = computeDotShorthandContextTypeElement(
333+
node,
334+
libraryElement,
335+
);
336+
var typeName = typeElement?.displayName;
337+
if (typeName != null) {
338+
_addMatcher(components: [memberName, typeName], kinds: kinds);
339+
}
340+
}
341+
311342
/// Build a matcher for the extension.
312343
void _buildFromExtensionOverride(ExtensionOverride node) {
313344
_addMatcher(
@@ -555,7 +586,26 @@ class _MatcherBuilder {
555586
// TODO(brianwilkerson): Use the static element, if there is one, in order to
556587
// get a more exact matcher.
557588
var parent = node.parent;
558-
if (parent is Label && parent.parent is NamedExpression) {
589+
if (parent is DotShorthandInvocation && node == parent.memberName) {
590+
var kinds = [ElementKind.methodKind];
591+
if (parent.typeArguments == null) {
592+
kinds.add(ElementKind.constructorKind);
593+
}
594+
_buildFromDotShorthand(parent, parent.memberName.name, kinds);
595+
} else if (parent is DotShorthandPropertyAccess &&
596+
node == parent.propertyName) {
597+
_buildFromDotShorthand(parent, parent.propertyName.name, [
598+
ElementKind.constantKind,
599+
ElementKind.fieldKind,
600+
ElementKind.getterKind,
601+
ElementKind.methodKind, // tear-off
602+
]);
603+
} else if (parent is DotShorthandConstructorInvocation &&
604+
node == parent.constructorName) {
605+
_buildFromDotShorthand(parent, parent.constructorName.name, [
606+
ElementKind.constructorKind,
607+
]);
608+
} else if (parent is Label && parent.parent is NamedExpression) {
559609
// The parent of the named expression is an argument list. Because we
560610
// don't represent parameters as elements, the element we need to match
561611
// against is the invocation containing those arguments.
@@ -609,6 +659,10 @@ class _MatcherBuilder {
609659
_buildFromFunctionExpressionInvocation(parent);
610660
} else if (parent is InstanceCreationExpression) {
611661
_buildFromInstanceCreationExpression(parent);
662+
} else if (parent is DotShorthandInvocation) {
663+
_buildFromDotShorthand(parent, parent.memberName.name, [
664+
ElementKind.methodKind,
665+
]);
612666
} else if (parent is MethodInvocation) {
613667
_buildFromMethodInvocation(parent);
614668
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,12 @@ class ModifyParameters extends Change<_Data> {
373373
} else if (grandParent is InstanceCreationExpression) {
374374
var argumentList = grandParent.argumentList;
375375
return _Data(argumentList);
376+
} else if (parent is NamedExpression &&
377+
greatGrandParent is DotShorthandInvocation) {
378+
return _Data(greatGrandParent.argumentList);
379+
} else if (parent is NamedExpression &&
380+
greatGrandParent is DotShorthandConstructorInvocation) {
381+
return _Data(greatGrandParent.argumentList);
376382
}
377383
return null;
378384
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,6 +1251,8 @@ final _builtInNonLintMultiGenerators = {
12511251
CreateClass.new,
12521252
ImportLibrary.forType,
12531253
],
1254+
CompileTimeErrorCode.dotShorthandUndefinedGetter: [DataDriven.new],
1255+
CompileTimeErrorCode.dotShorthandUndefinedInvocation: [DataDriven.new],
12541256
CompileTimeErrorCode.extendsNonClass: [
12551257
CreateClass.new,
12561258
DataDriven.new,

0 commit comments

Comments
 (0)