Index: pkg/analysis_server/lib/src/services/correction/fix_internal.dart |
diff --git a/pkg/analysis_server/lib/src/services/correction/fix_internal.dart b/pkg/analysis_server/lib/src/services/correction/fix_internal.dart |
index 7b0f8a179eed9b414a9379e03523e84e1627e82f..cd4fd648633ad9b3353289bf7f69156a6c2a8b68 100644 |
--- a/pkg/analysis_server/lib/src/services/correction/fix_internal.dart |
+++ b/pkg/analysis_server/lib/src/services/correction/fix_internal.dart |
@@ -396,6 +396,10 @@ class FixProcessor { |
await _addFix_convertFlutterChild(); |
await _addFix_convertFlutterChildren(); |
} |
+ if (errorCode == |
+ CompileTimeErrorCode.INITIALIZING_FORMAL_FOR_NON_EXISTENT_FIELD) { |
+ await _addFix_createField_initializingFormal(); |
+ } |
// lints |
if (errorCode is LintCode) { |
if (errorCode.name == LintNames.annotate_overrides) { |
@@ -1181,7 +1185,7 @@ class FixProcessor { |
// prepare location |
ClassMemberLocation targetLocation = |
utils.prepareNewFieldLocation(targetClassNode); |
- // build method source |
+ // build field source |
Source targetSource = targetClassElement.source; |
String targetFile = targetSource.fullName; |
DartChangeBuilder changeBuilder = new DartChangeBuilder(driver); |
@@ -1202,6 +1206,41 @@ class FixProcessor { |
_addFixFromBuilder(changeBuilder, DartFixKind.CREATE_FIELD, args: [name]); |
} |
+ Future<Null> _addFix_createField_initializingFormal() async { |
+ // |
+ // Ensure that we are in an initializing formal parameter. |
+ // |
+ FieldFormalParameter parameter = |
+ node.getAncestor((node) => node is FieldFormalParameter); |
+ if (parameter == null) { |
+ return; |
+ } |
+ ClassDeclaration targetClassNode = |
+ parameter.getAncestor((node) => node is ClassDeclaration); |
+ if (targetClassNode == null) { |
+ return; |
+ } |
+ SimpleIdentifier nameNode = parameter.identifier; |
+ String name = nameNode.name; |
+ ClassMemberLocation targetLocation = |
+ utils.prepareNewFieldLocation(targetClassNode); |
+ // |
+ // Add proposal. |
+ // |
+ DartChangeBuilder changeBuilder = new DartChangeBuilder(driver); |
+ await changeBuilder.addFileEdit(file, fileStamp, |
+ (DartFileEditBuilder builder) { |
+ DartType fieldType = parameter.type?.type; |
+ builder.addInsertion(targetLocation.offset, (DartEditBuilder builder) { |
+ builder.write(targetLocation.prefix); |
+ builder.writeFieldDeclaration(name, |
+ nameGroupName: 'NAME', type: fieldType, typeGroupName: 'TYPE'); |
+ builder.write(targetLocation.suffix); |
+ }); |
+ }); |
+ _addFixFromBuilder(changeBuilder, DartFixKind.CREATE_FIELD, args: [name]); |
+ } |
+ |
Future<Null> _addFix_createFunction_forFunctionType() async { |
if (node is SimpleIdentifier) { |
SimpleIdentifier nameNode = node as SimpleIdentifier; |