/
dynamic.ts
127 lines (103 loc) Β· 4.11 KB
/
dynamic.ts
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
/**
* @license
* Copyright Google Inc. 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 ts from 'typescript';
import {Reference} from '../../imports';
/**
* The reason why a value cannot be determined statically.
*/
export const enum DynamicValueReason {
/**
* A value could not be determined statically, because it contains a term that could not be
* determined statically.
* (E.g. a property assignment or call expression where the lhs is a `DynamicValue`, a template
* literal with a dynamic expression, an object literal with a spread assignment which could not
* be determined statically, etc.)
*/
DYNAMIC_INPUT,
/**
* A string could not be statically evaluated.
* (E.g. a dynamically constructed object property name or a template literal expression that
* could not be statically resolved to a primitive value.)
*/
DYNAMIC_STRING,
/**
* An external reference could not be resolved to a value which can be evaluated.
* For example a call expression for a function declared in `.d.ts`, or accessing native globals
* such as `window`.
*/
EXTERNAL_REFERENCE,
/**
* Syntax that `StaticInterpreter` doesn't know how to evaluate, for example a type of
* `ts.Expression` that is not supported.
*/
UNSUPPORTED_SYNTAX,
/**
* A declaration of a `ts.Identifier` could not be found.
*/
UNKNOWN_IDENTIFIER,
/**
* A value could be resolved, but is not an acceptable type for the operation being performed.
*
* For example, attempting to call a non-callable expression.
*/
INVALID_EXPRESSION_TYPE,
/**
* A value could not be determined statically for any reason other the above.
*/
UNKNOWN,
}
/**
* Represents a value which cannot be determined statically.
*/
export class DynamicValue<R = unknown> {
private constructor(
readonly node: ts.Node, readonly reason: R, private code: DynamicValueReason) {}
static fromDynamicInput(node: ts.Node, input: DynamicValue): DynamicValue<DynamicValue> {
return new DynamicValue(node, input, DynamicValueReason.DYNAMIC_INPUT);
}
static fromDynamicString(node: ts.Node): DynamicValue {
return new DynamicValue(node, undefined, DynamicValueReason.DYNAMIC_STRING);
}
static fromExternalReference(node: ts.Node, ref: Reference<ts.Declaration>):
DynamicValue<Reference<ts.Declaration>> {
return new DynamicValue(node, ref, DynamicValueReason.EXTERNAL_REFERENCE);
}
static fromUnsupportedSyntax(node: ts.Node): DynamicValue {
return new DynamicValue(node, undefined, DynamicValueReason.UNSUPPORTED_SYNTAX);
}
static fromUnknownIdentifier(node: ts.Identifier): DynamicValue {
return new DynamicValue(node, undefined, DynamicValueReason.UNKNOWN_IDENTIFIER);
}
static fromInvalidExpressionType(node: ts.Node, value: unknown): DynamicValue<unknown> {
return new DynamicValue(node, value, DynamicValueReason.INVALID_EXPRESSION_TYPE);
}
static fromUnknown(node: ts.Node): DynamicValue {
return new DynamicValue(node, undefined, DynamicValueReason.UNKNOWN);
}
isFromDynamicInput(this: DynamicValue<R>): this is DynamicValue<DynamicValue> {
return this.code === DynamicValueReason.DYNAMIC_INPUT;
}
isFromDynamicString(this: DynamicValue<R>): this is DynamicValue {
return this.code === DynamicValueReason.DYNAMIC_STRING;
}
isFromExternalReference(this: DynamicValue<R>): this is DynamicValue<Reference<ts.Declaration>> {
return this.code === DynamicValueReason.EXTERNAL_REFERENCE;
}
isFromUnsupportedSyntax(this: DynamicValue<R>): this is DynamicValue {
return this.code === DynamicValueReason.UNSUPPORTED_SYNTAX;
}
isFromUnknownIdentifier(this: DynamicValue<R>): this is DynamicValue {
return this.code === DynamicValueReason.UNKNOWN_IDENTIFIER;
}
isFromInvalidExpressionType(this: DynamicValue<R>): this is DynamicValue<unknown> {
return this.code === DynamicValueReason.INVALID_EXPRESSION_TYPE;
}
isFromUnknown(this: DynamicValue<R>): this is DynamicValue {
return this.code === DynamicValueReason.UNKNOWN;
}
}