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 3 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
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -4683,5 +4683,9 @@
"Generate types for all packages without types": {
"category": "Message",
"code": 95068
},
"Make all const or readonly expressions reassignable": {
"category": "Message",
"code": 95069
}
}
69 changes: 69 additions & 0 deletions src/services/codefixes/fixConvertConstantVariableOrProperty.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* @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 } = context;
const { declaration, kind } = getDeclaration(sourceFile, span.start);
if (!declaration || !kind) return undefined;

const changes = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, declaration, kind));
const type = kind === SyntaxKind.ConstKeyword ? "const" : "readonly";
const changeTo = type === "const" ? "let" : "non-readonly";
muradkhan101 marked this conversation as resolved.
Show resolved Hide resolved
return [createCodeFixAction(fixId, changes, [Diagnostics.Change_0_to_1, type, changeTo], fixId, Diagnostics.Make_all_const_or_readonly_expressions_reassignable)];
},
fixIds: [fixId],
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => {
const { declaration, kind } = getDeclaration(diag.file, diag.start);
if (declaration && kind) doChange(changes, context.sourceFile, declaration, kind);
}),
});
function getDeclaration(sourceFile: SourceFile, pos: number): {declaration: VariableStatement | PropertyDeclaration | undefined, kind: SyntaxKind | undefined } {
const node = getTokenAtPosition(sourceFile, pos);
const tokenName = node.getText();
let declaration: VariableStatement | PropertyDeclaration | undefined;
let kind: SyntaxKind | undefined;
// The first parent of the property reassignment has [class].[prop]
if (node.parent.getText().includes(".")) {
muradkhan101 marked this conversation as resolved.
Show resolved Hide resolved
declaration = findChild(sourceFile, (node) => node.kind === SyntaxKind.PropertyDeclaration
muradkhan101 marked this conversation as resolved.
Show resolved Hide resolved
&& node.getText().includes(tokenName));
kind = SyntaxKind.PropertyDeclaration;
}
// The first parent with const reaassignment has [variable] = [value]
else if (node.parent.getText().includes("=")) {
declaration = findChild(sourceFile, (node) => node.kind === SyntaxKind.VariableStatement
&& node.getText().includes("const") && node.getText().includes(tokenName));
kind = SyntaxKind.ConstKeyword;
}
return { declaration, kind };
}

function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, node: VariableStatement | PropertyDeclaration, kind: SyntaxKind) {
if (kind === SyntaxKind.ConstKeyword) {
const constToken = findChild(node, node => node.kind === SyntaxKind.ConstKeyword);
if (constToken) {
const letToken = createToken(SyntaxKind.LetKeyword);
changes.replaceNode(sourceFile, constToken, letToken);
}
}
else if (kind === SyntaxKind.PropertyDeclaration) {
const readonlyToken = findChild(node, node => node.kind === SyntaxKind.ReadonlyKeyword);
if (readonlyToken) {
changes.delete(sourceFile, readonlyToken);
}
}
}

export function findChild<T extends Node>(
muradkhan101 marked this conversation as resolved.
Show resolved Hide resolved
n: Node,
test: (node: T) => boolean
): T | undefined {
const child = find(n.getChildren(), (c): c is T => test(c as T));
if (child) return child;
return n.getChildren().map((c) => findChild(c, test)).filter(Boolean)[0];
}
}
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: "Change 'const' to 'let'",
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: "Change 'readonly' to 'non-readonly'",
newFileContent:
`class Test {
prop = 5;
}
let testInstance = new Test();
testInstance.prop = 3;`,
});