fix(54982): "Inline variable" causes failure when variable is used as the initializer of a destructured variable declaration (#54988)

This commit is contained in:
Oleksandr T 2023-07-18 02:13:00 +03:00 committed by GitHub
parent 02e8d5ec61
commit f7a93c83ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 44 additions and 1 deletions

View File

@ -132,7 +132,7 @@ function getInliningInfo(file: SourceFile, startPosition: number, tryWithReferen
// If triggered in a variable declaration, make sure it's not in a catch clause or for-loop
// and that it has a value.
if (isInitializedVariable(parent) && isVariableDeclarationInVariableStatement(parent)) {
if (isInitializedVariable(parent) && isVariableDeclarationInVariableStatement(parent) && isIdentifier(parent.name)) {
// Don't inline the variable if it has multiple declarations.
if (checker.getMergedSymbol(parent.symbol).declarations?.length !== 1) {
return { error: getLocaleSpecificMessage(Diagnostics.Variables_with_multiple_declarations_cannot_be_inlined) };

View File

@ -0,0 +1,8 @@
/// <reference path="fourslash.ts" />
////export function Component(props: any) {
//// const { foo } = /*a*/props/*b*/;
////}
goTo.select("a", "b");
verify.not.refactorAvailable("Inline variable");

View File

@ -0,0 +1,8 @@
/// <reference path="fourslash.ts" />
////export function func(arr: any) {
//// const [foo] = /*a*/arr/*b*/;
////}
goTo.select("a", "b");
verify.not.refactorAvailable("Inline variable");

View File

@ -0,0 +1,9 @@
/// <reference path="fourslash.ts" />
////function f() {
//// const a = { prop: 123 };
//// const { prop } = /*a*/a/*b*/;
////}
goTo.select("a", "b");
verify.not.refactorAvailable("Inline variable");

View File

@ -0,0 +1,18 @@
/// <reference path="fourslash.ts" />
////function f() {
//// const a = { prop: 123 };
//// const { prop } = /*a*/a/*b*/;
////}
goTo.select("a", "b");
verify.refactorAvailableForTriggerReason("invoked", "Inline variable");
edit.applyRefactor({
refactorName: "Inline variable",
actionName: "Inline variable",
actionDescription: "Inline variable",
newContent:
`function f() {
const { prop } = { prop: 123 };
}`,
triggerReason: "invoked"
});