-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add code fix to remove unreachable code #24028
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* @internal */ | ||
namespace ts.codefix { | ||
const fixId = "fixUnreachableCode"; | ||
const errorCodes = [Diagnostics.Unreachable_code_detected.code]; | ||
registerCodeFix({ | ||
errorCodes, | ||
getCodeActions(context) { | ||
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, context.sourceFile, context.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) => doChange(changes, diag.file, diag.start)), | ||
}); | ||
|
||
function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, start: number): void { | ||
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: | ||
if ((container as IfStatement).elseStatement) { | ||
if (isBlock(statement.parent)) { | ||
changes.deleteNodeRange(sourceFile, first(statement.parent.statements), last(statement.parent.statements)); | ||
} | ||
else { | ||
changes.replaceNode(sourceFile, statement, createBlock(emptyArray)); | ||
} | ||
break; | ||
} | ||
// falls through | ||
case SyntaxKind.WhileStatement: | ||
case SyntaxKind.ForStatement: | ||
changes.deleteNode(sourceFile, container); | ||
break; | ||
default: | ||
if (isBlock(statement.parent)) { | ||
split(sliceAfter(statement.parent.statements, statement), s => !isFunctionDeclaration(s), (start, end) => changes.deleteNodeRange(sourceFile, start, end)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You probably also want to preserve type alias declarations, interfaces, const enums, type only namespaces, var statements, ... |
||
} | ||
else { | ||
changes.deleteNode(sourceFile, statement); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will a switch case ever be reported as unused? Does that need special handling? What about a catch block? |
||
} | ||
} | ||
} | ||
|
||
function sliceAfter<T>(arr: ReadonlyArray<T>, value: T): ReadonlyArray<T> { | ||
const index = arr.indexOf(value); | ||
Debug.assert(index !== -1); | ||
return arr.slice(index); | ||
} | ||
|
||
// Calls 'cb' with the start and end of each range where 'pred' is true. | ||
function split<T>(arr: ReadonlyArray<T>, pred: (t: T) => boolean, cb: (start: T, end: T) => void): void { | ||
let start: T | undefined; | ||
for (let i = 0; i < arr.length; i++) { | ||
const value = arr[i]; | ||
if (pred(value)) { | ||
start = start || value; | ||
} | ||
else { | ||
if (start) { | ||
cb(start, arr[i - 1]); | ||
start = undefined; | ||
} | ||
} | ||
} | ||
if (start) cb(start, arr[arr.length - 1]); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
////function f() { | ||
//// return f(); | ||
//// return 1; | ||
//// function f() {} | ||
//// return 2; | ||
////} | ||
|
||
verify.codeFix({ | ||
description: "Remove unreachable code", | ||
newFileContent: | ||
`function f() { | ||
return f(); | ||
function f() {} | ||
}`, | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
////if (false) a; | ||
////if (false) { | ||
//// a; | ||
////} | ||
//// | ||
////// No good way to delete just the 'if' part | ||
////if (false) a; else b; | ||
////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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In a perfect world, the output would be |
||
if (false) { | ||
} else { | ||
b; | ||
} | ||
|
||
|
||
`, | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we want to eliminate the whole else-clause?