mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-06 20:14:01 -06:00
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:
parent
a667b0455d
commit
fbb6cd57c0
@ -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;
|
||||
}
|
||||
}|]
|
||||
}
|
||||
}`);
|
||||
});
|
||||
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
45
tests/baselines/reference/extractMethod/extractMethod31.ts
Normal file
45
tests/baselines/reference/extractMethod/extractMethod31.ts
Normal 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;
|
||||
}
|
||||
46
tests/baselines/reference/extractMethod/extractMethod32.ts
Normal file
46
tests/baselines/reference/extractMethod/extractMethod32.ts
Normal 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;
|
||||
}
|
||||
};
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user