Skip to content

Commit

Permalink
Merge pull request #18448 from amcasey/NestedReturn
Browse files Browse the repository at this point in the history
Only introduce return properties at the top level

(cherry picked from commit 288a57c)
  • Loading branch information
amcasey committed Sep 20, 2017
1 parent a667b04 commit fbb6cd5
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 2 deletions.
27 changes: 27 additions & 0 deletions src/harness/unittests/extractMethods.ts
Expand Up @@ -667,6 +667,33 @@ function parseUnaryExpression(operator: string): UnaryExpression {
function parsePrimaryExpression(): any {
throw "Not implemented";
}`);
// Return in nested function
testExtractMethod("extractMethod31",
`namespace N {
export const value = 1;
() => {
var f: () => number;
[#|f = function (): number {
return value;
}|]
}
}`);
// Return in nested class
testExtractMethod("extractMethod32",
`namespace N {
export const value = 1;
() => {
[#|var c = class {
M() {
return value;
}
}|]
}
}`);
});

Expand Down
9 changes: 7 additions & 2 deletions src/services/refactors/extractMethod.ts
Expand Up @@ -830,6 +830,7 @@ namespace ts.refactor.extractMethod {
return { body: createBlock(body.statements, /*multLine*/ true), returnValueProperty: undefined };
}
let returnValueProperty: string;
let ignoreReturns = false;
const statements = createNodeArray(isBlock(body) ? body.statements.slice(0) : [isStatement(body) ? body : createReturn(<Expression>body)]);
// rewrite body if either there are writes that should be propagated back via return statements or there are substitutions
if (writes || substitutions.size) {
Expand All @@ -852,7 +853,7 @@ namespace ts.refactor.extractMethod {
}

function visitor(node: Node): VisitResult<Node> {
if (node.kind === SyntaxKind.ReturnStatement && writes) {
if (!ignoreReturns && node.kind === SyntaxKind.ReturnStatement && writes) {
const assignments: ObjectLiteralElementLike[] = getPropertyAssignmentsForWrites(writes);
if ((<ReturnStatement>node).expression) {
if (!returnValueProperty) {
Expand All @@ -868,8 +869,12 @@ namespace ts.refactor.extractMethod {
}
}
else {
const oldIgnoreReturns = ignoreReturns;
ignoreReturns = ignoreReturns || isFunctionLike(node) || isClassLike(node);
const substitution = substitutions.get(getNodeId(node).toString());
return substitution || visitEachChild(node, visitor, nullTransformationContext);
const result = substitution || visitEachChild(node, visitor, nullTransformationContext);
ignoreReturns = oldIgnoreReturns;
return result;
}
}
}
Expand Down
45 changes: 45 additions & 0 deletions tests/baselines/reference/extractMethod/extractMethod31.ts
@@ -0,0 +1,45 @@
// ==ORIGINAL==
namespace N {

export const value = 1;

() => {
var f: () => number;
f = function (): number {
return value;
}
}
}
// ==SCOPE::namespace 'N'==
namespace N {

export const value = 1;

() => {
var f: () => number;
f = /*RENAME*/newFunction(f);
}

function newFunction(f: () => number) {
f = function(): number {
return value;
};
return f;
}
}
// ==SCOPE::global scope==
namespace N {

export const value = 1;

() => {
var f: () => number;
f = /*RENAME*/newFunction(f);
}
}
function newFunction(f: () => number) {
f = function(): number {
return N.value;
};
return f;
}
46 changes: 46 additions & 0 deletions tests/baselines/reference/extractMethod/extractMethod32.ts
@@ -0,0 +1,46 @@
// ==ORIGINAL==
namespace N {

export const value = 1;

() => {
var c = class {
M() {
return value;
}
}
}
}
// ==SCOPE::namespace 'N'==
namespace N {

export const value = 1;

() => {
/*RENAME*/newFunction();
}

function newFunction() {
var c = class {
M() {
return value;
}
};
}
}
// ==SCOPE::global scope==
namespace N {

export const value = 1;

() => {
/*RENAME*/newFunction();
}
}
function newFunction() {
var c = class {
M() {
return N.value;
}
};
}

0 comments on commit fbb6cd5

Please sign in to comment.