Skip to content

Commit 9a2868b

Browse files
authored
Infer extracted local variable name from property name (microsoft#37902)
1 parent df1faa0 commit 9a2868b

10 files changed

+60
-1
lines changed

Diff for: src/services/refactors/extractSymbol.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1136,7 +1136,9 @@ namespace ts.refactor.extractSymbol {
11361136

11371137
// Make a unique name for the extracted variable
11381138
const file = scope.getSourceFile();
1139-
const localNameText = getUniqueName(isClassLike(scope) ? "newProperty" : "newLocal", file);
1139+
const localNameText = isPropertyAccessExpression(node) && !isClassLike(scope) && !checker.resolveName(node.name.text, node, SymbolFlags.Value, /*excludeGlobals*/ false) && !isPrivateIdentifier(node.name) && !isKeyword(node.name.originalKeywordKind!)
1140+
? node.name.text
1141+
: getUniqueName(isClassLike(scope) ? "newProperty" : "newLocal", file);
11401142
const isJS = isInJSFile(scope);
11411143

11421144
let variableType = isJS || !checker.isContextSensitive(node)

Diff for: src/testRunner/unittests/services/extract/constants.ts

+13
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,19 @@ switch (1) {
279279
break;
280280
}
281281
`);
282+
283+
testExtractConstant("extractConstant_PropertyName",
284+
`[#|x.y|].z();`);
285+
286+
testExtractConstant("extractConstant_PropertyName_ExistingName",
287+
`let y;
288+
[#|x.y|].z();`);
289+
290+
testExtractConstant("extractConstant_PropertyName_Keyword",
291+
`[#|x.if|].z();`);
292+
293+
testExtractConstant("extractConstant_PropertyName_PrivateIdentifierKeyword",
294+
`[#|this.#if|].z();`);
282295
});
283296

284297
function testExtractConstant(caption: string, text: string) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// ==ORIGINAL==
2+
/*[#|*/x.y/*|]*/.z();
3+
// ==SCOPE::Extract to constant in enclosing scope==
4+
const y = x.y;
5+
/*RENAME*/y.z();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// ==ORIGINAL==
2+
/*[#|*/x.y/*|]*/.z();
3+
// ==SCOPE::Extract to constant in enclosing scope==
4+
const y = x.y;
5+
/*RENAME*/y.z();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// ==ORIGINAL==
2+
let y;
3+
/*[#|*/x.y/*|]*/.z();
4+
// ==SCOPE::Extract to constant in enclosing scope==
5+
let y;
6+
const newLocal = x.y;
7+
/*RENAME*/newLocal.z();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// ==ORIGINAL==
2+
let y;
3+
/*[#|*/x.y/*|]*/.z();
4+
// ==SCOPE::Extract to constant in enclosing scope==
5+
let y;
6+
const newLocal = x.y;
7+
/*RENAME*/newLocal.z();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// ==ORIGINAL==
2+
/*[#|*/x.if/*|]*/.z();
3+
// ==SCOPE::Extract to constant in enclosing scope==
4+
const newLocal = x.if;
5+
/*RENAME*/newLocal.z();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// ==ORIGINAL==
2+
/*[#|*/x.if/*|]*/.z();
3+
// ==SCOPE::Extract to constant in enclosing scope==
4+
const newLocal = x.if;
5+
/*RENAME*/newLocal.z();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// ==ORIGINAL==
2+
/*[#|*/this.#if/*|]*/.z();
3+
// ==SCOPE::Extract to constant in enclosing scope==
4+
const newLocal = this.#if;
5+
/*RENAME*/newLocal.z();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// ==ORIGINAL==
2+
/*[#|*/this.#if/*|]*/.z();
3+
// ==SCOPE::Extract to constant in enclosing scope==
4+
const newLocal = this.#if;
5+
/*RENAME*/newLocal.z();

0 commit comments

Comments
 (0)