Skip to content

Commit

Permalink
Merge pull request #18423 from amcasey/GH18188
Browse files Browse the repository at this point in the history
Call getShorthandAssignmentValueSymbol rather than getSymbolAtLocation

(cherry picked from commit aade971)
  • Loading branch information
amcasey committed Sep 20, 2017
1 parent 334125e commit a667b04
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
19 changes: 19 additions & 0 deletions src/harness/unittests/extractMethods.ts
Expand Up @@ -648,6 +648,25 @@ function M3() { }`);
}
M3() { }
constructor() { }
}`);
// Shorthand property names
testExtractMethod("extractMethod29",
`interface UnaryExpression {
kind: "Unary";
operator: string;
operand: any;
}
function parseUnaryExpression(operator: string): UnaryExpression {
[#|return {
kind: "Unary",
operator,
operand: parsePrimaryExpression(),
};|]
}
function parsePrimaryExpression(): any {
throw "Not implemented";
}`);
});

Expand Down
6 changes: 5 additions & 1 deletion src/services/refactors/extractMethod.ts
Expand Up @@ -1022,7 +1022,11 @@ namespace ts.refactor.extractMethod {
}

function recordUsagebySymbol(identifier: Identifier, usage: Usage, isTypeName: boolean) {
const symbol = checker.getSymbolAtLocation(identifier);
// If the identifier is both a property name and its value, we're only interested in its value
// (since the name is a declaration and will be included in the extracted range).
const symbol = identifier.parent && isShorthandPropertyAssignment(identifier.parent) && identifier.parent.name === identifier
? checker.getShorthandAssignmentValueSymbol(identifier.parent)
: checker.getSymbolAtLocation(identifier);
if (!symbol) {
// cannot find symbol - do nothing
return undefined;
Expand Down
62 changes: 62 additions & 0 deletions tests/baselines/reference/extractMethod/extractMethod29.ts
@@ -0,0 +1,62 @@
// ==ORIGINAL==
interface UnaryExpression {
kind: "Unary";
operator: string;
operand: any;
}

function parseUnaryExpression(operator: string): UnaryExpression {
return {
kind: "Unary",
operator,
operand: parsePrimaryExpression(),
};
}

function parsePrimaryExpression(): any {
throw "Not implemented";
}
// ==SCOPE::function 'parseUnaryExpression'==
interface UnaryExpression {
kind: "Unary";
operator: string;
operand: any;
}

function parseUnaryExpression(operator: string): UnaryExpression {
return /*RENAME*/newFunction();

function newFunction() {
return {
kind: "Unary",
operator,
operand: parsePrimaryExpression(),
};
}
}

function parsePrimaryExpression(): any {
throw "Not implemented";
}
// ==SCOPE::global scope==
interface UnaryExpression {
kind: "Unary";
operator: string;
operand: any;
}

function parseUnaryExpression(operator: string): UnaryExpression {
return /*RENAME*/newFunction(operator);
}

function newFunction(operator: string) {
return {
kind: "Unary",
operator,
operand: parsePrimaryExpression(),
};
}

function parsePrimaryExpression(): any {
throw "Not implemented";
}

0 comments on commit a667b04

Please sign in to comment.