From 2d4b24319558ada4f9864375e42c87d8d1e241b8 Mon Sep 17 00:00:00 2001 From: Oleksandr T Date: Fri, 5 Nov 2021 19:00:31 +0200 Subject: [PATCH] fix(33325): allow extract refactoring on selected statement without trailing semicolon (#45765) --- src/services/refactors/extractSymbol.ts | 6 +++--- .../unittests/services/extract/ranges.ts | 2 +- tests/cases/fourslash/extract-const10.ts | 20 +++++++++++++++++++ 3 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 tests/cases/fourslash/extract-const10.ts diff --git a/src/services/refactors/extractSymbol.ts b/src/services/refactors/extractSymbol.ts index 6490fde5387..a14ee526ae7 100644 --- a/src/services/refactors/extractSymbol.ts +++ b/src/services/refactors/extractSymbol.ts @@ -364,10 +364,11 @@ namespace ts.refactor.extractSymbol { return node.expression; } } - else if (isVariableStatement(node)) { + else if (isVariableStatement(node) || isVariableDeclarationList(node)) { + const declarations = isVariableStatement(node) ? node.declarationList.declarations : node.declarations; let numInitializers = 0; let lastInitializer: Expression | undefined; - for (const declaration of node.declarationList.declarations) { + for (const declaration of declarations) { if (declaration.initializer) { numInitializers++; lastInitializer = declaration.initializer; @@ -383,7 +384,6 @@ namespace ts.refactor.extractSymbol { return node.initializer; } } - return node; } diff --git a/src/testRunner/unittests/services/extract/ranges.ts b/src/testRunner/unittests/services/extract/ranges.ts index 2264f3982ff..34f379df26d 100644 --- a/src/testRunner/unittests/services/extract/ranges.ts +++ b/src/testRunner/unittests/services/extract/ranges.ts @@ -191,7 +191,7 @@ namespace ts { testExtractRange("extractRange28", `[#|return [$|1|];|]`); // For statements - testExtractRange("extractRange29", `for ([#|var i = 1|]; i < 2; i++) {}`); + testExtractRange("extractRange29", `for ([#|var i = [$|1|]|]; i < 2; i++) {}`); testExtractRange("extractRange30", `for (var i = [#|[$|1|]|]; i < 2; i++) {}`); }); diff --git a/tests/cases/fourslash/extract-const10.ts b/tests/cases/fourslash/extract-const10.ts new file mode 100644 index 00000000000..2eaea499157 --- /dev/null +++ b/tests/cases/fourslash/extract-const10.ts @@ -0,0 +1,20 @@ +/// + +// @filename: foo.ts +////function foo() { +//// /*a*/const a = [1]/*b*/; +//// return a; +////} + +goTo.select("a", "b"); +edit.applyRefactor({ + refactorName: "Extract Symbol", + actionName: "constant_scope_1", + actionDescription: "Extract to constant in global scope", + newContent: +`const newLocal = [1]; +function foo() { + const a = /*RENAME*/newLocal; + return a; +}` +});