fix(49838): "Extract function" refactoring action is disabled for a wrong reason (#49840)

* fix(49838): allow extracting functions with a break statement inside loop context

* remove useless flag

* add more tests
This commit is contained in:
Oleksandr T 2022-07-19 23:06:45 +03:00 committed by GitHub
parent 298b3a432c
commit 5a53e9bb5e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 205 additions and 1 deletions

View File

@ -551,7 +551,7 @@ namespace ts.refactor.extractSymbol {
const savedPermittedJumps = permittedJumps;
switch (node.kind) {
case SyntaxKind.IfStatement:
permittedJumps = PermittedJumps.None;
permittedJumps &= ~PermittedJumps.Return;
break;
case SyntaxKind.TryStatement:
// forbid all jumps inside try blocks

View File

@ -0,0 +1,54 @@
/// <reference path="fourslash.ts" />
////export function fn(m: number) {
//// const mode = m >= 0 ? "a" : "b";
//// let result: number = 0;
////
//// if (mode === "a") {
//// /*a*/for (let i = 0; i < 10; i++) {
//// const rand = Math.random();
//// if (rand > 0.5) {
//// result = rand;
//// break;
//// }
//// }/*b*/
//// }
//// else {
//// result = 0;
//// }
////
//// return result;
////}
goTo.select("a", "b");
edit.applyRefactor({
refactorName: "Extract Symbol",
actionName: "function_scope_1",
actionDescription: "Extract to function in module scope",
newContent:
`export function fn(m: number) {
const mode = m >= 0 ? "a" : "b";
let result: number = 0;
if (mode === "a") {
result = /*RENAME*/newFunction(result);
}
else {
result = 0;
}
return result;
}
function newFunction(result: number) {
for (let i = 0; i < 10; i++) {
const rand = Math.random();
if (rand > 0.5) {
result = rand;
break;
}
}
return result;
}
`
});

View File

@ -0,0 +1,54 @@
/// <reference path="fourslash.ts" />
////export function fn(m: number, n: number) {
//// const mode = m >= 0 ? "a" : "b";
//// let result: number = 0;
////
//// if (mode === "a") {
//// /*a*/for (let i = 0; i < n; i++) {
//// const rand = Math.random();
//// if (rand > 0.5) {
//// result = rand;
//// break;
//// }
//// }/*b*/
//// }
//// else {
//// result = 0;
//// }
////
//// return result;
////}
goTo.select("a", "b");
edit.applyRefactor({
refactorName: "Extract Symbol",
actionName: "function_scope_1",
actionDescription: "Extract to function in module scope",
newContent:
`export function fn(m: number, n: number) {
const mode = m >= 0 ? "a" : "b";
let result: number = 0;
if (mode === "a") {
result = /*RENAME*/newFunction(n, result);
}
else {
result = 0;
}
return result;
}
function newFunction(n: number, result: number) {
for (let i = 0; i < n; i++) {
const rand = Math.random();
if (rand > 0.5) {
result = rand;
break;
}
}
return result;
}
`
});

View File

@ -0,0 +1,60 @@
/// <reference path="fourslash.ts" />
////export function fn(m: number, n: number) {
//// const mode = m >= 0 ? "a" : "b";
//// let result: number = 0;
////
//// if (mode === "a") {
//// /*a*/for (let i = 0; i < n; i++) {
//// const rand = Math.random();
//// switch (rand) {
//// case 0.5:
//// result = rand;
//// break;
//// default:
//// result = 1;
//// break;
//// }
//// }/*b*/
//// }
//// else {
//// result = 0;
//// }
//// return result;
////}
goTo.select("a", "b");
edit.applyRefactor({
refactorName: "Extract Symbol",
actionName: "function_scope_1",
actionDescription: "Extract to function in module scope",
newContent:
`export function fn(m: number, n: number) {
const mode = m >= 0 ? "a" : "b";
let result: number = 0;
if (mode === "a") {
result = /*RENAME*/newFunction(n, result);
}
else {
result = 0;
}
return result;
}
function newFunction(n: number, result: number) {
for (let i = 0; i < n; i++) {
const rand = Math.random();
switch (rand) {
case 0.5:
result = rand;
break;
default:
result = 1;
break;
}
}
return result;
}
`
});

View File

@ -0,0 +1,36 @@
/// <reference path="fourslash.ts" />
////export function fn(x: number, y: number) {
//// /*a*/switch (x) {
//// case 1:
//// if (y) {
//// break;
//// }
//// x--;
//// break;
//// }/*b*/
////}
goTo.select("a", "b");
edit.applyRefactor({
refactorName: "Extract Symbol",
actionName: "function_scope_1",
actionDescription: "Extract to function in module scope",
newContent:
`export function fn(x: number, y: number) {
x = /*RENAME*/newFunction(x, y);
}
function newFunction(x: number, y: number) {
switch (x) {
case 1:
if (y) {
break;
}
x--;
break;
}
return x;
}
`
});