|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {absoluteFrom} from '@angular/compiler-cli'; |
| 10 | +import {NgCompiler} from '@angular/compiler-cli/src/ngtsc/core'; |
| 11 | +import * as tss from 'typescript/lib/tsserverlibrary'; |
| 12 | + |
| 13 | +import {TemplateInfo} from '../utils'; |
| 14 | + |
| 15 | +/** |
| 16 | + * This context is the info includes the `errorCode` at the given span the user selected in the |
| 17 | + * editor and the `NgCompiler` could help to fix it. |
| 18 | + * |
| 19 | + * When the editor tries to provide a code fix for a diagnostic in a span of a template file, this |
| 20 | + * context will be provided to the `CodeActionMeta` which could handle the `errorCode`. |
| 21 | + */ |
| 22 | +export interface CodeActionContext { |
| 23 | + templateInfo: TemplateInfo; |
| 24 | + compiler: NgCompiler; |
| 25 | + start: number; |
| 26 | + end: number; |
| 27 | + errorCode: number; |
| 28 | + formatOptions: tss.FormatCodeSettings; |
| 29 | + preferences: tss.UserPreferences; |
| 30 | + tsLs: tss.LanguageService; |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * This context is the info includes all diagnostics in the `scope` and the `NgCompiler` that could |
| 35 | + * help to fix it. |
| 36 | + * |
| 37 | + * When the editor tries to fix the all same type of diagnostics selected by the user in the |
| 38 | + * `scope`, this context will be provided to the `CodeActionMeta` which could handle the `fixId`. |
| 39 | + */ |
| 40 | +export interface CodeFixAllContext { |
| 41 | + scope: tss.CombinedCodeFixScope; |
| 42 | + compiler: NgCompiler; |
| 43 | + // https://github.com/microsoft/TypeScript/blob/5c4caafc2a2d0fceb03fce80fb14d3ee4407d918/src/services/types.ts#L781-L785 |
| 44 | + fixId: string; |
| 45 | + formatOptions: tss.FormatCodeSettings; |
| 46 | + preferences: tss.UserPreferences; |
| 47 | + tsLs: tss.LanguageService; |
| 48 | + diagnostics: tss.Diagnostic[]; |
| 49 | +} |
| 50 | + |
| 51 | +export interface CodeActionMeta { |
| 52 | + errorCodes: Array<number>; |
| 53 | + getCodeActions: (context: CodeActionContext) => readonly tss.CodeFixAction[]; |
| 54 | + fixIds: FixIdForCodeFixesAll[]; |
| 55 | + getAllCodeActions: (context: CodeFixAllContext) => tss.CombinedCodeActions; |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * Convert the span of `textChange` in the TCB to the span of the template. |
| 60 | + */ |
| 61 | +export function convertFileTextChangeInTcb( |
| 62 | + changes: readonly tss.FileTextChanges[], compiler: NgCompiler): tss.FileTextChanges[] { |
| 63 | + const ttc = compiler.getTemplateTypeChecker(); |
| 64 | + const fileTextChanges: tss.FileTextChanges[] = []; |
| 65 | + for (const fileTextChange of changes) { |
| 66 | + if (!ttc.isTrackedTypeCheckFile(absoluteFrom(fileTextChange.fileName))) { |
| 67 | + fileTextChanges.push(fileTextChange); |
| 68 | + continue; |
| 69 | + } |
| 70 | + const textChanges: tss.TextChange[] = []; |
| 71 | + let fileName: string|undefined; |
| 72 | + const seenTextChangeInTemplate = new Set<string>(); |
| 73 | + for (const textChange of fileTextChange.textChanges) { |
| 74 | + const templateMap = ttc.getTemplateMappingAtTcbLocation({ |
| 75 | + tcbPath: absoluteFrom(fileTextChange.fileName), |
| 76 | + isShimFile: true, |
| 77 | + positionInFile: textChange.span.start, |
| 78 | + }); |
| 79 | + if (templateMap === null) { |
| 80 | + continue; |
| 81 | + } |
| 82 | + const mapping = templateMap.templateSourceMapping; |
| 83 | + if (mapping.type === 'external') { |
| 84 | + fileName = mapping.templateUrl; |
| 85 | + } else if (mapping.type === 'direct') { |
| 86 | + fileName = mapping.node.getSourceFile().fileName; |
| 87 | + } else { |
| 88 | + continue; |
| 89 | + } |
| 90 | + const start = templateMap.span.start.offset; |
| 91 | + const length = templateMap.span.end.offset - templateMap.span.start.offset; |
| 92 | + const changeSpanKey = `${start},${length}`; |
| 93 | + if (seenTextChangeInTemplate.has(changeSpanKey)) { |
| 94 | + continue; |
| 95 | + } |
| 96 | + seenTextChangeInTemplate.add(changeSpanKey); |
| 97 | + textChanges.push({ |
| 98 | + newText: textChange.newText, |
| 99 | + span: { |
| 100 | + start, |
| 101 | + length, |
| 102 | + }, |
| 103 | + }); |
| 104 | + } |
| 105 | + if (fileName === undefined) { |
| 106 | + continue; |
| 107 | + } |
| 108 | + fileTextChanges.push({ |
| 109 | + fileName, |
| 110 | + isNewFile: fileTextChange.isNewFile, |
| 111 | + textChanges, |
| 112 | + }); |
| 113 | + } |
| 114 | + return fileTextChanges; |
| 115 | +} |
| 116 | + |
| 117 | +/** |
| 118 | + * 'fix all' is only available when there are multiple diagnostics that the code action meta |
| 119 | + * indicates it can fix. |
| 120 | + */ |
| 121 | +export function isFixAllAvailable(meta: CodeActionMeta, diagnostics: tss.Diagnostic[]) { |
| 122 | + const errorCodes = meta.errorCodes; |
| 123 | + let maybeFixableDiagnostics = 0; |
| 124 | + for (const diag of diagnostics) { |
| 125 | + if (errorCodes.includes(diag.code)) maybeFixableDiagnostics++; |
| 126 | + if (maybeFixableDiagnostics > 1) return true; |
| 127 | + } |
| 128 | + |
| 129 | + return false; |
| 130 | +} |
| 131 | + |
| 132 | +export enum FixIdForCodeFixesAll { |
| 133 | + FIX_SPELLING = 'fixSpelling', |
| 134 | + FIX_MISSING_MEMBER = 'fixMissingMember', |
| 135 | +} |
0 commit comments