UNPKG

8.44 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/language-service/src/types" />
9import { CompileDirectiveMetadata, CompileDirectiveSummary, CompilePipeSummary, CssSelector, NgAnalyzedModules, Node as HtmlAst, ParseError, Parser, StaticSymbol, TemplateAst } from '@angular/compiler';
10import * as ts from 'typescript';
11import { Span, Symbol, SymbolQuery, SymbolTable } from './symbols';
12export { StaticSymbol } from '@angular/compiler';
13export { BuiltinType, Definition, PipeInfo, Pipes, Signature, Span, Symbol, SymbolDeclaration, SymbolQuery, SymbolTable } from './symbols';
14/**
15 * The information `LanguageService` needs from the `LanguageServiceHost` to describe the content of
16 * a template and the language context the template is in.
17 *
18 * A host interface; see `LanguageServiceHost`.
19 *
20 * @publicApi
21 */
22export interface TemplateSource {
23 /**
24 * The source of the template.
25 */
26 readonly source: string;
27 /**
28 * The span of the template within the source file.
29 */
30 readonly span: Span;
31 /**
32 * A static symbol for the template's component.
33 */
34 readonly type: StaticSymbol;
35 /**
36 * The `SymbolTable` for the members of the component.
37 */
38 readonly members: SymbolTable;
39 /**
40 * A `SymbolQuery` for the context of the template.
41 */
42 readonly query: SymbolQuery;
43 /**
44 * Name of the file that contains the template. Could be `.html` or `.ts`.
45 */
46 readonly fileName: string;
47}
48/**
49 * Error information found getting declaration information
50 *
51 * A host type; see `LanguageServiceHost`.
52 *
53 * @publicApi
54 */
55export interface DeclarationError {
56 /**
57 * The span of the error in the declaration's module.
58 */
59 readonly span: Span;
60 /**
61 * The message to display describing the error or a chain
62 * of messages.
63 */
64 readonly message: string | DiagnosticMessageChain;
65}
66/**
67 * Information about the component declarations.
68 *
69 * A file might contain a declaration without a template because the file contains only
70 * templateUrl references. However, the component declaration might contain errors that
71 * need to be reported such as the template string is missing or the component is not
72 * declared in a module. These error should be reported on the declaration, not the
73 * template.
74 *
75 * A host type; see `LanguageServiceHost`.
76 *
77 * @publicApi
78 */
79export interface Declaration {
80 /**
81 * The static symbol of the compponent being declared.
82 */
83 readonly type: StaticSymbol;
84 /**
85 * The span of the declaration annotation reference (e.g. the 'Component' or 'Directive'
86 * reference).
87 */
88 readonly declarationSpan: Span;
89 /**
90 * Reference to the compiler directive metadata for the declaration.
91 */
92 readonly metadata: CompileDirectiveMetadata;
93 /**
94 * Error reported trying to get the metadata.
95 */
96 readonly errors: DeclarationError[];
97}
98/**
99 * The host for a `LanguageService`. This provides all the `LanguageService` requires to respond
100 * to the `LanguageService` requests.
101 *
102 * This interface describes the requirements of the `LanguageService` on its host.
103 *
104 * The host interface is host language agnostic.
105 *
106 * Adding optional member to this interface or any interface that is described as a
107 * `LanguageServiceHost` interface is not considered a breaking change as defined by SemVer.
108 * Removing a method or changing a member from required to optional will also not be considered a
109 * breaking change.
110 *
111 * If a member is deprecated it will be changed to optional in a minor release before it is
112 * removed in a major release.
113 *
114 * Adding a required member or changing a method's parameters, is considered a breaking change and
115 * will only be done when breaking changes are allowed. When possible, a new optional member will
116 * be added and the old member will be deprecated. The new member will then be made required in
117 * and the old member will be removed only when breaking changes are allowed.
118 *
119 * While an interface is marked as experimental breaking-changes will be allowed between minor
120 * releases. After an interface is marked as stable breaking-changes will only be allowed between
121 * major releases. No breaking changes are allowed between patch releases.
122 *
123 * @publicApi
124 */
125export interface LanguageServiceHost {
126 /**
127 * Return the template source information for all templates in `fileName` or for `fileName` if
128 * it is a template file.
129 */
130 getTemplates(fileName: string): TemplateSource[];
131 /**
132 * Returns the Angular declarations in the given file.
133 */
134 getDeclarations(fileName: string): Declaration[];
135 /**
136 * Return a summary of all Angular modules in the project.
137 */
138 getAnalyzedModules(): NgAnalyzedModules;
139 /**
140 * Return the AST for both HTML and template for the contextFile.
141 */
142 getTemplateAst(template: TemplateSource): AstResult | undefined;
143 /**
144 * Return the template AST for the node that corresponds to the position.
145 */
146 getTemplateAstAtPosition(fileName: string, position: number): AstResult | undefined;
147}
148/**
149 * The type of Angular directive. Used for QuickInfo in template.
150 */
151export declare enum DirectiveKind {
152 COMPONENT = "component",
153 DIRECTIVE = "directive",
154 EVENT = "event"
155}
156/**
157 * ScriptElementKind for completion.
158 */
159export declare enum CompletionKind {
160 ANGULAR_ELEMENT = "angular element",
161 ATTRIBUTE = "attribute",
162 COMPONENT = "component",
163 ELEMENT = "element",
164 ENTITY = "entity",
165 HTML_ATTRIBUTE = "html attribute",
166 HTML_ELEMENT = "html element",
167 KEY = "key",
168 METHOD = "method",
169 PIPE = "pipe",
170 PROPERTY = "property",
171 REFERENCE = "reference",
172 TYPE = "type",
173 VARIABLE = "variable"
174}
175export declare type CompletionEntry = Omit<ts.CompletionEntry, 'kind'> & {
176 kind: CompletionKind;
177};
178/**
179 * A template diagnostics message chain. This is similar to the TypeScript
180 * DiagnosticMessageChain. The messages are intended to be formatted as separate
181 * sentence fragments and indented.
182 *
183 * For compatibility previous implementation, the values are expected to override
184 * toString() to return a formatted message.
185 *
186 * @publicApi
187 */
188export interface DiagnosticMessageChain {
189 /**
190 * The text of the diagnostic message to display.
191 */
192 message: string;
193 /**
194 * The next message in the chain.
195 */
196 next?: DiagnosticMessageChain[];
197}
198/**
199 * An template diagnostic message to display.
200 *
201 * @publicApi
202 */
203export interface Diagnostic {
204 /**
205 * The kind of diagnostic message
206 */
207 kind: ts.DiagnosticCategory;
208 /**
209 * The source span that should be highlighted.
210 */
211 span: Span;
212 /**
213 * The text of the diagnostic message to display or a chain of messages.
214 */
215 message: string | DiagnosticMessageChain;
216}
217/**
218 * An instance of an Angular language service created by `createLanguageService()`.
219 *
220 * The Angular language service implements a subset of methods defined in
221 * The Angular language service implements a subset of methods defined by
222 * the TypeScript language service.
223 *
224 * @publicApi
225 */
226export declare type LanguageService = Pick<ts.LanguageService, 'getCompletionsAtPosition' | 'getDefinitionAndBoundSpan' | 'getQuickInfoAtPosition' | 'getSemanticDiagnostics' | 'getReferencesAtPosition'>;
227/** Information about an Angular template AST. */
228export interface AstResult {
229 htmlAst: HtmlAst[];
230 templateAst: TemplateAst[];
231 directive: CompileDirectiveMetadata;
232 directives: CompileDirectiveSummary[];
233 pipes: CompilePipeSummary[];
234 parseErrors?: ParseError[];
235 expressionParser: Parser;
236 template: TemplateSource;
237}
238/** Information about a directive's selectors. */
239export declare type SelectorInfo = {
240 selectors: CssSelector[];
241 map: Map<CssSelector, CompileDirectiveSummary>;
242};
243export interface SymbolInfo {
244 symbol: Symbol;
245 span: ts.TextSpan;
246 staticSymbol?: StaticSymbol;
247}
248/** TODO: this should probably be merged with AstResult */
249export interface DiagnosticTemplateInfo {
250 fileName?: string;
251 offset: number;
252 query: SymbolQuery;
253 members: SymbolTable;
254 htmlAst: HtmlAst[];
255 templateAst: TemplateAst[];
256 source: string;
257}