-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Add codefix to make non-reassignable variables reassignable #27593
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
Closed
Closed
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2995385
Added codefix for convert const / readonly to reassignable types
muradkhan101 5a53b97
Added test cases for const -> let and readonly -> non-readonly codefi…
muradkhan101 bfc12d8
Added new codefix file to tsconfig file
muradkhan101 9925517
Updated logic and diagnostic messages in convert const / readonly cod…
33200d9
Minor refactoring to remove unnecessary code
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/services/codefixes/fixConvertConstantVariableOrProperty.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
return [createCodeFixAction(fixId, changes, [Diagnostics.Change_0_to_1, type, changeTo], fixId, Diagnostics.Make_all_const_or_readonly_expressions_reassignable)]; | ||
< 8000 /td> | }, | |
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]; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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", | ||
|
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;`, | ||
}); |
17 changes: 17 additions & 0 deletions
17
tests/cases/fourslash/codeFixConvertConstantVariableOrProperty_readonly.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
< 4E7B td class="blob-code blob-code-addition js-file-line"> ////testInstance.prop = 3; | ||
|
||
verify.codeFix({ | ||
description: "Change 'readonly' to 'non-readonly'", | ||
newFileContent: | ||
`class Test { | ||
prop = 5; | ||
} | ||
let testInstance = new Test(); | ||
testInstance.prop = 3;`, | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
You can’t perform that action at this time.
Uh oh!
There was an error while loading. Please reload this page.