Skip to content

Commit 2d4b243

Browse files
authored
fix(33325): allow extract refactoring on selected statement without trailing semicolon (#45765)
1 parent 4fca1e1 commit 2d4b243

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

Diff for: src/services/refactors/extractSymbol.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -364,10 +364,11 @@ namespace ts.refactor.extractSymbol {
364364
return node.expression;
365365
}
366366
}
367-
else if (isVariableStatement(node)) {
367+
else if (isVariableStatement(node) || isVariableDeclarationList(node)) {
368+
const declarations = isVariableStatement(node) ? node.declarationList.declarations : node.declarations;
368369
let numInitializers = 0;
369370
let lastInitializer: Expression | undefined;
370-
for (const declaration of node.declarationList.declarations) {
371+
for (const declaration of declarations) {
371372
if (declaration.initializer) {
372373
numInitializers++;
373374
lastInitializer = declaration.initializer;
@@ -383,7 +384,6 @@ namespace ts.refactor.extractSymbol {
383384
return node.initializer;
384385
}
385386
}
386-
387387
return node;
388388
}
389389

Diff for: src/testRunner/unittests/services/extract/ranges.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ namespace ts {
191191
testExtractRange("extractRange28", `[#|return [$|1|];|]`);
192192

193193
// For statements
194-
testExtractRange("extractRange29", `for ([#|var i = 1|]; i < 2; i++) {}`);
194+
testExtractRange("extractRange29", `for ([#|var i = [$|1|]|]; i < 2; i++) {}`);
195195
testExtractRange("extractRange30", `for (var i = [#|[$|1|]|]; i < 2; i++) {}`);
196196
});
197197

Diff for: tests/cases/fourslash/extract-const10.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @filename: foo.ts
4+
////function foo() {
5+
//// /*a*/const a = [1]/*b*/;
6+
//// return a;
7+
////}
8+
9+
goTo.select("a", "b");
10+
edit.applyRefactor({
11+
refactorName: "Extract Symbol",
12+
actionName: "constant_scope_1",
13+
actionDescription: "Extract to constant in global scope",
14+
newContent:
15+
`const newLocal = [1];
16+
function foo() {
17+
const a = /*RENAME*/newLocal;
18+
return a;
19+
}`
20+
});

0 commit comments

Comments
 (0)