Skip to content

Commit 34576c2

Browse files
committed
Call getShorthandAssignmentValueSymbol rather than getSymbolAtLocation
...for shorthand property assignment names when collecting usages.
1 parent 2a70bf5 commit 34576c2

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
@@ -709,6 +709,25 @@ function M3() { }`);
709709
}
710710
M3() { }
711711
constructor() { }
712+
}`);
713+
// Shorthand property names
714+
testExtractMethod("extractMethod29",
715+
`interface UnaryExpression {
716+
kind: "Unary";
717+
operator: string;
718+
operand: any;
719+
}
720+
721+
function parseUnaryExpression(operator: string): UnaryExpression {
722+
[#|return {
723+
kind: "Unary",
724+
operator,
725+
operand: parsePrimaryExpression(),
726+
};|]
727+
}
728+
729+
function parsePrimaryExpression(): any {
730+
throw "Not implemented";
712731
}`);
713732
});
714733

src/services/refactors/extractMethod.ts

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

11631163
function recordUsagebySymbol(identifier: Identifier, usage: Usage, isTypeName: boolean) {
1164-
const symbol = checker.getSymbolAtLocation(identifier);
1164+
// If the identifier is both a property name and its value, we're only interested in its value
1165+
// (since the name is a declaration and will be included in the extracted range).
1166+
const symbol = identifier.parent && isShorthandPropertyAssignment(identifier.parent) && identifier.parent.name === identifier
1167+
? checker.getShorthandAssignmentValueSymbol(identifier.parent)
1168+
: checker.getSymbolAtLocation(identifier);
11651169
if (!symbol) {
11661170
// cannot find symbol - do nothing
11671171
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::inner function in 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::function in 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)