fork of hey-api/openapi-ts because I need some additional things
1import type { AnalysisContext } from '@hey-api/codegen-core';
2
3import { py } from '../../ts-python';
4import { PyDsl } from '../base';
5
6const Mixed = PyDsl<py.Statement>;
7
8export class StmtPyDsl extends Mixed {
9 readonly '~dsl' = 'StmtPyDsl';
10
11 protected _inner: py.Expression | py.Statement | PyDsl<any>;
12
13 constructor(inner: py.Expression | py.Statement | PyDsl<any>) {
14 super();
15 this._inner = inner;
16 }
17
18 override analyze(ctx: AnalysisContext): void {
19 super.analyze(ctx);
20 ctx.analyze(this._inner);
21 }
22
23 override toAst() {
24 const node = this.$node(this._inner);
25 if (isStatement(node)) return node;
26 return py.factory.createExpressionStatement(node as py.Expression);
27 }
28}
29
30/** Checks whether a Python AST node is a statement. */
31function isStatement(node: py.Expression | py.Statement): node is py.Statement {
32 const statementKinds = new Set([
33 py.PyNodeKind.Assignment,
34 py.PyNodeKind.AugmentedAssignment,
35 py.PyNodeKind.Block,
36 py.PyNodeKind.BreakStatement,
37 py.PyNodeKind.ClassDeclaration,
38 py.PyNodeKind.Comment,
39 py.PyNodeKind.ContinueStatement,
40 py.PyNodeKind.EmptyStatement,
41 py.PyNodeKind.ExpressionStatement,
42 py.PyNodeKind.ForStatement,
43 py.PyNodeKind.FunctionDeclaration,
44 py.PyNodeKind.IfStatement,
45 py.PyNodeKind.ImportStatement,
46 py.PyNodeKind.RaiseStatement,
47 py.PyNodeKind.ReturnStatement,
48 py.PyNodeKind.TryStatement,
49 py.PyNodeKind.WhileStatement,
50 py.PyNodeKind.WithStatement,
51 ]);
52 return statementKinds.has(node.kind);
53}