fix(33325): allow extract refactoring on selected statement without trailing semicolon (#45765)

This commit is contained in:
Oleksandr T
2021-11-05 10:00:31 -07:00
committed by GitHub
parent 4fca1e1fcd
commit 2d4b243195
3 changed files with 24 additions and 4 deletions
+3 -3
View File
@@ -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;
}
@@ -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++) {}`);
});
+20
View File
@@ -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;
}`
});