mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-05 16:38:05 -06:00
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:
parent
298b3a432c
commit
5a53e9bb5e
@ -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
|
||||
|
||||
54
tests/cases/fourslash/extract-method45.ts
Normal file
54
tests/cases/fourslash/extract-method45.ts
Normal 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;
|
||||
}
|
||||
`
|
||||
});
|
||||
54
tests/cases/fourslash/extract-method46.ts
Normal file
54
tests/cases/fourslash/extract-method46.ts
Normal 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;
|
||||
}
|
||||
`
|
||||
});
|
||||
60
tests/cases/fourslash/extract-method47.ts
Normal file
60
tests/cases/fourslash/extract-method47.ts
Normal 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;
|
||||
}
|
||||
`
|
||||
});
|
||||
36
tests/cases/fourslash/extract-method48.ts
Normal file
36
tests/cases/fourslash/extract-method48.ts
Normal 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;
|
||||
}
|
||||
`
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user