Skip to content

Commit 8a0bdaa

Browse files
authored
Merge pull request #3612 from hey-api/feat/ts-compiler-1
feat: scaffold ts-compiler folder
2 parents f2ca37e + d139d72 commit 8a0bdaa

36 files changed

Lines changed: 573 additions & 47 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
s = "hello"
22
n = 123
33
b = True
4+
c = False
45
none = None

packages/openapi-python/src/py-compiler/__tests__/nodes/expressions/literal.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ describe('literal expression', () => {
1919
undefined,
2020
py.factory.createLiteral(true),
2121
),
22+
py.factory.createAssignment(
23+
py.factory.createIdentifier('c'),
24+
undefined,
25+
py.factory.createLiteral(false),
26+
),
2227
py.factory.createAssignment(
2328
py.factory.createIdentifier('none'),
2429
undefined,

packages/openapi-python/src/py-compiler/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ import type { PyIdentifier as _PyIdentifier } from './nodes/expressions/identifi
2424
import type { PyKeywordArgument as _PyKeywordArgument } from './nodes/expressions/keywordArg';
2525
import type { PyLambdaExpression as _PyLambdaExpression } from './nodes/expressions/lambda';
2626
import type { PyListExpression as _PyListExpression } from './nodes/expressions/list';
27-
import type { PyLiteral as _PyLiteral } from './nodes/expressions/literal';
27+
import type {
28+
PyLiteral as _PyLiteral,
29+
PyLiteralValue as _PyLiteralValue,
30+
} from './nodes/expressions/literal';
2831
import type { PyMemberExpression as _PyMemberExpression } from './nodes/expressions/member';
2932
import type { PySetExpression as _PySetExpression } from './nodes/expressions/set';
3033
import type { PySubscriptExpression as _PySubscriptExpression } from './nodes/expressions/subscript';
@@ -129,6 +132,9 @@ export namespace py {
129132

130133
// Printer
131134
export type PrinterOptions = _PyPrinterOptions;
135+
136+
// Miscellaneous
137+
export type LiteralValue = _PyLiteralValue;
132138
}
133139

134140
export const py = {

packages/openapi-python/src/py-compiler/nodes/declarations/functionParameter.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,25 @@ import type { PyExpression } from '../expression';
33
import { PyNodeKind } from '../kinds';
44

55
export interface PyFunctionParameter extends PyNodeBase {
6-
annotation?: PyExpression;
76
defaultValue?: PyExpression;
87
kind: PyNodeKind.FunctionParameter;
98
name: string;
9+
type?: PyExpression;
1010
}
1111

1212
export function createFunctionParameter(
1313
name: string,
14-
annotation?: PyExpression,
14+
type?: PyExpression,
1515
defaultValue?: PyExpression,
1616
leadingComments?: ReadonlyArray<string>,
1717
trailingComments?: ReadonlyArray<string>,
1818
): PyFunctionParameter {
1919
return {
20-
annotation,
2120
defaultValue,
2221
kind: PyNodeKind.FunctionParameter,
2322
leadingComments,
2423
name,
2524
trailingComments,
25+
type,
2626
};
2727
}

packages/openapi-python/src/py-compiler/nodes/expressions/literal.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import type { PyNodeBase } from '../base';
22
import { PyNodeKind } from '../kinds';
33

4-
export type LiteralValue = string | number | boolean | null;
4+
export type PyLiteralValue = string | number | boolean | null;
55

66
export interface PyLiteral extends PyNodeBase {
77
kind: PyNodeKind.Literal;
8-
value: LiteralValue;
8+
value: PyLiteralValue;
99
}
1010

1111
export function createLiteral(
12-
value: LiteralValue,
12+
value: PyLiteralValue,
1313
leadingComments?: ReadonlyArray<string>,
1414
trailingComments?: ReadonlyArray<string>,
1515
): PyLiteral {

packages/openapi-python/src/py-compiler/nodes/statements/assignment.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,29 @@ import type { PyExpression } from '../expression';
33
import { PyNodeKind } from '../kinds';
44

55
export interface PyAssignment extends PyNodeBase {
6-
annotation?: PyExpression;
76
kind: PyNodeKind.Assignment;
87
target: PyExpression;
8+
type?: PyExpression;
99
value?: PyExpression;
1010
}
1111

1212
export function createAssignment(
1313
target: PyExpression,
14-
annotation?: PyExpression,
14+
type?: PyExpression,
1515
value?: PyExpression,
1616
leadingComments?: ReadonlyArray<string>,
1717
trailingComments?: ReadonlyArray<string>,
1818
): PyAssignment {
19-
if (!annotation && !value) {
20-
throw new Error('Assignment requires at least annotation or value');
19+
if (!type && !value) {
20+
throw new Error('Assignment requires at least type or value');
2121
}
2222

2323
return {
24-
annotation,
2524
kind: PyNodeKind.Assignment,
2625
leadingComments,
2726
target,
2827
trailingComments,
28+
type,
2929
value,
3030
};
3131
}

packages/openapi-python/src/py-compiler/printer.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ export interface PyPrinterOptions {
55
indentSize?: number;
66
}
77

8+
const DEFAULT_INDENT_SIZE = 4;
89
const PARAMS_MULTILINE_THRESHOLD = 3;
910

1011
export function createPrinter(options?: PyPrinterOptions) {
11-
const indentSize = options?.indentSize ?? 4;
12+
const indentSize = options?.indentSize ?? DEFAULT_INDENT_SIZE;
1213

1314
let indentLevel = 0;
1415

@@ -52,12 +53,12 @@ export function createPrinter(options?: PyPrinterOptions) {
5253
switch (node.kind) {
5354
case PyNodeKind.Assignment: {
5455
const target = printNode(node.target);
55-
if (node.annotation) {
56-
const annotation = printNode(node.annotation);
56+
if (node.type) {
57+
const type = printNode(node.type);
5758
if (node.value) {
58-
parts.push(printLine(`${target}: ${annotation} = ${printNode(node.value)}`));
59+
parts.push(printLine(`${target}: ${type} = ${printNode(node.value)}`));
5960
} else {
60-
parts.push(printLine(`${target}: ${annotation}`));
61+
parts.push(printLine(`${target}: ${type}`));
6162
}
6263
} else {
6364
parts.push(printLine(`${target} = ${printNode(node.value!)}`));
@@ -182,7 +183,7 @@ export function createPrinter(options?: PyPrinterOptions) {
182183
const defPrefix = modifiers ? `${modifiers} def` : 'def';
183184
const formatParameter = (parameter: (typeof node.parameters)[number]): string => {
184185
const children: Array<string> = [parameter.name];
185-
if (parameter.annotation) children.push(`: ${printNode(parameter.annotation)}`);
186+
if (parameter.type) children.push(`: ${printNode(parameter.type)}`);
186187
if (parameter.defaultValue) children.push(` = ${printNode(parameter.defaultValue)}`);
187188
return children.join('');
188189
};
@@ -272,7 +273,7 @@ export function createPrinter(options?: PyPrinterOptions) {
272273
case PyNodeKind.LambdaExpression: {
273274
const parameters = node.parameters.map((parameter) => {
274275
const children: Array<string> = [parameter.name];
275-
if (parameter.annotation) children.push(`: ${printNode(parameter.annotation)}`);
276+
if (parameter.type) children.push(`: ${printNode(parameter.type)}`);
276277
if (parameter.defaultValue) children.push(` = ${printNode(parameter.defaultValue)}`);
277278
return children.join('');
278279
});

packages/openapi-python/src/py-dsl/expr/literal.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@ import type { AnalysisContext } from '@hey-api/codegen-core';
33
import { py } from '../../py-compiler';
44
import { PyDsl } from '../base';
55

6-
export type LiteralValue = string | number | boolean | null;
7-
86
const Mixed = PyDsl<py.Literal>;
97

108
export class LiteralPyDsl extends Mixed {
119
readonly '~dsl' = 'LiteralPyDsl';
1210

13-
protected value: LiteralValue;
11+
protected value: py.LiteralValue;
1412

15-
constructor(value: LiteralValue) {
13+
constructor(value: py.LiteralValue) {
1614
super();
1715
this.value = value;
1816
}

packages/openapi-python/src/py-dsl/stmt/var.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ export class VarPyDsl extends Mixed {
4444
override toAst() {
4545
this.$validate();
4646
const target = this.$node(this.name)!;
47-
const annotation = this.$type();
47+
const type = this.$type();
4848
const value = this.$value();
4949

50-
return py.factory.createAssignment(target, annotation, value);
50+
return py.factory.createAssignment(target, type, value);
5151
}
5252

5353
$validate(): asserts this {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
let x;
2+
let y;
3+
y = 42;
4+
x = y;

0 commit comments

Comments
 (0)