Fix for crash in WrapInText when the parameter is not InterfaceType.
There was a crash in the logs.
_TypeError: type 'GenericFunctionTypeElementImpl' is not a subtype of type 'ClassElement'
at WrapInText.compute (wrap_in_text.dart:26 )
at FixProcessor._addFromProducers.compute (fix_internal.dart:4506 )
at FixProcessor._addFromProducers (fix_internal.dart:4520 )
at FixProcessor.compute (fix_internal.dart:762 )
at DartFixContributor.computeFixes (fix_internal.dart:82 )
R=brianwilkerson@google.com, pquitslund@google.com
Change-Id: I187b3348a212391cc6c64ab2c412653bd2319334
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/141820
Reviewed-by: Phil Quitslund <pquitslund@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/wrap_in_text.dart b/pkg/analysis_server/lib/src/services/correction/dart/wrap_in_text.dart
index 7f26c42..e391f54 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/wrap_in_text.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/wrap_in_text.dart
@@ -5,11 +5,15 @@
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer/dart/ast/ast.dart';
+import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_dart.dart';
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
import 'package:analyzer_plugin/utilities/range_factory.dart';
class WrapInText extends CorrectionProducer {
+ ParameterElement _parameterElement;
+ Expression _stringExpression;
+
@override
FixKind get fixKind => DartFixKind.WRAP_IN_TEXT;
@@ -18,29 +22,34 @@
//
// Extract the information needed to build the edit.
//
- var value = _findStringToWrap(node);
- if (value == null) {
+ _extractContextInformation(node);
+ if (_parameterElement == null || _stringExpression == null) {
return;
}
- var parameter = (value.parent as Expression).staticParameterElement;
- if (parameter == null || !flutter.isWidget(parameter.type.element)) {
+ if (!flutter.isWidgetType(_parameterElement.type)) {
return;
}
+
//
// Extract the information needed to build the edit.
//
- var literalSource = utils.getNodeText(value);
+ var stringExpressionCode = utils.getNodeText(_stringExpression);
+
//
// Build the edit.
//
await builder.addFileEdit(file, (DartFileEditBuilder builder) {
- builder.addSimpleReplacement(range.node(value), 'Text($literalSource)');
+ builder.addSimpleReplacement(
+ range.node(_stringExpression),
+ 'Text($stringExpressionCode)',
+ );
});
}
- /// Return the expression that should be wrapped in an invocation of the
- /// constructor for `Text`.
- Expression _findStringToWrap(AstNode node) {
+ /// Set the `String` typed named expression to [_stringExpression], and the
+ /// corresponding parameter to [_parameterElement]. Leave the fields `null`
+ /// if not a named argument, or not a `String` typed expression.
+ void _extractContextInformation(AstNode node) {
if (node is SimpleIdentifier) {
var label = node.parent;
if (label is Label) {
@@ -48,11 +57,11 @@
if (namedExpression is NamedExpression) {
var expression = namedExpression.expression;
if (expression.staticType.isDartCoreString) {
- return expression;
+ _parameterElement = node.staticElement;
+ _stringExpression = expression;
}
}
}
}
- return null;
}
}
diff --git a/pkg/analysis_server/test/src/services/correction/fix/wrap_in_text_test.dart b/pkg/analysis_server/test/src/services/correction/fix/wrap_in_text_test.dart
index a04df7c..d3b0b24 100644
--- a/pkg/analysis_server/test/src/services/correction/fix/wrap_in_text_test.dart
+++ b/pkg/analysis_server/test/src/services/correction/fix/wrap_in_text_test.dart
@@ -44,7 +44,20 @@
await assertNoFix();
}
- Future<void> test_notWidget() async {
+ Future<void> test_parameterType_notClass() async {
+ await resolveTestUnit('''
+typedef F = void Function();
+
+void foo({F a}) {}
+
+void bar() {
+ foo(a: '');
+}
+''');
+ await assertNoFix();
+ }
+
+ Future<void> test_parameterType_notWidget() async {
await resolveTestUnit('''
void f(int i) {
f('a');