Skip to content

fix(33325): Allow running extract refactoring on selected statement even when trailing semicolon is not included in selection #45765

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/services/refactors/extractSymbol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -383,7 +384,6 @@ namespace ts.refactor.extractSymbol {
return node.initializer;
}
}

return node;
}

Expand Down
2 changes: 1 addition & 1 deletion src/testRunner/unittests/services/extract/ranges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {}`);
});

Expand Down
20 changes: 20 additions & 0 deletions tests/cases/fourslash/extract-const10.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/// <reference path='fourslash.ts' />

// @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;
}`
});