@@ -461,6 +461,7 @@ class FixProcessor extends BaseProcessor {
461
461
await _addFix_createGetter ();
462
462
await _addFix_createFunction_forFunctionType ();
463
463
await _addFix_createMixin ();
464
+ await _addFix_createSetter ();
464
465
await _addFix_importLibrary_withType ();
465
466
await _addFix_importLibrary_withExtension ();
466
467
await _addFix_importLibrary_withFunction ();
@@ -532,9 +533,11 @@ class FixProcessor extends BaseProcessor {
532
533
if (errorCode == StaticTypeWarningCode .UNDEFINED_SETTER ) {
533
534
await _addFix_undefinedClassAccessor_useSimilar ();
534
535
await _addFix_createField ();
536
+ await _addFix_createSetter ();
535
537
}
536
538
if (errorCode == CompileTimeErrorCode .UNDEFINED_EXTENSION_SETTER ) {
537
539
await _addFix_undefinedClassAccessor_useSimilar ();
540
+ await _addFix_createSetter ();
538
541
}
539
542
if (errorCode == CompileTimeErrorCode .UNDEFINED_NAMED_PARAMETER ) {
540
543
await _addFix_convertFlutterChild ();
@@ -2441,6 +2444,91 @@ class FixProcessor extends BaseProcessor {
2441
2444
}
2442
2445
}
2443
2446
2447
+ Future <void > _addFix_createSetter () async {
2448
+ if (node is ! SimpleIdentifier ) {
2449
+ return ;
2450
+ }
2451
+ SimpleIdentifier nameNode = node;
2452
+ if (! nameNode.inSetterContext ()) {
2453
+ return ;
2454
+ }
2455
+ // prepare target
2456
+ Expression target;
2457
+ {
2458
+ AstNode nameParent = nameNode.parent;
2459
+ if (nameParent is PrefixedIdentifier ) {
2460
+ target = nameParent.prefix;
2461
+ } else if (nameParent is PropertyAccess ) {
2462
+ target = nameParent.realTarget;
2463
+ }
2464
+ }
2465
+ // prepare target element
2466
+ bool staticModifier = false ;
2467
+ Element targetElement;
2468
+ if (target is ExtensionOverride ) {
2469
+ targetElement = target.staticElement;
2470
+ } else if (target is Identifier &&
2471
+ target.staticElement is ExtensionElement ) {
2472
+ targetElement = target.staticElement;
2473
+ staticModifier = true ;
2474
+ } else if (target != null ) {
2475
+ // prepare target interface type
2476
+ DartType targetType = target.staticType;
2477
+ if (targetType is ! InterfaceType ) {
2478
+ return ;
2479
+ }
2480
+ targetElement = targetType.element;
2481
+ // maybe static
2482
+ if (target is Identifier ) {
2483
+ Identifier targetIdentifier = target;
2484
+ Element targetElement = targetIdentifier.staticElement;
2485
+ staticModifier = targetElement? .kind == ElementKind .CLASS ;
2486
+ }
2487
+ } else {
2488
+ targetElement =
2489
+ getEnclosingClassElement (node) ?? getEnclosingExtensionElement (node);
2490
+ if (targetElement == null ) {
2491
+ return ;
2492
+ }
2493
+ staticModifier = _inStaticContext ();
2494
+ }
2495
+ if (targetElement.librarySource.isInSystemLibrary) {
2496
+ return ;
2497
+ }
2498
+ // prepare target declaration
2499
+ var targetDeclarationResult =
2500
+ await sessionHelper.getElementDeclaration (targetElement);
2501
+ if (targetDeclarationResult.node is ! ClassOrMixinDeclaration &&
2502
+ targetDeclarationResult.node is ! ExtensionDeclaration ) {
2503
+ return ;
2504
+ }
2505
+ CompilationUnitMember targetNode = targetDeclarationResult.node;
2506
+ // prepare location
2507
+ ClassMemberLocation targetLocation = CorrectionUtils (
2508
+ targetDeclarationResult.resolvedUnit)
2509
+ .prepareNewGetterLocation (targetNode); // Rename to "AccessorLocation"
2510
+ // build method source
2511
+ Source targetSource = targetElement.source;
2512
+ String targetFile = targetSource.fullName;
2513
+ String name = nameNode.name;
2514
+ var changeBuilder = _newDartChangeBuilder ();
2515
+ await changeBuilder.addFileEdit (targetFile, (DartFileEditBuilder builder) {
2516
+ builder.addInsertion (targetLocation.offset, (DartEditBuilder builder) {
2517
+ Expression parameterTypeNode = climbPropertyAccess (nameNode);
2518
+ DartType parameterType =
2519
+ _inferUndefinedExpressionType (parameterTypeNode);
2520
+ builder.write (targetLocation.prefix);
2521
+ builder.writeSetterDeclaration (name,
2522
+ isStatic: staticModifier,
2523
+ nameGroupName: 'NAME' ,
2524
+ parameterType: parameterType,
2525
+ parameterTypeGroupName: 'TYPE' );
2526
+ builder.write (targetLocation.suffix);
2527
+ });
2528
+ });
2529
+ _addFixFromBuilder (changeBuilder, DartFixKind .CREATE_SETTER , args: [name]);
2530
+ }
2531
+
2444
2532
Future <void > _addFix_extendClassForMixin () async {
2445
2533
ClassDeclaration declaration =
2446
2534
node.thisOrAncestorOfType <ClassDeclaration >();
0 commit comments