Skip to content

Commit eb60f05

Browse files
bwilkersoncommit-bot@chromium.org
authored andcommitted
Support a fix to add a setter, both in extensions and elsewhere
Change-Id: I94d7ec13a2bcf73b120be18facf473a4710a6e28 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/116053 Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent e227ffa commit eb60f05

File tree

7 files changed

+619
-2
lines changed

7 files changed

+619
-2
lines changed

Diff for: pkg/analysis_server/lib/src/services/correction/fix.dart

+2
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,8 @@ class DartFixKind {
219219
const FixKind('CREATE_MIXIN', 50, "Create mixin '{0}'");
220220
static const CREATE_NO_SUCH_METHOD = const FixKind(
221221
'CREATE_NO_SUCH_METHOD', 49, "Create 'noSuchMethod' method");
222+
static const CREATE_SETTER =
223+
const FixKind('CREATE_SETTER', 50, "Create setter '{0}'");
222224
static const EXTEND_CLASS_FOR_MIXIN =
223225
const FixKind('EXTEND_CLASS_FOR_MIXIN', 50, "Extend the class '{0}'");
224226
static const IMPORT_ASYNC =

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

+88
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,7 @@ class FixProcessor extends BaseProcessor {
461461
await _addFix_createGetter();
462462
await _addFix_createFunction_forFunctionType();
463463
await _addFix_createMixin();
464+
await _addFix_createSetter();
464465
await _addFix_importLibrary_withType();
465466
await _addFix_importLibrary_withExtension();
466467
await _addFix_importLibrary_withFunction();
@@ -532,9 +533,11 @@ class FixProcessor extends BaseProcessor {
532533
if (errorCode == StaticTypeWarningCode.UNDEFINED_SETTER) {
533534
await _addFix_undefinedClassAccessor_useSimilar();
534535
await _addFix_createField();
536+
await _addFix_createSetter();
535537
}
536538
if (errorCode == CompileTimeErrorCode.UNDEFINED_EXTENSION_SETTER) {
537539
await _addFix_undefinedClassAccessor_useSimilar();
540+
await _addFix_createSetter();
538541
}
539542
if (errorCode == CompileTimeErrorCode.UNDEFINED_NAMED_PARAMETER) {
540543
await _addFix_convertFlutterChild();
@@ -2441,6 +2444,91 @@ class FixProcessor extends BaseProcessor {
24412444
}
24422445
}
24432446

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+
24442532
Future<void> _addFix_extendClassForMixin() async {
24452533
ClassDeclaration declaration =
24462534
node.thisOrAncestorOfType<ClassDeclaration>();

0 commit comments

Comments
 (0)