-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython_nodes.ts
560 lines (431 loc) · 17 KB
/
python_nodes.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
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
/**
* @file all Python node classes have fields on a per-need basis (instead of
* a full fledged python ast). Unused fields are commented out, they are irrelevant for typing conversion.
*/
import * as ts from "typescript"
import {factory, SyntaxKind} from "typescript"
type PrimitiveField = string | number | null
/**
* node data parsed for example from json.
*/
interface NodeData {
_PyType: string
[x: string]: NodeData | NodeData[] | PrimitiveField
}
// interface OfType<NodeDataType extends string>{
// _PyType: NodeDataType
// }
function isNotNodeData<T>(a: T | NodeData): a is T {
return (a ? !a.hasOwnProperty("_PyType") : true)
}
interface ClassDefNodeData extends NodeData {
_PyType: "ClassDef"
}
type NodeField = Node | Node[] | string | number | null
type CalculatedField = boolean
type NodeClass<T extends Node> = { _PyType(): string };
// interface TransformationContext<T> {
//
// }
//
//
// enum TransformationRule{
// Direct
//
// }
/**
* A Python node. See python3.9's ast page for more https://docs.python.org/3/library/ast.html
* This class and the deriving classes is just a subset of the whole ast. Only what we need.
*/
export abstract class Node {
// static transformationRule: TransformationContext<this>;
static _PyType(): string {
return this.name
}
// todo: What if a Python class has some boolean field?
[x: string]: NodeField | CalculatedField | Function
// warning: if you use Webstorm, do not use its "refactor function signature" here
// It'll overwrite the return type of all deriving class's transform method
// Simply change it here by hand
/**
* transform to (a new) typescript Node
*/
public abstract transform(): ts.Node | ts.Node[] | undefined
/**
* factory method. Add more here for each new deriving class
* @constructor
*/
static Create(data: NodeData) {
let node: Node;
switch (data._PyType) {
case "Module":
node = new Module(data)
break;
case "Name":
node = new Name(data)
break;
// Exists in python ast, but a irrelevant abstraction for us.
// case "Expr":
// node = new Expr(data)
// break;
case "Constant":
node = new Constant(data)
break;
case "ClassDef":
node = new ClassDef(data)
break;
case "AnnAssign":
node = new AnnAssign(data)
break;
case "Store":
node = new Store(data)
break;
case "Load":
node = new Load(data)
break;
case "Subscript":
node = new Subscript(data)
break;
case "Tuple":
node = new Tuple(data)
break;
default:
console.debug(`Debug: Ignored irrelevant Python Node: ${data._PyType}`)
node = new AnyNode(data)
}
node.calculateProperties()
return node
}
protected calculateProperties() {
}
private buildNode(fromData: NodeData): void {
for (const [key, value] of Object.entries(fromData)) {
if (key == "_PyType") {
} else {
if (isNotNodeData(value)) {
if (value instanceof Array) {
const fieldToBuild = []
for (const descendantNodeData of value) {
fieldToBuild.push(Node.Create(descendantNodeData))
}
this[key] = fieldToBuild
} else {
this[key] = value
}
} else {
this[key] = Node.Create(value)
}
}
}
}
/**
* @param data parsed from json.
*/
protected constructor(data: NodeData) {
this.buildNode(data)
}
public static IsType<T extends Node>(n: Node, cls: NodeClass<T>): n is T {
return cls._PyType() == n.constructor.name
}
}
/**
* A whole python file (usually? correct me if it's wrong)
* This class is actually unused: we directly grab TypedDict's from a module, ignoring this top level
*/
export class Module extends Node {
body: Node[]
transform(): undefined {
return undefined;
}
}
export class Name extends Node {
id: string
ctx: Load | Store
/**
* This assumes this Name appears as one of the following cases:
* 1. class field type hint
* 2. inside square bracket of List[] or Tuple[]
*/
private transformInLoadContext() {
switch (this.id) {
case "str": {
return factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword)
}
case "int": {
return factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword)
}
case "bool": {
return factory.createKeywordTypeNode(ts.SyntaxKind.BooleanKeyword)
}
case "List":
case "list": {
return factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword)
}
case "tuple":
case "Tuple": {
// a tuple in Load context (e.g. List[Tuple]) doesn't give any information
// thus this case should be transformed to Array<any>
// correct me if there's a better way
return factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword)
}
default: {
return factory.createIdentifier(this.id)
}
}
}
transform(): ts.Identifier | ts.KeywordTypeNode {
if (this.ctx instanceof Store) return factory.createIdentifier(this.id)
else if (this.ctx instanceof Load) return this.transformInLoadContext()
else return undefined
}
}
// fixme: I think a good thing to do is to make all Node classes and deriving classes have all readonly fields
// but either typescript doesn't support it too well, or I don't know how to do it well.
/**
*
*/
export class ClassDef extends Node {
private name: string
private bases: Name[]
private body: Node[]
// calculated fields upon creation
private subclassOfTypedDict: boolean
protected calculateProperties() {
super.calculateProperties();
this.subclassOfTypedDict = this.isSubclassOfTypedDict()
}
transform(): ts.InterfaceDeclaration {
if (this.isSubclassOfTypedDict) {
const members: ts.PropertySignature[] = []
for (const bodyNode of this.body) {
// we are ignoring other nodes (like methods) in a class body
if (Node.IsType(bodyNode, AnnAssign)) {
// ts what the heck? did I break TS here?
// specifically, if type guard in Node.isType works, then ts should know bodyNode here i a AnnAssign
// instead, it seems TS is just confused here and thinks the type guard doesn't work.
// uncomment the following line and see, AnnAssign's target node is actually a Name
// but typescript won't complain
// bodyNode.target = 1
// so we are actually casting ourselves:
members.push((<AnnAssign>bodyNode).transform())
}
}
return factory.createInterfaceDeclaration(undefined, [factory.createModifier(SyntaxKind.ExportKeyword)], factory.createIdentifier(this.name), undefined, undefined, members)
}
return undefined
}
/**
* Is this class a subclass of TypedDict
*/
private isSubclassOfTypedDict(): boolean {
for (const base of this.bases) {
if (base.id == "TypedDict") {
return true
}
}
return false
}
}
// class Expr extends Node {
// // todo:
// transform(): ts.Node | undefined {
// return undefined;
// }
// }
// After processing of our python serialization script, it's pretty much string
/**
* see python39's AST page https://docs.python.org/3/library/ast.html
*/
type ASDLConstant = string | number | boolean
/**
* The only relevant Constant is "eclipse", where value = "...", or python literals
*/
class Constant extends Node {
value: ASDLConstant
// What's confusing is python39's official ast page says "kind?" is a string field
// while test cases always give null
// Maybe it's the fault of our serializing python script
// kind?: null // irrelevant (seems like it's always null)
/**
*
*/
transform() {
switch (typeof this.value) {
case "string":
return factory.createLiteralTypeNode(factory.createStringLiteral(this.value))
case "number":
return factory.createLiteralTypeNode(factory.createNumericLiteral(this.value))
case "boolean":
if (this.value) return factory.createLiteralTypeNode(factory.createTrue())
else return factory.createLiteralTypeNode(factory.createFalse())
default: {
console.error(`An unexpected case happened while converting a Constant, ${this.value} is not one of string, number, or boolean`)
}
}
}
}
// todo: this
class Subscript extends Node {
value: Name
slice: Tuple | Name | Constant
// ctx: Load | Store | Del // irrelevant to our use case
// we only consider the case when this subscript is a type hint
transform(): ts.ArrayTypeNode | ts.TupleTypeNode | ts.UnionTypeNode | ts.LiteralTypeNode {
switch (this.value.id) {
case "list":
case "List": {
if (this.slice instanceof Tuple) return undefined
else if (this.slice instanceof Name) {
const typeOrIdentifier = this.slice.transform()
if (ts.isIdentifierOrPrivateIdentifier(typeOrIdentifier)) {
return factory.createArrayTypeNode(factory.createTypePredicateNode(undefined, typeOrIdentifier, undefined))
} else if (ts.isTypeNode(typeOrIdentifier)) {
return factory.createArrayTypeNode(typeOrIdentifier)
} else {
console.error("An impossible case happened, argument of List is not understood as identifier or keyword")
return undefined
}
}
return undefined
}
case "tuple":
case "Tuple": {
if (this.slice instanceof Tuple) {
if (this.slice.hasTrailingEllipsis) {
// python39 only allow ellipsis to be trailing, not anywhere else
// This means we have an array length HOMOGENEOUS tuple (of a single type)
// see: https://github.com/python/typing/issues/180
// So in Typescript this actually corresponds to an Array
const typeOrIdentifier = this.slice.elts[0].transform()
// Tuple with ellipsis in python is converted to Array in typescript
// Are there idiomatic ellipsis in typescript?
// I actually don't know, pls tell me.
if (ts.isIdentifierOrPrivateIdentifier(typeOrIdentifier)) {
return factory.createArrayTypeNode(factory.createTypePredicateNode(undefined, typeOrIdentifier, undefined))
} else if (ts.isTypeNode(typeOrIdentifier)) {
return factory.createArrayTypeNode(typeOrIdentifier)
} else {
console.error("An impossible case happened: Tuple has trailing ellipsis but the first element is not an understandable Type")
return undefined
}
}
return factory.createTupleTypeNode(this.slice.transform())
} else if (this.slice instanceof Name) {
const typeNode = this.slice.transform()
if (ts.isTypeNode(typeNode)) return factory.createTupleTypeNode([typeNode])
return undefined
}
return undefined
}
case "Literal": {
if (this.slice instanceof Constant) {
return this.slice.transform()
} else if (this.slice instanceof Tuple) {
return factory.createUnionTypeNode(this.slice.transform())
} else if (this.slice instanceof Name) {
// todo: is this possible at all?
console.error("An unexpected case happened, an identifier is found in Literal[]")
return undefined
} else {
// this should be impossible to reach
console.error("An unexpected case happened, Cannot parse the content inside Literal[] correctly")
return undefined
}
}
case "Union": {
if (this.slice instanceof Constant) {
// should not be possible
console.error("An unexpected case happened, an literal is found in Union[]")
return undefined
} else if (this.slice instanceof Tuple) {
return factory.createUnionTypeNode(this.slice.transform())
} else if (this.slice instanceof Name) {
console.error("An unexpected case happened, an identifier is found in Union[]")
return undefined
} else {
console.error("An unexpected case happened, Cannot parse the content inside Union[] correctly")
return undefined
}
}
default: {
console.error(`Ignored type annotation: ${this.value.id}. This might be an unwanted behavior`)
return undefined
}
}
}
}
class Tuple extends Node {
elts: (Name | Subscript | Constant)[] // simpler that what can actually be
// ctx: Load // irrelevant to our use case
// calculated properties
hasTrailingEllipsis: boolean
protected calculateProperties() {
super.calculateProperties();
this.hasTrailingEllipsis = false
// python39 doesn't allow a single ellipsis in Tuple[]
if (this.elts.length > 1) {
const lastElt = this.elts[this.elts.length - 1]
// Note we assume a Constant must be a ellipsis (3 dot notation)
this.hasTrailingEllipsis = (Node.IsType(lastElt, Constant) && lastElt.value == "...")
}
}
transform(): ts.TypeNode[] {
// side note: has trailing ellipsis case is handled from outside
// we can be sure this.hasTrailingEllipsis is False here
// ts dumbdumb, .filter should guarantee the types match and we shouldn't need the casting
return <ts.TypeNode[]>this.elts
.map(elt => {
const node = elt.transform()
let typeNode
if (ts.isTypeNode(node)) typeNode = node
else if (ts.isIdentifierOrPrivateIdentifier(node)) typeNode = factory.createTypePredicateNode(undefined, node, undefined)
else {
console.error("An unexpected case happened during Tuple conversion. Can't parse content inside Tuple[]")
typeNode = undefined
}
return typeNode
}).filter(n => ts.isTypeNode(n))
}
}
class Load extends Node {
transform(): undefined {
return undefined;
}
}
class Store extends Node {
transform(): undefined {
return undefined;
}
}
class AnnAssign extends Node {
target: Name
annotation: Name | Subscript
// value?: Expr
// simple: 0 | 1
/**
* correct if wrong:
* ts.PropertySignature extends ts.TypeElement, I believe the previous one is the right one to use
*/
transform(): ts.PropertySignature | undefined {
const identifierNode = this.target.transform()
let typeNode = this.annotation.transform()
let actualTypeNode
if (ts.isIdentifierOrPrivateIdentifier(typeNode)) {
actualTypeNode = factory.createTypePredicateNode(undefined, typeNode, undefined)
} else actualTypeNode = typeNode
if (ts.isIdentifierOrPrivateIdentifier(identifierNode))
return factory.createPropertySignature(undefined, identifierNode, undefined, actualTypeNode)
return undefined
}
}
/**
* A placeholder for node we don't recognize
*/
class AnyNode extends Node {
static _PyType(): string {
throw new Error("We do not know the type of this node")
}
transform(): undefined {
return undefined;
}
}