Skip to content

Commit a667b04

Browse files
committed
Merge pull request #18423 from amcasey/GH18188
Call getShorthandAssignmentValueSymbol rather than getSymbolAtLocation (cherry picked from commit aade971)
1 parent 334125e commit a667b04

File tree

3 files changed

+86
-1
lines changed

3 files changed

+86
-1
lines changed

src/harness/unittests/extractMethods.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,25 @@ function M3() { }`);
648648
}
649649
M3() { }
650650
constructor() { }
651+
}`);
652+
// Shorthand property names
653+
testExtractMethod("extractMethod29",
654+
`interface UnaryExpression {
655+
kind: "Unary";
656+
operator: string;
657+
operand: any;
658+
}
659+
660+
function parseUnaryExpression(operator: string): UnaryExpression {
661+
[#|return {
662+
kind: "Unary",
663+
operator,
664+
operand: parsePrimaryExpression(),
665+
};|]
666+
}
667+
668+
function parsePrimaryExpression(): any {
669+
throw "Not implemented";
651670
}`);
652671
});
653672

src/services/refactors/extractMethod.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1022,7 +1022,11 @@ namespace ts.refactor.extractMethod {
10221022
}
10231023

10241024
function recordUsagebySymbol(identifier: Identifier, usage: Usage, isTypeName: boolean) {
1025-
const symbol = checker.getSymbolAtLocation(identifier);
1025+
// If the identifier is both a property name and its value, we're only interested in its value
1026+
// (since the name is a declaration and will be included in the extracted range).
1027+
const symbol = identifier.parent && isShorthandPropertyAssignment(identifier.parent) && identifier.parent.name === identifier
1028+
? checker.getShorthandAssignmentValueSymbol(identifier.parent)
1029+
: checker.getSymbolAtLocation(identifier);
10261030
if (!symbol) {
10271031
// cannot find symbol - do nothing
10281032
return undefined;
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// ==ORIGINAL==
2+
interface UnaryExpression {
3+
kind: "Unary";
4+
operator: string;
5+
operand: any;
6+
}
7+
8+
function parseUnaryExpression(operator: string): UnaryExpression {
9+
return {
10+
kind: "Unary",
11+
operator,
12+
operand: parsePrimaryExpression(),
13+
};
14+
}
15+
16+
function parsePrimaryExpression(): any {
17+
throw "Not implemented";
18+
}
19+
// ==SCOPE::function 'parseUnaryExpression'==
20+
interface UnaryExpression {
21+
kind: "Unary";
22+
operator: string;
23+
operand: any;
24+
}
25+
26+
function parseUnaryExpression(operator: string): UnaryExpression {
27+
return /*RENAME*/newFunction();
28+
29+
function newFunction() {
30+
return {
31+
kind: "Unary",
32+
operator,
33+
operand: parsePrimaryExpression(),
34+
};
35+
}
36+
}
37+
38+
function parsePrimaryExpression(): any {
39+
throw "Not implemented";
40+
}
41+
// ==SCOPE::global scope==
42+
interface UnaryExpression {
43+
kind: "Unary";
44+
operator: string;
45+
operand: any;
46+
}
47+
48+
function parseUnaryExpression(operator: string): UnaryExpression {
49+
return /*RENAME*/newFunction(operator);
50+
}
51+
52+
function newFunction(operator: string) {
53+
return {
54+
kind: "Unary",
55+
operator,
56+
operand: parsePrimaryExpression(),
57+
};
58+
}
59+
60+
function parsePrimaryExpression(): any {
61+
throw "Not implemented";
62+
}

0 commit comments

Comments
 (0)