Skip to content

Commit

Permalink
Add code fix to remove unreachable code
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Hanson committed May 10, 2018
1 parent ff177f0 commit 88b7948
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -4245,5 +4245,13 @@
"Convert all 'require' to 'import'": {
"category": "Message",
"code": 95048
},
"Remove unreachable code": {
"category": "Message",
"code": 95049
},
"Remove all unreachable code": {
"category": "Message",
"code": 95050
}
}
1 change: 1 addition & 0 deletions src/harness/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
"../services/codefixes/fixExtendsInterfaceBecomesImplements.ts",
"../services/codefixes/fixForgottenThisPropertyAccess.ts",
"../services/codefixes/fixUnusedIdentifier.ts",
"../services/codefixes/fixUnreachableCode.ts",
"../services/codefixes/fixJSDocTypes.ts",
"../services/codefixes/fixAwaitInSyncFunction.ts",
"../services/codefixes/disableJsDiagnostics.ts",
Expand Down
1 change: 1 addition & 0 deletions src/server/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
"../services/codefixes/fixExtendsInterfaceBecomesImplements.ts",
"../services/codefixes/fixForgottenThisPropertyAccess.ts",
"../services/codefixes/fixUnusedIdentifier.ts",
"../services/codefixes/fixUnreachableCode.ts",
"../services/codefixes/fixJSDocTypes.ts",
"../services/codefixes/fixAwaitInSyncFunction.ts",
"../services/codefixes/disableJsDiagnostics.ts",
Expand Down
1 change: 1 addition & 0 deletions src/server/tsconfig.library.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
"../services/codefixes/fixExtendsInterfaceBecomesImplements.ts",
"../services/codefixes/fixForgottenThisPropertyAccess.ts",
"../services/codefixes/fixUnusedIdentifier.ts",
"../services/codefixes/fixUnreachableCode.ts",
"../services/codefixes/fixJSDocTypes.ts",
"../services/codefixes/fixAwaitInSyncFunction.ts",
"../services/codefixes/disableJsDiagnostics.ts",
Expand Down
34 changes: 34 additions & 0 deletions src/services/codefixes/fixUnreachableCode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* @internal */
namespace ts.codefix {
const fixId = "fixUnreachableCode";
const errorCodes = [Diagnostics.Unreachable_code_detected.code];
registerCodeFix({
errorCodes,
getCodeActions: context => {
const { sourceFile, span } = context;
const changes = textChanges.ChangeTracker.with(context, t => t.deleteNode(sourceFile, getStatement(sourceFile, span.start)));
return [createCodeFixAction(fixId, changes, Diagnostics.Remove_unreachable_code, fixId, Diagnostics.Remove_all_unreachable_code)];
},
fixIds: [fixId],
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => {
changes.deleteNode(diag.file, getStatement(diag.file, diag.start));
}),
});

function getStatement(sourceFile: SourceFile, start: number): Statement {
const token = getTokenAtPosition(sourceFile, start, /*includeJsDocComment*/ false);
const statement = findAncestor(token, isStatement);
Debug.assert(statement.getStart(sourceFile) === token.getStart(sourceFile));

const container = (isBlock(statement.parent) ? statement.parent : statement).parent;
switch (container.kind) {
case SyntaxKind.IfStatement:
return (container as IfStatement).elseStatement ? statement : container as IfStatement;
case SyntaxKind.WhileStatement:
case SyntaxKind.ForStatement:
return container as WhileStatement | ForStatement;
default:
return statement;
}
}
}
1 change: 1 addition & 0 deletions src/services/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
"codefixes/fixExtendsInterfaceBecomesImplements.ts",
"codefixes/fixForgottenThisPropertyAccess.ts",
"codefixes/fixUnusedIdentifier.ts",
"codefixes/fixUnreachableCode.ts",
"codefixes/fixJSDocTypes.ts",
"codefixes/fixAwaitInSyncFunction.ts",
"codefixes/disableJsDiagnostics.ts",
Expand Down
14 changes: 14 additions & 0 deletions tests/cases/fourslash/codeFixUnreachableCode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// <reference path='fourslash.ts' />

////function f() {
//// return 0;
//// return 1;
////}

verify.codeFix({
description: "Remove unreachable code",
newFileContent:
`function f() {
return 0;
}`,
});
37 changes: 37 additions & 0 deletions tests/cases/fourslash/codeFixUnreachableCode_if.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/// <reference path='fourslash.ts' />

////if (false) a;
////if (false) {
//// a;
////}
////
////// No good way to delete just the 'if' part
////if (false) {
//// a;
////} else {
//// b;
////}
////
////while (false) a;
////while (false) {
//// a;
////}
////
////for (let x = 0; false; ++x) a;
////for (let x = 0; false; ++x) {
//// a;
////}

verify.codeFixAll({
fixId: "fixUnreachableCode",
fixAllDescription: "Remove all unreachable code",
newFileContent:
`
// No good way to delete just the 'if' part
if (false) else {
b;
}
`,
});

0 comments on commit 88b7948

Please sign in to comment.