-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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 unused label (#24037)
* 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
Showing
8 changed files
with
69 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,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 }); | ||
} | ||
} |
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,11 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
// @noUnusedLocals: true | ||
|
||
/////* a */label/* b */:/* c */while (1) {} | ||
|
||
verify.codeFix({ | ||
description: "Remove unused label", | ||
newFileContent: | ||
`/* a */while (1) {}`, | ||
}); |
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,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) {} | ||
}`, | ||
}); |