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): support to fix invalid banana in box
The Angular compiler will report the invalid banana in box, this code fixes will try to fix the error and all the same errors in the selected file.
- Loading branch information
1 parent
4e10a74
commit c3e4bfa
Showing
5 changed files
with
219 additions
and
8 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
128 changes: 128 additions & 0 deletions
128
packages/language-service/src/codefixes/fix_invalid_banana_in_box.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,128 @@ | ||
/** | ||
* @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 {isExternalResource} from '@angular/compiler-cli/src/ngtsc/metadata'; | ||
import {BoundEvent} from '@angular/compiler/src/render3/r3_ast'; | ||
import * as tss from 'typescript/lib/tsserverlibrary'; | ||
|
||
import {getTargetAtPosition, TargetNodeKind} from '../template_target'; | ||
import {getTemplateInfoAtPosition, TemplateInfo} from '../utils'; | ||
|
||
import {CodeActionMeta, FixIdForCodeFixesAll} from './utils'; | ||
|
||
/** | ||
* fix [invalid banana-in-box](https://angular.io/extended-diagnostics/NG8101) | ||
*/ | ||
export const fixInvalidBananaInBoxMeta: CodeActionMeta = { | ||
errorCodes: [ngErrorCode(ErrorCode.INVALID_BANANA_IN_BOX)], | ||
getCodeActions({start, templateInfo, compiler}) { | ||
const resource = compiler.getComponentResources(templateInfo.component)?.template; | ||
if (resource === undefined) { | ||
return []; | ||
} | ||
|
||
let fileName = resource.expression.getSourceFile().fileName; | ||
if (isExternalResource(resource)) { | ||
fileName = resource.path; | ||
} | ||
|
||
const boundEvent = getTheBoundEventAtPosition(templateInfo, start); | ||
if (boundEvent === null) { | ||
return []; | ||
} | ||
const textChanges = convertBoundEventToTsTextChange(boundEvent); | ||
return [{ | ||
fixName: FixIdForCodeFixesAll.FIX_INVALID_BANANA_IN_BOX, | ||
fixId: FixIdForCodeFixesAll.FIX_INVALID_BANANA_IN_BOX, | ||
fixAllDescription: 'fix all invalid banana-in-box', | ||
description: `fix invalid banana-in-box for '${boundEvent.sourceSpan.toString()}'`, | ||
changes: [{ | ||
fileName, | ||
textChanges, | ||
}], | ||
}]; | ||
}, | ||
fixIds: [FixIdForCodeFixesAll.FIX_INVALID_BANANA_IN_BOX], | ||
getAllCodeActions({diagnostics, compiler}) { | ||
const fileNameToTextChangesMap = new Map<string, tss.TextChange[]>(); | ||
for (const diag of diagnostics) { | ||
const fileName = diag.file?.fileName; | ||
if (fileName === undefined) { | ||
continue; | ||
} | ||
const start = diag.start; | ||
if (start === undefined) { | ||
continue; | ||
} | ||
const templateInfo = getTemplateInfoAtPosition(fileName, start, compiler); | ||
if (templateInfo === undefined) { | ||
continue; | ||
} | ||
const boundEvent = getTheBoundEventAtPosition(templateInfo, start); | ||
if (boundEvent === null) { | ||
continue; | ||
} | ||
|
||
if (!fileNameToTextChangesMap.has(fileName)) { | ||
fileNameToTextChangesMap.set(fileName, []); | ||
} | ||
const fileTextChanges = fileNameToTextChangesMap.get(fileName)!; | ||
const textChanges = convertBoundEventToTsTextChange(boundEvent); | ||
fileTextChanges.push(...textChanges); | ||
} | ||
|
||
const fileTextChanges: tss.FileTextChanges[] = []; | ||
for (const [fileName, textChanges] of fileNameToTextChangesMap) { | ||
fileTextChanges.push({ | ||
fileName, | ||
textChanges, | ||
}); | ||
} | ||
return { | ||
changes: fileTextChanges, | ||
}; | ||
}, | ||
}; | ||
|
||
function getTheBoundEventAtPosition(templateInfo: TemplateInfo, start: number): BoundEvent|null { | ||
// It's safe to get the bound event at the position `start + 1` because the `start` is at the | ||
// start of the diagnostic, and the node outside the attribute key and value spans are skipped by | ||
// the function `getTargetAtPosition`. | ||
// https://github.com/angular/vscode-ng-language-service/blob/8553115972ca40a55602747667c3d11d6f47a6f8/server/src/session.ts#L220 | ||
// https://github.com/angular/angular/blob/4e10a7494130b9bb4772ee8f76b66675867b2145/packages/language-service/src/template_target.ts#L347-L356 | ||
const positionDetail = getTargetAtPosition(templateInfo.template, start + 1); | ||
if (!positionDetail) { | ||
return null; | ||
} | ||
|
||
let node: BoundEvent|null = null; | ||
|
||
if (positionDetail.context.kind === TargetNodeKind.AttributeInKeyContext && | ||
positionDetail.context.node instanceof BoundEvent) { | ||
node = positionDetail.context.node; | ||
} | ||
|
||
return node; | ||
} | ||
|
||
function convertBoundEventToTsTextChange(node: BoundEvent): readonly tss.TextChange[] { | ||
const name = node.name; | ||
const boundSyntax = node.sourceSpan.toString(); | ||
const expectedBoundSyntax = boundSyntax.replace(`(${name})`, `[(${name.slice(1, -1)})]`); | ||
|
||
return [ | ||
{ | ||
span: { | ||
start: node.sourceSpan.start.offset, | ||
length: boundSyntax.length, | ||
}, | ||
newText: expectedBoundSyntax, | ||
}, | ||
]; | ||
} |
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