mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-06 20:14:01 -06:00
Merge pull request #18165 from amcasey/GH18144
Simplify and correct PermittedJumps computation (cherry picked from commit deefb01c9d53020a46a1692bf73b34696dad5c0c)
This commit is contained in:
parent
2dd647d08c
commit
12d1ea50ca
@ -378,6 +378,30 @@ namespace A {
|
||||
"Cannot extract range containing conditional return statement."
|
||||
]);
|
||||
|
||||
testExtractRangeFailed("extractRangeFailed7",
|
||||
`
|
||||
function test(x: number) {
|
||||
while (x) {
|
||||
x--;
|
||||
[#|break;|]
|
||||
}
|
||||
}
|
||||
`,
|
||||
[
|
||||
"Cannot extract range containing conditional break or continue statements."
|
||||
]);
|
||||
testExtractRangeFailed("extractRangeFailed8",
|
||||
`
|
||||
function test(x: number) {
|
||||
switch (x) {
|
||||
case 1:
|
||||
[#|break;|]
|
||||
}
|
||||
}
|
||||
`,
|
||||
[
|
||||
"Cannot extract range containing conditional break or continue statements."
|
||||
]);
|
||||
testExtractRangeFailed("extract-method-not-for-token-expression-statement", `[#|a|]`, ["Select more than a single token."]);
|
||||
|
||||
testExtractMethod("extractMethod1",
|
||||
@ -561,6 +585,15 @@ namespace A {
|
||||
let x = 10;
|
||||
[#|x++;
|
||||
return;|]
|
||||
}`);
|
||||
// Return in finally block
|
||||
testExtractMethod("extractMethod22",
|
||||
`function test() {
|
||||
try {
|
||||
}
|
||||
finally {
|
||||
[#|return 1;|]
|
||||
}
|
||||
}`);
|
||||
});
|
||||
|
||||
|
||||
@ -340,45 +340,31 @@ namespace ts.refactor.extractMethod {
|
||||
return false;
|
||||
}
|
||||
const savedPermittedJumps = permittedJumps;
|
||||
if (node.parent) {
|
||||
switch (node.parent.kind) {
|
||||
case SyntaxKind.IfStatement:
|
||||
if ((<IfStatement>node.parent).thenStatement === node || (<IfStatement>node.parent).elseStatement === node) {
|
||||
// forbid all jumps inside thenStatement or elseStatement
|
||||
permittedJumps = PermittedJumps.None;
|
||||
}
|
||||
break;
|
||||
case SyntaxKind.TryStatement:
|
||||
if ((<TryStatement>node.parent).tryBlock === node) {
|
||||
// forbid all jumps inside try blocks
|
||||
permittedJumps = PermittedJumps.None;
|
||||
}
|
||||
else if ((<TryStatement>node.parent).finallyBlock === node) {
|
||||
// allow unconditional returns from finally blocks
|
||||
permittedJumps = PermittedJumps.Return;
|
||||
}
|
||||
break;
|
||||
case SyntaxKind.CatchClause:
|
||||
if ((<CatchClause>node.parent).block === node) {
|
||||
// forbid all jumps inside the block of catch clause
|
||||
permittedJumps = PermittedJumps.None;
|
||||
}
|
||||
break;
|
||||
case SyntaxKind.CaseClause:
|
||||
if ((<CaseClause>node).expression !== node) {
|
||||
// allow unlabeled break inside case clauses
|
||||
permittedJumps |= PermittedJumps.Break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
if (isIterationStatement(node.parent, /*lookInLabeledStatements*/ false)) {
|
||||
if ((<IterationStatement>node.parent).statement === node) {
|
||||
// allow unlabeled break/continue inside loops
|
||||
permittedJumps |= PermittedJumps.Break | PermittedJumps.Continue;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
switch (node.kind) {
|
||||
case SyntaxKind.IfStatement:
|
||||
permittedJumps = PermittedJumps.None;
|
||||
break;
|
||||
case SyntaxKind.TryStatement:
|
||||
// forbid all jumps inside try blocks
|
||||
permittedJumps = PermittedJumps.None;
|
||||
break;
|
||||
case SyntaxKind.Block:
|
||||
if (node.parent && node.parent.kind === SyntaxKind.TryStatement && (<TryStatement>node).finallyBlock === node) {
|
||||
// allow unconditional returns from finally blocks
|
||||
permittedJumps = PermittedJumps.Return;
|
||||
}
|
||||
break;
|
||||
case SyntaxKind.CaseClause:
|
||||
// allow unlabeled break inside case clauses
|
||||
permittedJumps |= PermittedJumps.Break;
|
||||
break;
|
||||
default:
|
||||
if (isIterationStatement(node, /*lookInLabeledStatements*/ false)) {
|
||||
// allow unlabeled break/continue inside loops
|
||||
permittedJumps |= PermittedJumps.Break | PermittedJumps.Continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
switch (node.kind) {
|
||||
@ -405,7 +391,7 @@ namespace ts.refactor.extractMethod {
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (!(permittedJumps & (SyntaxKind.BreakStatement ? PermittedJumps.Break : PermittedJumps.Continue))) {
|
||||
if (!(permittedJumps & (node.kind === SyntaxKind.BreakStatement ? PermittedJumps.Break : PermittedJumps.Continue))) {
|
||||
// attempt to break or continue in a forbidden context
|
||||
(errors || (errors = [])).push(createDiagnosticForNode(node, Messages.CannotExtractRangeContainingConditionalBreakOrContinueStatements));
|
||||
}
|
||||
|
||||
31
tests/baselines/reference/extractMethod/extractMethod22.ts
Normal file
31
tests/baselines/reference/extractMethod/extractMethod22.ts
Normal file
@ -0,0 +1,31 @@
|
||||
// ==ORIGINAL==
|
||||
function test() {
|
||||
try {
|
||||
}
|
||||
finally {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
// ==SCOPE::function 'test'==
|
||||
function test() {
|
||||
try {
|
||||
}
|
||||
finally {
|
||||
return /*RENAME*/newFunction();
|
||||
}
|
||||
|
||||
function newFunction() {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
// ==SCOPE::global scope==
|
||||
function test() {
|
||||
try {
|
||||
}
|
||||
finally {
|
||||
return /*RENAME*/newFunction();
|
||||
}
|
||||
}
|
||||
function newFunction() {
|
||||
return 1;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user