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 unused label #24037

Merged
3 commits merged into from
May 18, 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
8 changes: 8 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -4249,5 +4249,13 @@
"Move to a new file": {
"category": "Message",
"code": 95049
},
"Remove unused label": {
"category": "Message",
"code": 95050
},
"Remove all unused labels": {
"category": "Message",
"code": 95051
}
}
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/fixUnusedLabel.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/fixUnusedLabel.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/fixUnusedLabel.ts",
"../services/codefixes/fixJSDocTypes.ts",
"../services/codefixes/fixAwaitInSyncFunction.ts",
"../services/codefixes/disableJsDiagnostics.ts",
Expand Down
20 changes: 20 additions & 0 deletions src/services/codefixes/fixUnusedLabel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* @internal */
namespace ts.codefix {
const fixId = "fixUnusedLabel";
const errorCodes = [Diagnostics.Unused_label.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_unused_label, fixId, Diagnostics.Remove_all_unused_labels)];
},
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 = cast(token.parent, isLabeledStatement).statement;
changes.deleteRange(sourceFile, { pos: token.getStart(sourceFile), end: statement.getStart(sourceFile) });
Copy link
Contributor

Choose a reason for hiding this comment

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

this will remove comments on the statement before the label.. if you are deleting a range, why not just delete from token.pos to :.end?

Copy link
Author

Choose a reason for hiding this comment

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

I wanted to remove the space between the : and the statement following it. token.getStart should not include any comments preceding token. (But token.pos would.)

Copy link
Member

Choose a reason for hiding this comment

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

/*not deleted*/ label /*deleted*/ : /*deleted*/ call();?

}
}
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/fixUnusedLabel.ts",
"codefixes/fixJSDocTypes.ts",
"codefixes/fixAwaitInSyncFunction.ts",
"codefixes/disableJsDiagnostics.ts",
Expand Down
11 changes: 11 additions & 0 deletions tests/cases/fourslash/codeFixUnusedLabel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// <reference path='fourslash.ts' />

// @noUnusedLocals: true

////label: while (1) {}

verify.codeFix({
description: "Remove unused label",
newFileContent:
`while (1) {}`,
});
14 changes: 14 additions & 0 deletions tests/cases/fourslash/codeFixUnusedLabel_all.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// <reference path='fourslash.ts' />

// @noUnusedLocals: true

////label1: while (1) {}
Copy link
Member

Choose a reason for hiding this comment

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

What happens for outdented labels on preceding lines?

label:
    code

Does formatting restore the indentation? Otherwise, it seems like you'd end up with code outdented.

Copy link
Author

Choose a reason for hiding this comment

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

Fixed.

////label2: while (1) {}

verify.codeFixAll({
fixId: "fixUnusedLabel",
fixAllDescription: "Remove all unused labels",
newFileContent:
`while (1) {}
while (1) {}`,
});