Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(813)

Side by Side Diff: pkg/analysis_server/lib/src/services/correction/fix_internal.dart

Issue 2946093002: Fix invalid initializing formal by adding a field (Closed)
Patch Set: cleanup Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | pkg/analysis_server/test/services/correction/fix_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 import 'dart:async'; 5 import 'dart:async';
6 import 'dart:collection'; 6 import 'dart:collection';
7 import 'dart:core'; 7 import 'dart:core';
8 8
9 import 'package:analysis_server/plugin/edit/fix/fix_core.dart'; 9 import 'package:analysis_server/plugin/edit/fix/fix_core.dart';
10 import 'package:analysis_server/plugin/edit/fix/fix_dart.dart'; 10 import 'package:analysis_server/plugin/edit/fix/fix_dart.dart';
(...skipping 378 matching lines...) Expand 10 before | Expand all | Expand 10 after
389 } 389 }
390 if (errorCode == StaticTypeWarningCode.UNDEFINED_SETTER) { 390 if (errorCode == StaticTypeWarningCode.UNDEFINED_SETTER) {
391 await _addFix_undefinedClassAccessor_useSimilar(); 391 await _addFix_undefinedClassAccessor_useSimilar();
392 await _addFix_createField(); 392 await _addFix_createField();
393 } 393 }
394 if (errorCode == CompileTimeErrorCode.UNDEFINED_NAMED_PARAMETER || 394 if (errorCode == CompileTimeErrorCode.UNDEFINED_NAMED_PARAMETER ||
395 errorCode == StaticWarningCode.UNDEFINED_NAMED_PARAMETER) { 395 errorCode == StaticWarningCode.UNDEFINED_NAMED_PARAMETER) {
396 await _addFix_convertFlutterChild(); 396 await _addFix_convertFlutterChild();
397 await _addFix_convertFlutterChildren(); 397 await _addFix_convertFlutterChildren();
398 } 398 }
399 if (errorCode ==
400 CompileTimeErrorCode.INITIALIZING_FORMAL_FOR_NON_EXISTENT_FIELD) {
401 await _addFix_createField_initializingFormal();
402 }
399 // lints 403 // lints
400 if (errorCode is LintCode) { 404 if (errorCode is LintCode) {
401 if (errorCode.name == LintNames.annotate_overrides) { 405 if (errorCode.name == LintNames.annotate_overrides) {
402 await _addFix_addOverrideAnnotation(); 406 await _addFix_addOverrideAnnotation();
403 } 407 }
404 if (errorCode.name == LintNames.avoid_annotating_with_dynamic) { 408 if (errorCode.name == LintNames.avoid_annotating_with_dynamic) {
405 await _addFix_removeTypeName(); 409 await _addFix_removeTypeName();
406 } 410 }
407 if (errorCode.name == LintNames.avoid_init_to_null) { 411 if (errorCode.name == LintNames.avoid_init_to_null) {
408 await _addFix_removeInitializer(); 412 await _addFix_removeInitializer();
(...skipping 765 matching lines...) Expand 10 before | Expand all | Expand 10 after
1174 utils.targetClassElement = targetClassElement; 1178 utils.targetClassElement = targetClassElement;
1175 // prepare target ClassDeclaration 1179 // prepare target ClassDeclaration
1176 AstNode targetTypeNode = getParsedClassElementNode(targetClassElement); 1180 AstNode targetTypeNode = getParsedClassElementNode(targetClassElement);
1177 if (targetTypeNode is! ClassDeclaration) { 1181 if (targetTypeNode is! ClassDeclaration) {
1178 return; 1182 return;
1179 } 1183 }
1180 ClassDeclaration targetClassNode = targetTypeNode; 1184 ClassDeclaration targetClassNode = targetTypeNode;
1181 // prepare location 1185 // prepare location
1182 ClassMemberLocation targetLocation = 1186 ClassMemberLocation targetLocation =
1183 utils.prepareNewFieldLocation(targetClassNode); 1187 utils.prepareNewFieldLocation(targetClassNode);
1184 // build method source 1188 // build field source
1185 Source targetSource = targetClassElement.source; 1189 Source targetSource = targetClassElement.source;
1186 String targetFile = targetSource.fullName; 1190 String targetFile = targetSource.fullName;
1187 DartChangeBuilder changeBuilder = new DartChangeBuilder(driver); 1191 DartChangeBuilder changeBuilder = new DartChangeBuilder(driver);
1188 await changeBuilder.addFileEdit(targetFile, targetSource.modificationStamp, 1192 await changeBuilder.addFileEdit(targetFile, targetSource.modificationStamp,
1189 (DartFileEditBuilder builder) { 1193 (DartFileEditBuilder builder) {
1190 Expression fieldTypeNode = climbPropertyAccess(nameNode); 1194 Expression fieldTypeNode = climbPropertyAccess(nameNode);
1191 DartType fieldType = _inferUndefinedExpressionType(fieldTypeNode); 1195 DartType fieldType = _inferUndefinedExpressionType(fieldTypeNode);
1192 builder.addInsertion(targetLocation.offset, (DartEditBuilder builder) { 1196 builder.addInsertion(targetLocation.offset, (DartEditBuilder builder) {
1193 builder.write(targetLocation.prefix); 1197 builder.write(targetLocation.prefix);
1194 builder.writeFieldDeclaration(name, 1198 builder.writeFieldDeclaration(name,
1195 isStatic: staticModifier, 1199 isStatic: staticModifier,
1196 nameGroupName: 'NAME', 1200 nameGroupName: 'NAME',
1197 type: fieldType, 1201 type: fieldType,
1198 typeGroupName: 'TYPE'); 1202 typeGroupName: 'TYPE');
1199 builder.write(targetLocation.suffix); 1203 builder.write(targetLocation.suffix);
1200 }); 1204 });
1201 }); 1205 });
1202 _addFixFromBuilder(changeBuilder, DartFixKind.CREATE_FIELD, args: [name]); 1206 _addFixFromBuilder(changeBuilder, DartFixKind.CREATE_FIELD, args: [name]);
1203 } 1207 }
1204 1208
1209 Future<Null> _addFix_createField_initializingFormal() async {
1210 //
1211 // Ensure that we are in an initializing formal parameter.
1212 //
1213 FieldFormalParameter parameter =
1214 node.getAncestor((node) => node is FieldFormalParameter);
1215 if (parameter == null) {
1216 return;
1217 }
1218 ClassDeclaration targetClassNode =
1219 parameter.getAncestor((node) => node is ClassDeclaration);
1220 if (targetClassNode == null) {
1221 return;
1222 }
1223 SimpleIdentifier nameNode = parameter.identifier;
1224 String name = nameNode.name;
1225 ClassMemberLocation targetLocation =
1226 utils.prepareNewFieldLocation(targetClassNode);
1227 //
1228 // Add proposal.
1229 //
1230 DartChangeBuilder changeBuilder = new DartChangeBuilder(driver);
1231 await changeBuilder.addFileEdit(file, fileStamp,
1232 (DartFileEditBuilder builder) {
1233 DartType fieldType = parameter.type?.type;
1234 builder.addInsertion(targetLocation.offset, (DartEditBuilder builder) {
1235 builder.write(targetLocation.prefix);
1236 builder.writeFieldDeclaration(name,
1237 nameGroupName: 'NAME', type: fieldType, typeGroupName: 'TYPE');
1238 builder.write(targetLocation.suffix);
1239 });
1240 });
1241 _addFixFromBuilder(changeBuilder, DartFixKind.CREATE_FIELD, args: [name]);
1242 }
1243
1205 Future<Null> _addFix_createFunction_forFunctionType() async { 1244 Future<Null> _addFix_createFunction_forFunctionType() async {
1206 if (node is SimpleIdentifier) { 1245 if (node is SimpleIdentifier) {
1207 SimpleIdentifier nameNode = node as SimpleIdentifier; 1246 SimpleIdentifier nameNode = node as SimpleIdentifier;
1208 // prepare argument expression (to get parameter) 1247 // prepare argument expression (to get parameter)
1209 ClassElement targetElement; 1248 ClassElement targetElement;
1210 Expression argument; 1249 Expression argument;
1211 { 1250 {
1212 Expression target = getQualifiedPropertyTarget(node); 1251 Expression target = getQualifiedPropertyTarget(node);
1213 if (target != null) { 1252 if (target != null) {
1214 DartType targetType = target.bestType; 1253 DartType targetType = target.bestType;
(...skipping 1960 matching lines...) Expand 10 before | Expand all | Expand 10 after
3175 } 3214 }
3176 } 3215 }
3177 } 3216 }
3178 3217
3179 void _updateList(Iterable<Element> elements) { 3218 void _updateList(Iterable<Element> elements) {
3180 for (Element element in elements) { 3219 for (Element element in elements) {
3181 _update(element); 3220 _update(element);
3182 } 3221 }
3183 } 3222 }
3184 } 3223 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analysis_server/test/services/correction/fix_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698