jexl-lezer 0.4.1 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,401 @@
1
+ # Undefined and Null Type {"dialect": "ts"}
2
+
3
+ let x: undefined
4
+ let y: null
5
+
6
+ ==>
7
+
8
+ Script(
9
+ VariableDeclaration(let,VariableDefinition,TypeAnnotation(
10
+ TypeName)),
11
+ VariableDeclaration(let,VariableDefinition,TypeAnnotation(
12
+ NullType(null))))
13
+
14
+ # Type declaration {"dialect": "ts"}
15
+
16
+ function foo(a: number, b: "literal" | Map<number, boolean>): RegExp[] {}
17
+
18
+ ==>
19
+
20
+ Script(FunctionDeclaration(function, VariableDefinition, ParamList(
21
+ VariableDefinition, TypeAnnotation(TypeName),
22
+ VariableDefinition, TypeAnnotation(UnionType(LiteralType(String), LogicOp, ParameterizedType(TypeName, TypeArgList(TypeName, TypeName))))
23
+ ), TypeAnnotation(ArrayType(TypeName)), Block))
24
+
25
+ # Type predicate {"dialect": "ts"}
26
+
27
+ function isFoo(foo: any): foo is Foo { return true }
28
+
29
+ function assertFoo(foo: any): asserts foo is "string" { return true }
30
+
31
+ ==>
32
+
33
+ Script(
34
+ FunctionDeclaration(function, VariableDefinition, ParamList(
35
+ VariableDefinition, TypeAnnotation(TypeName)
36
+ ), TypePredicate(VariableName, is, TypeName), Block(ReturnStatement(return, BooleanLiteral))),
37
+ FunctionDeclaration(function,VariableDefinition,ParamList(
38
+ VariableDefinition,TypeAnnotation(TypeName)
39
+ ),TypePredicate(asserts,VariableName,is,LiteralType(String)),Block(ReturnStatement(return,BooleanLiteral))))
40
+
41
+ # Type alias {"dialect": "ts"}
42
+
43
+ type Foo<T extends string> = T[]
44
+
45
+ ==>
46
+
47
+ Script(TypeAliasDeclaration(type, TypeDefinition, TypeParamList(TypeDefinition, extends, TypeName), Equals, ArrayType(TypeName)))
48
+
49
+ # Enum declaration {"dialect": "ts"}
50
+
51
+ const enum Type { Red = 1, Blue, Green }
52
+
53
+ ==>
54
+
55
+ Script(EnumDeclaration(const, enum, TypeDefinition, EnumBody(PropertyName, Equals, Number, PropertyName, PropertyName)))
56
+
57
+ # Interface declaration {"dialect": "ts"}
58
+
59
+ interface Foo {
60
+ readonly a: number
61
+ b(arg: string): void
62
+ (call: number): boolean
63
+ new (): Foo
64
+ readonly [x: string]: number
65
+ }
66
+
67
+ ==>
68
+
69
+ Script(InterfaceDeclaration(interface, TypeDefinition, ObjectType(
70
+ PropertyType(readonly, PropertyDefinition, TypeAnnotation(TypeName)),
71
+ MethodType(PropertyDefinition, ParamList(VariableDefinition, TypeAnnotation(TypeName)), TypeAnnotation(VoidType(void))),
72
+ CallSignature(ParamList(VariableDefinition, TypeAnnotation(TypeName)), TypeAnnotation(TypeName)),
73
+ NewSignature(new,ParamList, TypeAnnotation(TypeName)),
74
+ IndexSignature(readonly, PropertyDefinition, TypeAnnotation(TypeName), TypeAnnotation(TypeName)))))
75
+
76
+ # Call type args {"dialect": "ts"}
77
+
78
+ foo<number, string>() + new Bar<11>()
79
+ x < 10 > 5
80
+
81
+ ==>
82
+
83
+ Script(
84
+ ExpressionStatement(BinaryExpression(
85
+ CallExpression(InstantiationExpression(VariableName, TypeArgList(TypeName, TypeName)), ArgList),
86
+ ArithOp,
87
+ NewExpression(new, InstantiationExpression(VariableName, TypeArgList(LiteralType(Number))), ArgList))),
88
+ ExpressionStatement(BinaryExpression(BinaryExpression(VariableName, CompareOp, Number), CompareOp, Number)))
89
+
90
+ # Advanced types {"dialect": "ts"}
91
+
92
+ let x: typeof X.x | keyof Y & Z["Foo"] | A<string>
93
+ let tuple: [a, b]
94
+ let f: (x: number) => boolean
95
+
96
+ ==>
97
+
98
+ Script(
99
+ VariableDeclaration(let, VariableDefinition, TypeAnnotation(
100
+ UnionType(TypeofType(typeof, MemberExpression(VariableName, PropertyName)), LogicOp,
101
+ IntersectionType(KeyofType(keyof, TypeName), LogicOp, IndexedType(TypeName, LiteralType(String))),
102
+ LogicOp, ParameterizedType(TypeName, TypeArgList(TypeName))))),
103
+ VariableDeclaration(let, VariableDefinition, TypeAnnotation(TupleType(TypeName, TypeName))),
104
+ VariableDeclaration(let, VariableDefinition, TypeAnnotation(FunctionSignature(
105
+ ParamList(VariableDefinition, TypeAnnotation(TypeName)), Arrow, TypeName))))
106
+
107
+ # Prefix union/intersection
108
+
109
+ let x:
110
+ | A
111
+ | B
112
+ | C
113
+ let y: & RegExp & (& Date)
114
+
115
+ ==>
116
+
117
+ Script(
118
+ VariableDeclaration(let,VariableDefinition,TypeAnnotation(
119
+ UnionType(LogicOp,TypeName,LogicOp,TypeName,LogicOp,TypeName))),
120
+ VariableDeclaration(let,VariableDefinition,TypeAnnotation(
121
+ IntersectionType(LogicOp,TypeName,LogicOp,ParenthesizedType(IntersectionType(LogicOp,TypeName))))))
122
+
123
+ # Prefix cast {"dialect": "ts"}
124
+
125
+ <string>foo
126
+
127
+ ==>
128
+
129
+ Script(ExpressionStatement(PrefixCast(TypeName, VariableName)))
130
+
131
+ # No prefix cast in JSX {"dialect": "ts jsx"}
132
+
133
+ <string>foo</string>
134
+
135
+ ==>
136
+
137
+ Script(ExpressionStatement(JSXElement(
138
+ JSXOpenTag(JSXStartTag, JSXBuiltin(JSXIdentifier), JSXEndTag),
139
+ JSXText,
140
+ JSXCloseTag(JSXStartCloseTag, JSXBuiltin(JSXIdentifier), JSXEndTag))))
141
+
142
+ # Class definition {"dialect": "ts"}
143
+
144
+ class Foo<T> extends Bar<T> implements Stuff {
145
+ a: number
146
+ public readonly b: string = "two"
147
+ constructor(readonly x: boolean, public y: number, z: string) {}
148
+ private static blah(): void {}
149
+ }
150
+
151
+ ==>
152
+
153
+ Script(ClassDeclaration(
154
+ class, VariableDefinition, TypeParamList(TypeDefinition),
155
+ extends, VariableName, TypeArgList(TypeName),
156
+ implements TypeName,
157
+ ClassBody(
158
+ PropertyDeclaration(PropertyDefinition, TypeAnnotation(TypeName)),
159
+ PropertyDeclaration(Privacy, readonly, PropertyDefinition, TypeAnnotation(TypeName), Equals, String),
160
+ MethodDeclaration(PropertyDefinition, ParamList(
161
+ readonly, VariableDefinition, TypeAnnotation(TypeName),
162
+ Privacy, VariableDefinition, TypeAnnotation(TypeName),
163
+ VariableDefinition, TypeAnnotation(TypeName)), Block),
164
+ MethodDeclaration(Privacy, static, PropertyDefinition, ParamList, TypeAnnotation(VoidType(void)), Block))))
165
+
166
+ # Arrow with type params {"dialect": "ts"}
167
+
168
+ let x = <T>(arg: T): T => arg
169
+
170
+ ==>
171
+
172
+ Script(VariableDeclaration(let, VariableDefinition, Equals, ArrowFunction(
173
+ TypeParamList(TypeDefinition),
174
+ ParamList(VariableDefinition, TypeAnnotation(TypeName)),
175
+ TypeAnnotation(TypeName),
176
+ Arrow,
177
+ VariableName)))
178
+
179
+ # Template types {"dialect": "ts"}
180
+
181
+ type Tmpl<T> = `${string} ${5}` | `one ${Two}`
182
+
183
+ ==>
184
+
185
+ Script(TypeAliasDeclaration(type, TypeDefinition, TypeParamList(TypeDefinition), Equals,
186
+ UnionType(TemplateType(Interpolation(InterpolationStart,TypeName,InterpolationEnd), Interpolation(InterpolationStart,LiteralType(Number),InterpolationEnd)), LogicOp, TemplateType(Interpolation(InterpolationStart,TypeName,InterpolationEnd)))))
187
+
188
+ # Extending complex types {"dialect": "ts"}
189
+
190
+ class Foo extends A.B<Param> {}
191
+
192
+ ==>
193
+
194
+ Script(ClassDeclaration(class, VariableDefinition,
195
+ extends, MemberExpression(VariableName, PropertyName), TypeArgList(TypeName),
196
+ ClassBody))
197
+
198
+ # Object type {"dialect": "ts"}
199
+
200
+ type A = {a: number, b: number}
201
+ type B = {a: number; b: number;}
202
+
203
+ ==>
204
+
205
+ Script(
206
+ TypeAliasDeclaration(type,TypeDefinition,Equals,ObjectType(
207
+ PropertyType(PropertyDefinition,TypeAnnotation(TypeName)),
208
+ PropertyType(PropertyDefinition,TypeAnnotation(TypeName)))),
209
+ TypeAliasDeclaration(type,TypeDefinition,Equals,ObjectType(
210
+ PropertyType(PropertyDefinition,TypeAnnotation(TypeName)),
211
+ PropertyType(PropertyDefinition,TypeAnnotation(TypeName)))))
212
+
213
+ # Conditional Type {"dialect": "ts"}
214
+
215
+ type X<T> = T extends E ? number : A
216
+
217
+ ==>
218
+
219
+ Script(
220
+ TypeAliasDeclaration(type,TypeDefinition,TypeParamList(TypeDefinition),Equals,
221
+ ConditionalType(TypeName,extends,TypeName,LogicOp,TypeName,LogicOp,TypeName)))
222
+
223
+ # Generic Function Type {"dialect": "ts"}
224
+
225
+ let f: <T>() => T
226
+
227
+ ==>
228
+
229
+ Script(
230
+ VariableDeclaration(let,VariableDefinition,TypeAnnotation(
231
+ FunctionSignature(TypeParamList(TypeDefinition),ParamList,Arrow,TypeName))))
232
+
233
+ # Satisfies operator {"dialect": "ts"}
234
+
235
+ let x = 1 satisfies number
236
+
237
+ ==>
238
+
239
+ Script(VariableDeclaration(let,VariableDefinition,Equals,BinaryExpression(Number,satisfies,TypeName)))
240
+
241
+ # Override modifier on properties {"dialect": "ts"}
242
+
243
+ class A {
244
+ override accessor a;
245
+ static override b = 1;
246
+ override c = 2;
247
+ }
248
+
249
+ ==>
250
+
251
+ Script(ClassDeclaration(class,VariableDefinition,ClassBody(
252
+ PropertyDeclaration(override,accessor,PropertyDefinition),
253
+ PropertyDeclaration(static,override,PropertyDefinition,Equals,Number),
254
+ PropertyDeclaration(override,PropertyDefinition,Equals,Number))))
255
+
256
+ # Class extending expression {"dialect": "ts"}
257
+
258
+ class X extends class {} {}
259
+
260
+ ==>
261
+
262
+ Script(ClassDeclaration(class,VariableDefinition,extends,ClassExpression(class,ClassBody),ClassBody))
263
+
264
+ # Declare syntax {"dialect": "ts"}
265
+
266
+ declare namespace myLib {
267
+ function makeGreeting(s: string): string;
268
+ let numberOfGreetings: number;
269
+ }
270
+
271
+ declare function greet(setting: GreetingSettings): void;
272
+
273
+ declare class Greeter {
274
+ constructor(greeting: string);
275
+ greeting: string;
276
+ showGreeting(): void;
277
+ }
278
+
279
+ class X {
280
+ declare foo();
281
+ declare bar: number;
282
+ }
283
+
284
+ ==>
285
+
286
+ Script(
287
+ AmbientDeclaration(declare,NamespaceDeclaration(namespace,VariableDefinition,Block(
288
+ FunctionDeclaration(function,VariableDefinition,ParamList(VariableDefinition,TypeAnnotation(TypeName)),
289
+ TypeAnnotation(TypeName)),
290
+ VariableDeclaration(let,VariableDefinition,TypeAnnotation(TypeName))))),
291
+ AmbientDeclaration(declare,AmbientFunctionDeclaration(function,VariableDefinition,
292
+ ParamList(VariableDefinition,TypeAnnotation(TypeName)),TypeAnnotation(VoidType(void)))),
293
+ AmbientDeclaration(declare,ClassDeclaration(class,VariableDefinition,ClassBody(
294
+ MethodDeclaration(PropertyDefinition,ParamList(VariableDefinition,TypeAnnotation(TypeName))),
295
+ PropertyDeclaration(PropertyDefinition,TypeAnnotation(TypeName)),
296
+ MethodDeclaration(PropertyDefinition,ParamList,TypeAnnotation(VoidType(void)))))),
297
+ ClassDeclaration(class,VariableDefinition,ClassBody(
298
+ MethodDeclaration(declare,PropertyDefinition,ParamList),
299
+ PropertyDeclaration(declare,PropertyDefinition,TypeAnnotation(TypeName)))))
300
+
301
+ # Declare this in a Function {"dialect": "ts"}
302
+
303
+ function foo(this: User) {}
304
+
305
+ ==>
306
+
307
+ Script(FunctionDeclaration(function,VariableDefinition,ParamList(this,TypeAnnotation(TypeName)),Block))
308
+
309
+ # Prefers type parameters to comparison operators {"dialect": "ts jsx"}
310
+
311
+ let a = useState<string>(1)
312
+ return 2
313
+
314
+ ==>
315
+
316
+ Script(
317
+ VariableDeclaration(let,VariableDefinition,Equals,
318
+ CallExpression(InstantiationExpression(VariableName,TypeArgList(TypeName)),ArgList(Number))),
319
+ ReturnStatement(return,Number))
320
+
321
+ # Type parameters vs JSX {"dialect": "jsx ts"}
322
+
323
+ let a = <T extends any>(f) => null
324
+ let b = <T,>() => 1
325
+
326
+ ==>
327
+
328
+ Script(
329
+ VariableDeclaration(let,VariableDefinition,Equals,ArrowFunction(
330
+ TypeParamList(TypeDefinition,extends,TypeName),ParamList(VariableDefinition),Arrow,null)),
331
+ VariableDeclaration(let,VariableDefinition,Equals,ArrowFunction(
332
+ TypeParamList(TypeDefinition),ParamList,Arrow,Number)))
333
+
334
+ # Destructured parameters in function signature {"dialect": "ts"}
335
+
336
+ type F = ([a, b]: [number, number]) => void
337
+
338
+ ==>
339
+
340
+ Script(TypeAliasDeclaration(type,TypeDefinition,Equals,FunctionSignature(
341
+ ParamList(ArrayPattern(VariableDefinition,VariableDefinition),TypeAnnotation(TupleType(TypeName,TypeName))),
342
+ Arrow,
343
+ VoidType(void))))
344
+
345
+ # Instantiated expression {"dialect": "ts"}
346
+
347
+ let x = a<b>;
348
+
349
+ type Foo = Bar<typeof baz<Bug<Quux>>>;
350
+
351
+ ==>
352
+
353
+ Script(
354
+ VariableDeclaration(let,VariableDefinition,Equals,InstantiationExpression(VariableName,TypeArgList(TypeName))),
355
+ TypeAliasDeclaration(type,TypeDefinition,Equals,ParameterizedType(TypeName,TypeArgList(
356
+ TypeofType(typeof,InstantiationExpression(VariableName,TypeArgList(
357
+ ParameterizedType(TypeName,TypeArgList(TypeName)))))))))
358
+
359
+ # Not instantiated {"dialect": "ts"}
360
+
361
+ let x = a<b>c
362
+
363
+ ==>
364
+
365
+ Script(VariableDeclaration(let,VariableDefinition,Equals,BinaryExpression(
366
+ BinaryExpression(VariableName,CompareOp,VariableName),CompareOp,VariableName)))
367
+
368
+ # Allows computed properties in types {"dialect": "ts"}
369
+
370
+ interface X {
371
+ [Symbol.iterator](): Iterator<number>
372
+ [1]: string
373
+ }
374
+
375
+ ==>
376
+
377
+ Script(InterfaceDeclaration(interface,TypeDefinition,ObjectType(
378
+ MethodType(MemberExpression(VariableName,PropertyName),ParamList,
379
+ TypeAnnotation(ParameterizedType(TypeName,TypeArgList(TypeName)))),
380
+ PropertyType(Number,TypeAnnotation(TypeName)))))
381
+
382
+ # Binary type operators {"dialect": "ts"}
383
+
384
+ log(foo as number, {} satisfies AbstractObjectFactory<ParticleEmitter>)
385
+
386
+ ==>
387
+
388
+ Script(ExpressionStatement(CallExpression(VariableName,ArgList(
389
+ BinaryExpression(VariableName,as,TypeName),
390
+ BinaryExpression(ObjectExpression,satisfies,ParameterizedType(TypeName,TypeArgList(TypeName)))))))
391
+
392
+ # Allows this parameters in function types {"dialect": "ts"}
393
+
394
+ let x: (this: X, expression: string) => Promise<T[]>
395
+
396
+ ==>
397
+
398
+ Script(VariableDeclaration(let,VariableDefinition,TypeAnnotation(FunctionSignature(
399
+ ParamList(this,TypeAnnotation(TypeName),VariableDefinition,TypeAnnotation(TypeName)),
400
+ Arrow,
401
+ ParameterizedType(TypeName,TypeArgList(ArrayType(TypeName)))))))