forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(language-service): add code fix for unused standalone imports (a…
…ngular#57605) Adds an automatic code fix to the language service that will remove unused standalone imports. PR Close angular#57605
- Loading branch information
1 parent
a2e4ee0
commit 8da9fb4
Showing
4 changed files
with
171 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
76 changes: 76 additions & 0 deletions
76
packages/language-service/src/codefixes/fix_unused_standalone_imports.ts
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,76 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {ErrorCode, ngErrorCode} from '@angular/compiler-cli/src/ngtsc/diagnostics'; | ||
import tss from 'typescript'; | ||
|
||
import {CodeActionMeta, FixIdForCodeFixesAll} from './utils'; | ||
import {findFirstMatchingNode} from '../utils/ts_utils'; | ||
|
||
/** | ||
* Fix for [unused standalone imports](https://angular.io/extended-diagnostics/NG8113) | ||
*/ | ||
export const fixUnusedStandaloneImportsMeta: CodeActionMeta = { | ||
errorCodes: [ngErrorCode(ErrorCode.UNUSED_STANDALONE_IMPORTS)], | ||
getCodeActions: () => [], | ||
fixIds: [FixIdForCodeFixesAll.FIX_UNUSED_STANDALONE_IMPORTS], | ||
getAllCodeActions: ({diagnostics}) => { | ||
const changes: tss.FileTextChanges[] = []; | ||
|
||
for (const diag of diagnostics) { | ||
const {start, length, file, relatedInformation} = diag; | ||
if (file === undefined || start === undefined || length == undefined) { | ||
continue; | ||
} | ||
|
||
const node = findFirstMatchingNode(file, { | ||
filter: (current): current is tss.ArrayLiteralExpression => | ||
current.getStart() === start && | ||
current.getWidth() === length && | ||
tss.isArrayLiteralExpression(current), | ||
}); | ||
|
||
if (node === null) { | ||
continue; | ||
} | ||
|
||
let newText: string; | ||
|
||
// If `relatedInformation` is empty, it means that all the imports are unused. | ||
// Replace the array with an empty array. | ||
if (relatedInformation === undefined || relatedInformation.length === 0) { | ||
newText = '[]'; | ||
} else { | ||
// Otherwise each `relatedInformation` entry points to an unused import that should be | ||
// filtered out. We make a set of ranges corresponding to nodes which will be deleted and | ||
// remove all nodes that belong to the set. | ||
const excludeRanges = new Set( | ||
relatedInformation.map((info) => `${info.start}-${info.length}`), | ||
); | ||
const newArray = tss.factory.updateArrayLiteralExpression( | ||
node, | ||
node.elements.filter((el) => !excludeRanges.has(`${el.getStart()}-${el.getWidth()}`)), | ||
); | ||
|
||
newText = tss.createPrinter().printNode(tss.EmitHint.Unspecified, newArray, file); | ||
} | ||
|
||
changes.push({ | ||
fileName: file.fileName, | ||
textChanges: [ | ||
{ | ||
span: {start, length}, | ||
newText, | ||
}, | ||
], | ||
}); | ||
} | ||
|
||
return {changes}; | ||
}, | ||
}; |
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