Skip to content
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

Merged
4 commits merged into from
May 10, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/services/codefixes/fixUnreachableCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,33 @@ namespace ts.codefix {
break;
default:
if (isBlock(statement.parent)) {
split(sliceAfter(statement.parent.statements, statement), s => !isFunctionDeclaration(s), (start, end) => changes.deleteNodeRange(sourceFile, start, end));
split(sliceAfter(statement.parent.statements, statement), shouldRemove, (start, end) => changes.deleteNodeRange(sourceFile, start, end));
}
else {
changes.deleteNode(sourceFile, statement);
Copy link
Member

Choose a reason for hiding this comment

The 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 shouldRemove(s: Statement): boolean {
// Don't remove statements that can validly be used before they appear.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm missing something - why would it be reported as unused if it's used above?

return !isFunctionDeclaration(s) && !isPurelyTypeDeclaration(s) &&
// `var x;` may declare a variable used above
!(isVariableStatement(s) && !(getCombinedNodeFlags(s) & (NodeFlags.Let | NodeFlags.Const)) && s.declarationList.declarations.some(d => !d.initializer));
Copy link
Contributor

@mhegazy mhegazy May 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if it is a var statement it make sure it is not ambient.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's ambient, it should not have an initializer so we won't remove it, right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

declare let x; would be removed, right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, right.
Actually, I can't think of a way a declare statement would be unreachable?

}

function isPurelyTypeDeclaration(s: Statement): boolean {
switch (s.kind) {
case SyntaxKind.InterfaceDeclaration:
case SyntaxKind.TypeAliasDeclaration:
return true;
case SyntaxKind.ModuleDeclaration:
return getModuleInstanceState(s as ModuleDeclaration) !== ModuleInstanceState.Instantiated;
case SyntaxKind.EnumDeclaration:
return hasModifier(s, ModifierFlags.Const);
}
}

function sliceAfter<T>(arr: ReadonlyArray<T>, value: T): ReadonlyArray<T> {
const index = arr.indexOf(value);
Debug.assert(index !== -1);
Expand Down
14 changes: 14 additions & 0 deletions tests/cases/fourslash/codeFixUnreachableCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,27 @@
//// return 1;
//// function f() {}
//// return 2;
//// type T = number;
//// interface I {}
//// const enum E {}
//// enum E {}
//// namespace N { export type T = number; }
//// namespace N { export const x = 0; }
//// var x;
//// var y = 0;
////}

verify.codeFix({
description: "Remove unreachable code",
index: 0,
newFileContent:
`function f() {
return f();
function f() {}
type T = number;
interface I {}
const enum E {}
namespace N { export type T = number; }
var x;
}`,
});