Skip to content

Commit fbb6cd5

Browse files
committed
Merge pull request #18448 from amcasey/NestedReturn
Only introduce return properties at the top level (cherry picked from commit 288a57c)
1 parent a667b04 commit fbb6cd5

File tree

4 files changed

+125
-2
lines changed

4 files changed

+125
-2
lines changed

src/harness/unittests/extractMethods.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,33 @@ function parseUnaryExpression(operator: string): UnaryExpression {
667667
668668
function parsePrimaryExpression(): any {
669669
throw "Not implemented";
670+
}`);
671+
// Return in nested function
672+
testExtractMethod("extractMethod31",
673+
`namespace N {
674+
675+
export const value = 1;
676+
677+
() => {
678+
var f: () => number;
679+
[#|f = function (): number {
680+
return value;
681+
}|]
682+
}
683+
}`);
684+
// Return in nested class
685+
testExtractMethod("extractMethod32",
686+
`namespace N {
687+
688+
export const value = 1;
689+
690+
() => {
691+
[#|var c = class {
692+
M() {
693+
return value;
694+
}
695+
}|]
696+
}
670697
}`);
671698
});
672699

src/services/refactors/extractMethod.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,7 @@ namespace ts.refactor.extractMethod {
830830
return { body: createBlock(body.statements, /*multLine*/ true), returnValueProperty: undefined };
831831
}
832832
let returnValueProperty: string;
833+
let ignoreReturns = false;
833834
const statements = createNodeArray(isBlock(body) ? body.statements.slice(0) : [isStatement(body) ? body : createReturn(<Expression>body)]);
834835
// rewrite body if either there are writes that should be propagated back via return statements or there are substitutions
835836
if (writes || substitutions.size) {
@@ -852,7 +853,7 @@ namespace ts.refactor.extractMethod {
852853
}
853854

854855
function visitor(node: Node): VisitResult<Node> {
855-
if (node.kind === SyntaxKind.ReturnStatement && writes) {
856+
if (!ignoreReturns && node.kind === SyntaxKind.ReturnStatement && writes) {
856857
const assignments: ObjectLiteralElementLike[] = getPropertyAssignmentsForWrites(writes);
857858
if ((<ReturnStatement>node).expression) {
858859
if (!returnValueProperty) {
@@ -868,8 +869,12 @@ namespace ts.refactor.extractMethod {
868869
}
869870
}
870871
else {
872+
const oldIgnoreReturns = ignoreReturns;
873+
ignoreReturns = ignoreReturns || isFunctionLike(node) || isClassLike(node);
871874
const substitution = substitutions.get(getNodeId(node).toString());
872-
return substitution || visitEachChild(node, visitor, nullTransformationContext);
875+
const result = substitution || visitEachChild(node, visitor, nullTransformationContext);
876+
ignoreReturns = oldIgnoreReturns;
877+
return result;
873878
}
874879
}
875880
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// ==ORIGINAL==
2+
namespace N {
3+
4+
export const value = 1;
5+
6+
() => {
7+
var f: () => number;
8+
f = function (): number {
9+
return value;
10+
}
11+
}
12+
}
13+
// ==SCOPE::namespace 'N'==
14+
namespace N {
15+
16+
export const value = 1;
17+
18+
() => {
19+
var f: () => number;
20+
f = /*RENAME*/newFunction(f);
21+
}
22+
23+
function newFunction(f: () => number) {
24+
f = function(): number {
25+
return value;
26+
};
27+
return f;
28+
}
29+
}
30+
// ==SCOPE::global scope==
31+
namespace N {
32+
33+
export const value = 1;
34+
35+
() => {
36+
var f: () => number;
37+
f = /*RENAME*/newFunction(f);
38+
}
39+
}
40+
function newFunction(f: () => number) {
41+
f = function(): number {
42+
return N.value;
43+
};
44+
return f;
45+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// ==ORIGINAL==
2+
namespace N {
3+
4+
export const value = 1;
5+
6+
() => {
7+
var c = class {
8+
M() {
9+
return value;
10+
}
11+
}
12+
}
13+
}
14+
// ==SCOPE::namespace 'N'==
15+
namespace N {
16+
17+
export const value = 1;
18+
19+
() => {
20+
/*RENAME*/newFunction();
21+
}
22+
23+
function newFunction() {
24+
var c = class {
25+
M() {
26+
return value;
27+
}
28+
};
29+
}
30+
}
31+
// ==SCOPE::global scope==
32+
namespace N {
33+
34+
export const value = 1;
35+
36+
() => {
37+
/*RENAME*/newFunction();
38+
}
39+
}
40+
function newFunction() {
41+
var c = class {
42+
M() {
43+
return N.value;
44+
}
45+
};
46+
}

0 commit comments

Comments
 (0)