UNPKG

9.82 kBTypeScriptView Raw
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 */
8import { AST, LiteralPrimitive, ParseSourceSpan, PropertyRead, SafePropertyRead, TmplAstElement, TmplAstNode, TmplAstTemplate, TmplAstTextAttribute } from '@angular/compiler';
9import ts from 'typescript';
10import { AbsoluteFsPath } from '../../../../src/ngtsc/file_system';
11import { ErrorCode } from '../../diagnostics';
12import { FullTemplateMapping, NgTemplateDiagnostic, TypeCheckableDirectiveMeta } from './api';
13import { GlobalCompletion } from './completion';
14import { PotentialDirective, PotentialImport, PotentialPipe } from './scope';
15import { ElementSymbol, Symbol, TcbLocation, TemplateSymbol } from './symbols';
16/**
17 * Interface to the Angular Template Type Checker to extract diagnostics and intelligence from the
18 * compiler's understanding of component templates.
19 *
20 * This interface is analogous to TypeScript's own `ts.TypeChecker` API.
21 *
22 * In general, this interface supports two kinds of operations:
23 * - updating Type Check Blocks (TCB)s that capture the template in the form of TypeScript code
24 * - querying information about available TCBs, including diagnostics
25 *
26 * Once a TCB is available, information about it can be queried. If no TCB is available to answer a
27 * query, depending on the method either `null` will be returned or an error will be thrown.
28 */
29export interface TemplateTypeChecker {
30 /**
31 * Retrieve the template in use for the given component.
32 */
33 getTemplate(component: ts.ClassDeclaration): TmplAstNode[] | null;
34 /**
35 * Get all `ts.Diagnostic`s currently available for the given `ts.SourceFile`.
36 *
37 * This method will fail (throw) if there are components within the `ts.SourceFile` that do not
38 * have TCBs available.
39 *
40 * Generating a template type-checking program is expensive, and in some workflows (e.g. checking
41 * an entire program before emit), it should ideally only be done once. The `optimizeFor` flag
42 * allows the caller to hint to `getDiagnosticsForFile` (which internally will create a template
43 * type-checking program if needed) whether the caller is interested in just the results of the
44 * single file, or whether they plan to query about other files in the program. Based on this
45 * flag, `getDiagnosticsForFile` will determine how much of the user's program to prepare for
46 * checking as part of the template type-checking program it creates.
47 */
48 getDiagnosticsForFile(sf: ts.SourceFile, optimizeFor: OptimizeFor): ts.Diagnostic[];
49 /**
50 * Given a `shim` and position within the file, returns information for mapping back to a template
51 * location.
52 */
53 getTemplateMappingAtTcbLocation(tcbLocation: TcbLocation): FullTemplateMapping | null;
54 /**
55 * Get all `ts.Diagnostic`s currently available that pertain to the given component.
56 *
57 * This method always runs in `OptimizeFor.SingleFile` mode.
58 */
59 getDiagnosticsForComponent(component: ts.ClassDeclaration): ts.Diagnostic[];
60 /**
61 * Ensures shims for the whole program are generated. This type of operation would be required by
62 * operations like "find references" and "refactor/rename" because references may appear in type
63 * check blocks generated from templates anywhere in the program.
64 */
65 generateAllTypeCheckBlocks(): void;
66 /**
67 * Returns `true` if the given file is in the record of known shims generated by the compiler,
68 * `false` if we cannot find the file in the shim records.
69 */
70 isTrackedTypeCheckFile(filePath: AbsoluteFsPath): boolean;
71 /**
72 * Retrieve the top-level node representing the TCB for the given component.
73 *
74 * This can return `null` if there is no TCB available for the component.
75 *
76 * This method always runs in `OptimizeFor.SingleFile` mode.
77 */
78 getTypeCheckBlock(component: ts.ClassDeclaration): ts.Node | null;
79 /**
80 * Retrieves a `Symbol` for the node in a component's template.
81 *
82 * This method can return `null` if a valid `Symbol` cannot be determined for the node.
83 *
84 * @see Symbol
85 */
86 getSymbolOfNode(node: TmplAstElement, component: ts.ClassDeclaration): ElementSymbol | null;
87 getSymbolOfNode(node: TmplAstTemplate, component: ts.ClassDeclaration): TemplateSymbol | null;
88 getSymbolOfNode(node: AST | TmplAstNode, component: ts.ClassDeclaration): Symbol | null;
89 /**
90 * Get "global" `Completion`s in the given context.
91 *
92 * Global completions are completions in the global context, as opposed to completions within an
93 * existing expression. For example, completing inside a new interpolation expression (`{{|}}`) or
94 * inside a new property binding `[input]="|" should retrieve global completions, which will
95 * include completions from the template's context component, as well as any local references or
96 * template variables which are in scope for that expression.
97 */
98 getGlobalCompletions(context: TmplAstTemplate | null, component: ts.ClassDeclaration, node: AST | TmplAstNode): GlobalCompletion | null;
99 /**
100 * For the given expression node, retrieve a `TcbLocation` that can be used to perform
101 * autocompletion at that point in the expression, if such a location exists.
102 */
103 getExpressionCompletionLocation(expr: PropertyRead | SafePropertyRead, component: ts.ClassDeclaration): TcbLocation | null;
104 /**
105 * For the given node represents a `LiteralPrimitive`(the `TextAttribute` represents a string
106 * literal), retrieve a `TcbLocation` that can be used to perform autocompletion at that point in
107 * the node, if such a location exists.
108 */
109 getLiteralCompletionLocation(strNode: LiteralPrimitive | TmplAstTextAttribute, component: ts.ClassDeclaration): TcbLocation | null;
110 /**
111 * Get basic metadata on the directives which are in scope or can be imported for the given
112 * component.
113 */
114 getPotentialTemplateDirectives(component: ts.ClassDeclaration): PotentialDirective[];
115 /**
116 * Get basic metadata on the pipes which are in scope or can be imported for the given component.
117 */
118 getPotentialPipes(component: ts.ClassDeclaration): PotentialPipe[];
119 /**
120 * Retrieve a `Map` of potential template element tags, to either the `PotentialDirective` that
121 * declares them (if the tag is from a directive/component), or `null` if the tag originates from
122 * the DOM schema.
123 */
124 getPotentialElementTags(component: ts.ClassDeclaration): Map<string, PotentialDirective | null>;
125 /**
126 * In the context of an Angular trait, generate potential imports for a directive.
127 */
128 getPotentialImportsFor(directive: PotentialDirective, inComponent: ts.ClassDeclaration): ReadonlyArray<PotentialImport>;
129 /**
130 * Get the primary decorator for an Angular class (such as @Component). This does not work for
131 * `@Injectable`.
132 */
133 getPrimaryAngularDecorator(target: ts.ClassDeclaration): ts.Decorator | null;
134 /**
135 * Get the class of the NgModule that owns this Angular trait. If the result is `null`, that
136 * probably means the provided component is standalone.
137 */
138 getOwningNgModule(component: ts.ClassDeclaration): ts.ClassDeclaration | null;
139 /**
140 * Retrieve any potential DOM bindings for the given element.
141 *
142 * This returns an array of objects which list both the attribute and property names of each
143 * binding, which are usually identical but can vary if the HTML attribute name is for example a
144 * reserved keyword in JS, like the `for` attribute which corresponds to the `htmlFor` property.
145 */
146 getPotentialDomBindings(tagName: string): {
147 attribute: string;
148 property: string;
149 }[];
150 /**
151 * Retrieve any potential DOM events.
152 */
153 getPotentialDomEvents(tagName: string): string[];
154 /**
155 * Retrieve the type checking engine's metadata for the given directive class, if available.
156 */
157 getDirectiveMetadata(dir: ts.ClassDeclaration): TypeCheckableDirectiveMeta | null;
158 /**
159 * Reset the `TemplateTypeChecker`'s state for the given class, so that it will be recomputed on
160 * the next request.
161 */
162 invalidateClass(clazz: ts.ClassDeclaration): void;
163 /**
164 * Constructs a `ts.Diagnostic` for a given `ParseSourceSpan` within a template.
165 */
166 makeTemplateDiagnostic<T extends ErrorCode>(clazz: ts.ClassDeclaration, sourceSpan: ParseSourceSpan, category: ts.DiagnosticCategory, errorCode: T, message: string, relatedInformation?: {
167 text: string;
168 start: number;
169 end: number;
170 sourceFile: ts.SourceFile;
171 }[]): NgTemplateDiagnostic<T>;
172}
173/**
174 * Describes the scope of the caller's interest in template type-checking results.
175 */
176export declare enum OptimizeFor {
177 /**
178 * Indicates that a consumer of a `TemplateTypeChecker` is only interested in results for a
179 * given file, and wants them as fast as possible.
180 *
181 * Calling `TemplateTypeChecker` methods successively for multiple files while specifying
182 * `OptimizeFor.SingleFile` can result in significant unnecessary overhead overall.
183 */
184 SingleFile = 0,
185 /**
186 * Indicates that a consumer of a `TemplateTypeChecker` intends to query for results pertaining
187 * to the entire user program, and so the type-checker should internally optimize for this case.
188 *
189 * Initial calls to retrieve type-checking information may take longer, but repeated calls to
190 * gather information for the whole user program will be significantly faster with this mode of
191 * optimization.
192 */
193 WholeProgram = 1
194}