Skip to content

Commit aa2f72a

Browse files
committed
Handle the combination of a write and a void return
When the return type is void, there's no `returnValueProperty`, but that doesn't mean we don't need a `return` at the call site. Fixes microsoft#18140.
1 parent 27e590d commit aa2f72a

3 files changed

Lines changed: 39 additions & 2 deletions

File tree

‎src/harness/unittests/extractMethods.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ namespace ts {
224224
testExtractRange(`
225225
function f() {
226226
while (true) {
227-
[#|
227+
[#|
228228
if (x) {
229229
return;
230230
} |]
@@ -234,7 +234,7 @@ namespace ts {
234234
testExtractRange(`
235235
function f() {
236236
while (true) {
237-
[#|
237+
[#|
238238
[$|if (x) {
239239
}
240240
return;|]
@@ -605,6 +605,13 @@ namespace A {
605605
testExtractMethod("extractMethod19",
606606
`function F<T, U extends T[], V extends U[]>(v: V) {
607607
[#|v.toString()|];
608+
}`);
609+
// Write + void return
610+
testExtractMethod("extractMethod20",
611+
`function foo() {
612+
let x = 10;
613+
[#|x++;
614+
return;|]
608615
}`);
609616
});
610617

‎src/services/refactors/extractMethod.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,10 @@ namespace ts.refactor.extractMethod {
746746
}
747747
else {
748748
newNodes.push(createStatement(createBinary(assignments[0].name, SyntaxKind.EqualsToken, call)));
749+
750+
if (range.facts & RangeFacts.HasReturn) {
751+
newNodes.push(createReturn());
752+
}
749753
}
750754
}
751755
else {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// ==ORIGINAL==
2+
function foo() {
3+
let x = 10;
4+
x++;
5+
return;
6+
}
7+
// ==SCOPE::function 'foo'==
8+
function foo() {
9+
let x = 10;
10+
return newFunction();
11+
12+
function newFunction() {
13+
x++;
14+
return;
15+
}
16+
}
17+
// ==SCOPE::global scope==
18+
function foo() {
19+
let x = 10;
20+
x = newFunction(x);
21+
return;
22+
}
23+
function newFunction(x: number) {
24+
x++;
25+
return x;
26+
}

0 commit comments

Comments
 (0)