Merge pull request #18165 from amcasey/GH18144
Simplify and correct PermittedJumps computation
This commit is contained in:
commit
deefb01c9d
3 changed files with 92 additions and 40 deletions
|
@ -378,6 +378,32 @@ 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."
|
||||
]);
|
||||
|
||||
testExtractMethod("extractMethod1",
|
||||
`namespace A {
|
||||
let x = 1;
|
||||
|
@ -620,6 +646,15 @@ namespace A {
|
|||
let x = 10;
|
||||
[#|x++;
|
||||
return;|]
|
||||
}`);
|
||||
// Return in finally block
|
||||
testExtractMethod("extractMethod22",
|
||||
`function test() {
|
||||
try {
|
||||
}
|
||||
finally {
|
||||
[#|return 1;|]
|
||||
}
|
||||
}`);
|
||||
});
|
||||
|
||||
|
|
|
@ -352,46 +352,32 @@ namespace ts.refactor.extractMethod {
|
|||
return false;
|
||||
}
|
||||
const savedPermittedJumps = permittedJumps;
|
||||
if (node.parent) {
|
||||
switch (node.parent.kind) {
|
||||
|
||||
switch (node.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) {
|
||||
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.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) {
|
||||
if (isIterationStatement(node, /*lookInLabeledStatements*/ false)) {
|
||||
// allow unlabeled break/continue inside loops
|
||||
permittedJumps |= PermittedJumps.Break | PermittedJumps.Continue;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
switch (node.kind) {
|
||||
case SyntaxKind.ThisType:
|
||||
|
@ -417,7 +403,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 newFunction();
|
||||
}
|
||||
|
||||
function newFunction() {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
// ==SCOPE::global scope==
|
||||
function test() {
|
||||
try {
|
||||
}
|
||||
finally {
|
||||
return newFunction();
|
||||
}
|
||||
}
|
||||
function newFunction() {
|
||||
return 1;
|
||||
}
|
Loading…
Reference in a new issue