Merge pull request #18448 from amcasey/NestedReturn

Only introduce return properties at the top level

(cherry picked from commit 288a57c16dbf2b55070ed350d139de618953d9ef)
This commit is contained in:
Andrew Casey 2017-09-13 16:42:55 -07:00 committed by Andrew Casey
parent a667b0455d
commit fbb6cd57c0
4 changed files with 125 additions and 2 deletions

View File

@ -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;
}
}|]
}
}`);
});

View File

@ -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) {
@ -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) {
@ -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;
}
}
}

View File

@ -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;
}

View File

@ -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;
}
};
}