Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add codefix to make non-reassignable variables reassignable #27593

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -4683,5 +4683,13 @@
"Generate types for all packages without types": {
"category": "Message",
"code": 95068
},
"Make all const or readonly expressions reassignable": {
"category": "Message",
"code": 95069
},
"Remove '{0}' modifier": {
"category": "Message",
"code": 95070
}
}
51 changes: 51 additions & 0 deletions src/services/codefixes/fixConvertConstantVariableOrProperty.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* @internal */
namespace ts.codefix {
const fixId = "fixConvertConstantVariableOrProperty";
const errorCodes = [
Diagnostics.Cannot_assign_to_0_because_it_is_a_constant_or_a_read_only_property.code
];
registerCodeFix({
errorCodes,
getCodeActions(context) {
const { sourceFile, span, program } = context;
const declaration = getDeclaration(sourceFile, span.start, program.getTypeChecker());
if (!declaration) return undefined;
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, declaration));
const type = declaration.kind === SyntaxKind.VariableDeclaration ? "const" : "readonly";
return [createCodeFixAction(fixId, changes, [Diagnostics.Remove_0_modifier, type], fixId, Diagnostics.Make_all_const_or_readonly_expressions_reassignable)];
},
fixIds: [fixId],
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => {
const declaration = getDeclaration(diag.file, diag.start, context.program.getTypeChecker());
if (declaration) doChange(changes, context.sourceFile, declaration);
}),
});
function getDeclaration(sourceFile: SourceFile, pos: number, checker: TypeChecker): Declaration | undefined {
const node = getTokenAtPosition(sourceFile, pos);
const symbol = checker.getSymbolAtLocation(node);
if (symbol) {
return symbol.valueDeclaration;
}
}

function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, node: Declaration) {
if (node.kind === SyntaxKind.VariableDeclaration) {
const oldDeclarationList = node.parent as VariableDeclarationList;
const declarationList = createVariableDeclarationList(oldDeclarationList.declarations, NodeFlags.Let);
changes.replaceNode(sourceFile, oldDeclarationList, declarationList);
}
else if (node.kind === SyntaxKind.PropertyDeclaration) {
const readonlyToken = findAnyChildOfKind(node, SyntaxKind.ReadonlyKeyword);
if (readonlyToken) {
changes.delete(sourceFile, readonlyToken);
}
}
}

function findAnyChildOfKind(node: Node, kind: SyntaxKind): Node | undefined {
return node.forEachChild(child => {
if (child.kind === kind) return child;
else findAnyChildOfKind(child, kind);
});
}
}
1 change: 1 addition & 0 deletions src/services/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"codefixes/fixClassDoesntImplementInheritedAbstractMember.ts",
"codefixes/fixClassSuperMustPrecedeThisAccess.ts",
"codefixes/fixConstructorForDerivedNeedSuperCall.ts",
"codefixes/fixConvertConstantVariableOrProperty.ts",
"codefixes/fixExtendsInterfaceBecomesImplements.ts",
"codefixes/fixForgottenThisPropertyAccess.ts",
"codefixes/fixUnusedIdentifier.ts",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// <reference path='fourslash.ts' />

////const variable = 5;
////variable = 4;

verify.codeFix({
description: "Remove 'const' modifier",
newFileContent:
`let variable = 5;
variable = 4;`,
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/// <reference path='fourslash.ts' />

////class Test {
//// readonly prop = 5;
////}
////let testInstance = new Test();
////testInstance.prop = 3;

verify.codeFix({
description: "Remove 'readonly' modifier",
newFileContent:
`class Test {
prop = 5;
}
let testInstance = new Test();
testInstance.prop = 3;`,
});