Skip to content

Commit

Permalink
Add code fix to remove unused label (#24037)
Browse files Browse the repository at this point in the history
* Add code fix to remove unused label

* Test with trivia and fix indentation with dedented label
  • Loading branch information
Andy authored May 18, 2018
1 parent 1df7997 commit 3eb66da
Show file tree
Hide file tree
Showing 8 changed files with 69 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 @@ -4261,5 +4261,13 @@
"Add missing typeof": {
"category": "Message",
"code": 95052
},
"Remove unused label": {
"category": "Message",
"code": 95053
},
"Remove all unused labels": {
"category": "Message",
"code": 95054
}
}
1 change: 1 addition & 0 deletions src/harness/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
"../services/codefixes/fixForgottenThisPropertyAccess.ts",
"../services/codefixes/fixUnusedIdentifier.ts",
"../services/codefixes/fixUnreachableCode.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 @@ -102,6 +102,7 @@
"../services/codefixes/fixForgottenThisPropertyAccess.ts",
"../services/codefixes/fixUnusedIdentifier.ts",
"../services/codefixes/fixUnreachableCode.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 @@ -108,6 +108,7 @@
"../services/codefixes/fixForgottenThisPropertyAccess.ts",
"../services/codefixes/fixUnusedIdentifier.ts",
"../services/codefixes/fixUnreachableCode.ts",
"../services/codefixes/fixUnusedLabel.ts",
"../services/codefixes/fixJSDocTypes.ts",
"../services/codefixes/fixAwaitInSyncFunction.ts",
"../services/codefixes/disableJsDiagnostics.ts",
Expand Down
25 changes: 25 additions & 0 deletions src/services/codefixes/fixUnusedLabel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* @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 labeledStatement = cast(token.parent, isLabeledStatement);
const pos = token.getStart(sourceFile);
const statementPos = labeledStatement.statement.getStart(sourceFile);
// If label is on a separate line, just delete the rest of that line, but not the indentation of the labeled statement.
const end = positionsAreOnSameLine(pos, statementPos, sourceFile) ? statementPos
: skipTrivia(sourceFile.text, findChildOfKind(labeledStatement, SyntaxKind.ColonToken, sourceFile)!.end, /*stopAfterLineBreak*/ true);
changes.deleteRange(sourceFile, { pos, end });
}
}
1 change: 1 addition & 0 deletions src/services/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
"codefixes/fixForgottenThisPropertyAccess.ts",
"codefixes/fixUnusedIdentifier.ts",
"codefixes/fixUnreachableCode.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

/////* a */label/* b */:/* c */while (1) {}

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

// @noUnusedLocals: true

////label1: while (1) {}
////
////function f() {
////label2:
//// while (1) {}
////}

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

0 comments on commit 3eb66da

Please sign in to comment.