diff --git a/src/harness/unittests/extractMethods.ts b/src/harness/unittests/extractMethods.ts index c8b4b35ff1c3e..6fe5789572545 100644 --- a/src/harness/unittests/extractMethods.ts +++ b/src/harness/unittests/extractMethods.ts @@ -613,6 +613,13 @@ namespace A { [#|let a1 = { x: 1 }; return a1.x + 10;|] } +}`); + // Write + void return + testExtractMethod("extractMethod21", + `function foo() { + let x = 10; + [#|x++; + return;|] }`); }); diff --git a/src/services/refactors/extractMethod.ts b/src/services/refactors/extractMethod.ts index 25a995dc23139..c497b12675990 100644 --- a/src/services/refactors/extractMethod.ts +++ b/src/services/refactors/extractMethod.ts @@ -748,6 +748,10 @@ namespace ts.refactor.extractMethod { } else { newNodes.push(createStatement(createBinary(assignments[0].name, SyntaxKind.EqualsToken, call))); + + if (range.facts & RangeFacts.HasReturn) { + newNodes.push(createReturn()); + } } } else { diff --git a/tests/baselines/reference/extractMethod/extractMethod21.ts b/tests/baselines/reference/extractMethod/extractMethod21.ts new file mode 100644 index 0000000000000..4b73a6ed3c70d --- /dev/null +++ b/tests/baselines/reference/extractMethod/extractMethod21.ts @@ -0,0 +1,26 @@ +// ==ORIGINAL== +function foo() { + let x = 10; + x++; + return; +} +// ==SCOPE::function 'foo'== +function foo() { + let x = 10; + return newFunction(); + + function newFunction() { + x++; + return; + } +} +// ==SCOPE::global scope== +function foo() { + let x = 10; + x = newFunction(x); + return; +} +function newFunction(x: number) { + x++; + return x; +}