fixUnreachableCode: Don't delete whole block when non-first line is unreachable (#25625)
This commit is contained in:
parent
88c6ced3c6
commit
27da9eab8d
|
@ -18,31 +18,33 @@ namespace ts.codefix {
|
|||
Debug.assert(statement.getStart(sourceFile) === token.getStart(sourceFile));
|
||||
|
||||
const container = (isBlock(statement.parent) ? statement.parent : statement).parent;
|
||||
switch (container.kind) {
|
||||
case SyntaxKind.IfStatement:
|
||||
if ((container as IfStatement).elseStatement) {
|
||||
if (isBlock(statement.parent)) {
|
||||
changes.deleteNodeRange(sourceFile, first(statement.parent.statements), last(statement.parent.statements));
|
||||
if (!isBlock(statement.parent) || statement === first(statement.parent.statements)) {
|
||||
switch (container.kind) {
|
||||
case SyntaxKind.IfStatement:
|
||||
if ((container as IfStatement).elseStatement) {
|
||||
if (isBlock(statement.parent)) {
|
||||
break;
|
||||
}
|
||||
else {
|
||||
changes.replaceNode(sourceFile, statement, createBlock(emptyArray));
|
||||
}
|
||||
return;
|
||||
}
|
||||
else {
|
||||
changes.replaceNode(sourceFile, statement, createBlock(emptyArray));
|
||||
}
|
||||
break;
|
||||
}
|
||||
// falls through
|
||||
case SyntaxKind.WhileStatement:
|
||||
case SyntaxKind.ForStatement:
|
||||
changes.delete(sourceFile, container);
|
||||
break;
|
||||
default:
|
||||
if (isBlock(statement.parent)) {
|
||||
const end = start + length;
|
||||
const lastStatement = Debug.assertDefined(lastWhere(sliceAfter(statement.parent.statements, statement), s => s.pos < end));
|
||||
changes.deleteNodeRange(sourceFile, statement, lastStatement);
|
||||
}
|
||||
else {
|
||||
changes.delete(sourceFile, statement);
|
||||
}
|
||||
// falls through
|
||||
case SyntaxKind.WhileStatement:
|
||||
case SyntaxKind.ForStatement:
|
||||
changes.delete(sourceFile, container);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (isBlock(statement.parent)) {
|
||||
const end = start + length;
|
||||
const lastStatement = Debug.assertDefined(lastWhere(sliceAfter(statement.parent.statements, statement), s => s.pos < end));
|
||||
changes.deleteNodeRange(sourceFile, statement, lastStatement);
|
||||
}
|
||||
else {
|
||||
changes.delete(sourceFile, statement);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -22,6 +22,20 @@
|
|||
////for (let x = 0; false; ++x) {
|
||||
//// a;
|
||||
////}
|
||||
////
|
||||
////while (true) {
|
||||
//// 1;
|
||||
//// break;
|
||||
//// 2;
|
||||
////}
|
||||
////
|
||||
////function f() {
|
||||
//// if (true) {
|
||||
//// 1;
|
||||
//// return;
|
||||
//// 2;
|
||||
//// }
|
||||
////}
|
||||
|
||||
verify.codeFixAll({
|
||||
fixId: "fixUnreachableCode",
|
||||
|
@ -36,5 +50,16 @@ if (false) {
|
|||
}
|
||||
|
||||
|
||||
`,
|
||||
|
||||
while (true) {
|
||||
1;
|
||||
break;
|
||||
}
|
||||
|
||||
function f() {
|
||||
if (true) {
|
||||
1;
|
||||
return;
|
||||
}
|
||||
}`,
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue