UNPKG

9.54 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 */
8/// <amd-module name="@angular/compiler-cli/src/ngtsc/typecheck/api/symbols" />
9import { TmplAstElement, TmplAstReference, TmplAstTemplate, TmplAstVariable } from '@angular/compiler';
10import ts from 'typescript';
11import { AbsoluteFsPath } from '../../file_system';
12import { SymbolWithValueDeclaration } from '../../util/src/typescript';
13import { DirectiveInScope } from './scope';
14export declare enum SymbolKind {
15 Input = 0,
16 Output = 1,
17 Binding = 2,
18 Reference = 3,
19 Variable = 4,
20 Directive = 5,
21 Element = 6,
22 Template = 7,
23 Expression = 8,
24 DomBinding = 9,
25 Pipe = 10
26}
27/**
28 * A representation of an entity in the `TemplateAst`.
29 */
30export declare type Symbol = InputBindingSymbol | OutputBindingSymbol | ElementSymbol | ReferenceSymbol | VariableSymbol | ExpressionSymbol | DirectiveSymbol | TemplateSymbol | DomBindingSymbol | PipeSymbol;
31/**
32 * A `Symbol` which declares a new named entity in the template scope.
33 */
34export declare type TemplateDeclarationSymbol = ReferenceSymbol | VariableSymbol;
35/**
36 * Information about where a `ts.Node` can be found in the type check file. This can either be
37 * a type-checking shim file, or an original source file for inline type check blocks.
38 */
39export interface TcbLocation {
40 /**
41 * The fully qualified path of the file which contains the generated TypeScript type check
42 * code for the component's template.
43 */
44 tcbPath: AbsoluteFsPath;
45 /**
46 * Whether the type check block exists in a type-checking shim file or is inline.
47 */
48 isShimFile: boolean;
49 /** The location in the file where node appears. */
50 positionInFile: number;
51}
52/**
53 * A generic representation of some node in a template.
54 */
55export interface TsNodeSymbolInfo {
56 /** The `ts.Type` of the template node. */
57 tsType: ts.Type;
58 /** The `ts.Symbol` for the template node */
59 tsSymbol: ts.Symbol | null;
60 /** The position of the most relevant part of the template node. */
61 tcbLocation: TcbLocation;
62}
63/**
64 * A representation of an expression in a component template.
65 */
66export interface ExpressionSymbol {
67 kind: SymbolKind.Expression;
68 /** The `ts.Type` of the expression AST. */
69 tsType: ts.Type;
70 /**
71 * The `ts.Symbol` of the entity. This could be `null`, for example `AST` expression
72 * `{{foo.bar + foo.baz}}` does not have a `ts.Symbol` but `foo.bar` and `foo.baz` both do.
73 */
74 tsSymbol: ts.Symbol | null;
75 /** The position of the most relevant part of the expression. */
76 tcbLocation: TcbLocation;
77}
78/** Represents either an input or output binding in a template. */
79export interface BindingSymbol {
80 kind: SymbolKind.Binding;
81 /** The `ts.Type` of the class member on the directive that is the target of the binding. */
82 tsType: ts.Type;
83 /** The `ts.Symbol` of the class member on the directive that is the target of the binding. */
84 tsSymbol: ts.Symbol;
85 /**
86 * The `DirectiveSymbol` or `ElementSymbol` for the Directive, Component, or `HTMLElement` with
87 * the binding.
88 */
89 target: DirectiveSymbol | ElementSymbol | TemplateSymbol;
90 /** The location in the shim file where the field access for the binding appears. */
91 tcbLocation: TcbLocation;
92}
93/**
94 * A representation of an input binding in a component template.
95 */
96export interface InputBindingSymbol {
97 kind: SymbolKind.Input;
98 /** A single input may be bound to multiple components or directives. */
99 bindings: BindingSymbol[];
100}
101/**
102 * A representation of an output binding in a component template.
103 */
104export interface OutputBindingSymbol {
105 kind: SymbolKind.Output;
106 /** A single output may be bound to multiple components or directives. */
107 bindings: BindingSymbol[];
108}
109/**
110 * A representation of a local reference in a component template.
111 */
112export interface ReferenceSymbol {
113 kind: SymbolKind.Reference;
114 /**
115 * The `ts.Type` of the Reference value.
116 *
117 * `TmplAstTemplate` - The type of the `TemplateRef`
118 * `TmplAstElement` - The `ts.Type` for the `HTMLElement`.
119 * Directive - The `ts.Type` for the class declaration.
120 */
121 tsType: ts.Type;
122 /**
123 * The `ts.Symbol` for the Reference value.
124 *
125 * `TmplAstTemplate` - A `TemplateRef` symbol.
126 * `TmplAstElement` - The symbol for the `HTMLElement`.
127 * Directive - The symbol for the class declaration of the directive.
128 */
129 tsSymbol: ts.Symbol;
130 /**
131 * Depending on the type of the reference, this is one of the following:
132 * - `TmplAstElement` when the local ref refers to the HTML element
133 * - `TmplAstTemplate` when the ref refers to an `ng-template`
134 * - `ts.ClassDeclaration` when the local ref refers to a Directive instance (#ref="myExportAs")
135 */
136 target: TmplAstElement | TmplAstTemplate | ts.ClassDeclaration;
137 /**
138 * The node in the `TemplateAst` where the symbol is declared. That is, node for the `#ref` or
139 * `#ref="exportAs"`.
140 */
141 declaration: TmplAstReference;
142 /**
143 * The location in the shim file of a variable that holds the type of the local ref.
144 * For example, a reference declaration like the following:
145 * ```
146 * var _t1 = document.createElement('div');
147 * var _t2 = _t1; // This is the reference declaration
148 * ```
149 * This `targetLocation` is `[_t1 variable declaration].getStart()`.
150 */
151 targetLocation: TcbLocation;
152 /**
153 * The location in the TCB for the identifier node in the reference variable declaration.
154 * For example, given a variable declaration statement for a template reference:
155 * `var _t2 = _t1`, this location is `[_t2 node].getStart()`. This location can
156 * be used to find references to the variable within the template.
157 */
158 referenceVarLocation: TcbLocation;
159}
160/**
161 * A representation of a context variable in a component template.
162 */
163export interface VariableSymbol {
164 kind: SymbolKind.Variable;
165 /**
166 * The `ts.Type` of the entity.
167 *
168 * This will be `any` if there is no `ngTemplateContextGuard`.
169 */
170 tsType: ts.Type;
171 /**
172 * The `ts.Symbol` for the context variable.
173 *
174 * This will be `null` if there is no `ngTemplateContextGuard`.
175 */
176 tsSymbol: ts.Symbol | null;
177 /**
178 * The node in the `TemplateAst` where the variable is declared. That is, the node for the `let-`
179 * node in the template.
180 */
181 declaration: TmplAstVariable;
182 /**
183 * The location in the shim file for the identifier that was declared for the template variable.
184 */
185 localVarLocation: TcbLocation;
186 /**
187 * The location in the shim file for the initializer node of the variable that represents the
188 * template variable.
189 */
190 initializerLocation: TcbLocation;
191}
192/**
193 * A representation of an element in a component template.
194 */
195export interface ElementSymbol {
196 kind: SymbolKind.Element;
197 /** The `ts.Type` for the `HTMLElement`. */
198 tsType: ts.Type;
199 /** The `ts.Symbol` for the `HTMLElement`. */
200 tsSymbol: ts.Symbol | null;
201 /** A list of directives applied to the element. */
202 directives: DirectiveSymbol[];
203 /** The location in the shim file for the variable that holds the type of the element. */
204 tcbLocation: TcbLocation;
205 templateNode: TmplAstElement;
206}
207export interface TemplateSymbol {
208 kind: SymbolKind.Template;
209 /** A list of directives applied to the element. */
210 directives: DirectiveSymbol[];
211 templateNode: TmplAstTemplate;
212}
213/**
214 * A representation of a directive/component whose selector matches a node in a component
215 * template.
216 */
217export interface DirectiveSymbol extends DirectiveInScope {
218 kind: SymbolKind.Directive;
219 /** The `ts.Type` for the class declaration. */
220 tsType: ts.Type;
221 /** The location in the shim file for the variable that holds the type of the directive. */
222 tcbLocation: TcbLocation;
223}
224/**
225 * A representation of an attribute on an element or template. These bindings aren't currently
226 * type-checked (see `checkTypeOfDomBindings`) so they won't have a `ts.Type`, `ts.Symbol`, or shim
227 * location.
228 */
229export interface DomBindingSymbol {
230 kind: SymbolKind.DomBinding;
231 /** The symbol for the element or template of the text attribute. */
232 host: ElementSymbol | TemplateSymbol;
233}
234/**
235 * A representation for a call to a pipe's transform method in the TCB.
236 */
237export interface PipeSymbol {
238 kind: SymbolKind.Pipe;
239 /** The `ts.Type` of the transform node. */
240 tsType: ts.Type;
241 /**
242 * The `ts.Symbol` for the transform call. This could be `null` when `checkTypeOfPipes` is set to
243 * `false` because the transform call would be of the form `(_pipe1 as any).transform()`
244 */
245 tsSymbol: ts.Symbol | null;
246 /** The position of the transform call in the template. */
247 tcbLocation: TcbLocation;
248 /** The symbol for the pipe class as an instance that appears in the TCB. */
249 classSymbol: ClassSymbol;
250}
251/** Represents an instance of a class found in the TCB, i.e. `var _pipe1: MyPipe = null!; */
252export interface ClassSymbol {
253 /** The `ts.Type` of class. */
254 tsType: ts.Type;
255 /** The `ts.Symbol` for class. */
256 tsSymbol: SymbolWithValueDeclaration;
257 /** The position for the variable declaration for the class instance. */
258 tcbLocation: TcbLocation;
259}