From 2dd647d08c07c78b60207e5849be40d830ad7491 Mon Sep 17 00:00:00 2001 From: Andrew Casey Date: Thu, 7 Sep 2017 16:32:55 -0700 Subject: [PATCH] Merge pull request #18164 from amcasey/GH18140 Handle the combination of a write and a void return (cherry picked from commit 02cfb81ac0c9b870d31be14b16b0a08ea24ed32a) --- src/harness/unittests/extractMethods.ts | 7 +++++ src/services/refactors/extractMethod.ts | 4 +++ .../extractMethod/extractMethod21.ts | 26 +++++++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 tests/baselines/reference/extractMethod/extractMethod21.ts diff --git a/src/harness/unittests/extractMethods.ts b/src/harness/unittests/extractMethods.ts index 9fc7bd689e9..72617bf7740 100644 --- a/src/harness/unittests/extractMethods.ts +++ b/src/harness/unittests/extractMethods.ts @@ -554,6 +554,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 f2b644a9389..bb9df097940 100644 --- a/src/services/refactors/extractMethod.ts +++ b/src/services/refactors/extractMethod.ts @@ -722,6 +722,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 00000000000..bbbee38781a --- /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 /*RENAME*/newFunction(); + + function newFunction() { + x++; + return; + } +} +// ==SCOPE::global scope== +function foo() { + let x = 10; + x = /*RENAME*/newFunction(x); + return; +} +function newFunction(x: number) { + x++; + return x; +}