forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathast_value.ts
More file actions
324 lines (285 loc) · 10.6 KB
/
Copy pathast_value.ts
File metadata and controls
324 lines (285 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/**
* @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 * as o from '@angular/compiler';
import {FatalLinkerError} from '../fatal_linker_error';
import {AstHost, Range} from './ast_host';
/**
* Represents only those types in `T` that are object types.
*/
type ObjectType<T> = Extract<T, object>;
/**
* Represents the value type of an object literal.
*/
type ObjectValueType<T> = T extends Record<string, infer R>? R : never;
/**
* Represents the value type of an array literal.
*/
type ArrayValueType<T> = T extends Array<infer R>? R : never;
/**
* Ensures that `This` has its generic type `Actual` conform to the expected generic type in
* `Expected`, to disallow calling a method if the generic type does not conform.
*/
type ConformsTo<This, Actual, Expected> = Actual extends Expected ? This : never;
/**
* Ensures that `This` is an `AstValue` whose generic type conforms to `Expected`, to disallow
* calling a method if the value's type does not conform.
*/
type HasValueType<This, Expected> =
This extends AstValue<infer Actual, any>? ConformsTo<This, Actual, Expected>: never;
/**
* Represents only the string keys of type `T`.
*/
type PropertyKey<T> = keyof T&string;
/**
* This helper class wraps an object expression along with an `AstHost` object, exposing helper
* methods that make it easier to extract the properties of the object.
*
* The generic `T` is used as reference type of the expected structure that is represented by this
* object. It does not achieve full type-safety for the provided operations in correspondence with
* `T`; its main goal is to provide references to a documented type and ensure that the properties
* that are read from the object are present.
*
* Unfortunately, the generic types are unable to prevent reading an optional property from the
* object without first having called `has` to ensure that the property exists. This is one example
* of where full type-safety is not achieved.
*/
export class AstObject<T extends object, TExpression> {
/**
* Create a new `AstObject` from the given `expression` and `host`.
*/
static parse<T extends object, TExpression>(expression: TExpression, host: AstHost<TExpression>):
AstObject<T, TExpression> {
const obj = host.parseObjectLiteral(expression);
return new AstObject(expression, obj, host);
}
private constructor(
readonly expression: TExpression, private obj: Map<string, TExpression>,
private host: AstHost<TExpression>) {}
/**
* Returns true if the object has a property called `propertyName`.
*/
has(propertyName: PropertyKey<T>): boolean {
return this.obj.has(propertyName);
}
/**
* Returns the number value of the property called `propertyName`.
*
* Throws an error if there is no such property or the property is not a number.
*/
getNumber<K extends PropertyKey<T>>(this: ConformsTo<this, T[K], number>, propertyName: K):
number {
return this.host.parseNumericLiteral(this.getRequiredProperty(propertyName));
}
/**
* Returns the string value of the property called `propertyName`.
*
* Throws an error if there is no such property or the property is not a string.
*/
getString<K extends PropertyKey<T>>(this: ConformsTo<this, T[K], string>, propertyName: K):
string {
return this.host.parseStringLiteral(this.getRequiredProperty(propertyName));
}
/**
* Returns the boolean value of the property called `propertyName`.
*
* Throws an error if there is no such property or the property is not a boolean.
*/
getBoolean<K extends PropertyKey<T>>(this: ConformsTo<this, T[K], boolean>, propertyName: K):
boolean {
return this.host.parseBooleanLiteral(this.getRequiredProperty(propertyName)) as any;
}
/**
* Returns the nested `AstObject` parsed from the property called `propertyName`.
*
* Throws an error if there is no such property or the property is not an object.
*/
getObject<K extends PropertyKey<T>>(this: ConformsTo<this, T[K], object>, propertyName: K):
AstObject<ObjectType<T[K]>, TExpression> {
const expr = this.getRequiredProperty(propertyName);
const obj = this.host.parseObjectLiteral(expr);
return new AstObject(expr, obj, this.host);
}
/**
* Returns an array of `AstValue` objects parsed from the property called `propertyName`.
*
* Throws an error if there is no such property or the property is not an array.
*/
getArray<K extends PropertyKey<T>>(this: ConformsTo<this, T[K], unknown[]>, propertyName: K):
AstValue<ArrayValueType<T[K]>, TExpression>[] {
const arr = this.host.parseArrayLiteral(this.getRequiredProperty(propertyName));
return arr.map(entry => new AstValue(entry, this.host));
}
/**
* Returns a `WrappedNodeExpr` object that wraps the expression at the property called
* `propertyName`.
*
* Throws an error if there is no such property.
*/
getOpaque(propertyName: PropertyKey<T>): o.WrappedNodeExpr<TExpression> {
return new o.WrappedNodeExpr(this.getRequiredProperty(propertyName));
}
/**
* Returns the raw `TExpression` value of the property called `propertyName`.
*
* Throws an error if there is no such property.
*/
getNode(propertyName: PropertyKey<T>): TExpression {
return this.getRequiredProperty(propertyName);
}
/**
* Returns an `AstValue` that wraps the value of the property called `propertyName`.
*
* Throws an error if there is no such property.
*/
getValue<K extends PropertyKey<T>>(propertyName: K): AstValue<T[K], TExpression> {
return new AstValue(this.getRequiredProperty(propertyName), this.host);
}
/**
* Converts the AstObject to a raw JavaScript object, mapping each property value (as an
* `AstValue`) to the generic type (`T`) via the `mapper` function.
*/
toLiteral<V>(mapper: (value: AstValue<ObjectValueType<T>, TExpression>) => V): Record<string, V> {
const result: Record<string, V> = {};
for (const [key, expression] of this.obj) {
result[key] = mapper(new AstValue(expression, this.host));
}
return result;
}
/**
* Converts the AstObject to a JavaScript Map, mapping each property value (as an
* `AstValue`) to the generic type (`T`) via the `mapper` function.
*/
toMap<V>(mapper: (value: AstValue<ObjectValueType<T>, TExpression>) => V): Map<string, V> {
const result = new Map<string, V>();
for (const [key, expression] of this.obj) {
result.set(key, mapper(new AstValue(expression, this.host)));
}
return result;
}
private getRequiredProperty(propertyName: PropertyKey<T>): TExpression {
if (!this.obj.has(propertyName)) {
throw new FatalLinkerError(
this.expression, `Expected property '${propertyName}' to be present.`);
}
return this.obj.get(propertyName)!;
}
}
/**
* This helper class wraps an `expression`, exposing methods that use the `host` to give
* access to the underlying value of the wrapped expression.
*
* The generic `T` is used as reference type of the expected type that is represented by this value.
* It does not achieve full type-safety for the provided operations in correspondence with `T`; its
* main goal is to provide references to a documented type.
*/
export class AstValue<T, TExpression> {
constructor(readonly expression: TExpression, private host: AstHost<TExpression>) {}
/**
* Get the name of the symbol represented by the given expression node, or `null` if it is not a
* symbol.
*/
getSymbolName(): string|null {
return this.host.getSymbolName(this.expression);
}
/**
* Is this value a number?
*/
isNumber(): boolean {
return this.host.isNumericLiteral(this.expression);
}
/**
* Parse the number from this value, or error if it is not a number.
*/
getNumber(this: HasValueType<this, number>): number {
return this.host.parseNumericLiteral(this.expression);
}
/**
* Is this value a string?
*/
isString(): boolean {
return this.host.isStringLiteral(this.expression);
}
/**
* Parse the string from this value, or error if it is not a string.
*/
getString(this: HasValueType<this, string>): string {
return this.host.parseStringLiteral(this.expression);
}
/**
* Is this value a boolean?
*/
isBoolean(): boolean {
return this.host.isBooleanLiteral(this.expression);
}
/**
* Parse the boolean from this value, or error if it is not a boolean.
*/
getBoolean(this: HasValueType<this, boolean>): boolean {
return this.host.parseBooleanLiteral(this.expression);
}
/**
* Is this value an object literal?
*/
isObject(): boolean {
return this.host.isObjectLiteral(this.expression);
}
/**
* Parse this value into an `AstObject`, or error if it is not an object literal.
*/
getObject(this: HasValueType<this, object>): AstObject<ObjectType<T>, TExpression> {
return AstObject.parse(this.expression, this.host);
}
/**
* Is this value an array literal?
*/
isArray(): boolean {
return this.host.isArrayLiteral(this.expression);
}
/**
* Parse this value into an array of `AstValue` objects, or error if it is not an array literal.
*/
getArray(this: HasValueType<this, unknown[]>): AstValue<ArrayValueType<T>, TExpression>[] {
const arr = this.host.parseArrayLiteral(this.expression);
return arr.map(entry => new AstValue(entry, this.host));
}
/**
* Is this value a function expression?
*/
isFunction(): boolean {
return this.host.isFunctionExpression(this.expression);
}
/**
* Extract the return value as an `AstValue` from this value as a function expression, or error if
* it is not a function expression.
*/
getFunctionReturnValue<R>(this: HasValueType<this, Function>): AstValue<R, TExpression> {
return new AstValue(this.host.parseReturnValue(this.expression), this.host);
}
isCallExpression(): boolean {
return this.host.isCallExpression(this.expression);
}
getCallee(): AstValue<unknown, TExpression> {
return new AstValue(this.host.parseCallee(this.expression), this.host);
}
getArguments(): AstValue<unknown, TExpression>[] {
const args = this.host.parseArguments(this.expression);
return args.map(arg => new AstValue(arg, this.host));
}
/**
* Return the `TExpression` of this value wrapped in a `WrappedNodeExpr`.
*/
getOpaque(): o.WrappedNodeExpr<TExpression> {
return new o.WrappedNodeExpr(this.expression);
}
/**
* Get the range of the location of this value in the original source.
*/
getRange(): Range {
return this.host.getRange(this.expression);
}
}