-
Notifications
You must be signed in to change notification settings - Fork 12.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add code fix to remove unreachable code
- Loading branch information
Andy Hanson
committed
May 10, 2018
1 parent
ff177f0
commit 88b7948
Showing
8 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}`, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
`, | ||
}); |