Skip to content

Commit 0d2b8ec

Browse files
authored
Merge pull request #3666 from hey-api/chore/ts-dsl-bump
chore: update ts-dsl
2 parents 19075fb + 581c31c commit 0d2b8ec

File tree

20 files changed

+250
-82
lines changed

20 files changed

+250
-82
lines changed

packages/openapi-ts/src/ts-dsl/decl/func.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { DocMixin } from '../mixins/doc';
1010
import {
1111
AbstractMixin,
1212
AsyncMixin,
13+
ExportMixin,
1314
PrivateMixin,
1415
ProtectedMixin,
1516
PublicMixin,
@@ -29,11 +30,13 @@ const Mixed = AbstractMixin(
2930
DecoratorMixin(
3031
DoMixin(
3132
DocMixin(
32-
ParamMixin(
33-
PrivateMixin(
34-
ProtectedMixin(
35-
PublicMixin(
36-
StaticMixin(TypeParamsMixin(TypeReturnsMixin(TsDsl<ts.ArrowFunction>))),
33+
ExportMixin(
34+
ParamMixin(
35+
PrivateMixin(
36+
ProtectedMixin(
37+
PublicMixin(
38+
StaticMixin(TypeParamsMixin(TypeReturnsMixin(TsDsl<ts.ArrowFunction>))),
39+
),
3740
),
3841
),
3942
),

packages/openapi-ts/src/ts-dsl/decl/method.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@ import { TypeParamsMixin } from '../mixins/type-params';
1919
import { TypeReturnsMixin } from '../mixins/type-returns';
2020
import { BlockTsDsl } from '../stmt/block';
2121
import { TokenTsDsl } from '../token';
22+
import { f } from '../utils/factories';
2223
import { safeAccessorName } from '../utils/name';
2324

25+
export type MethodCtor = (name: NodeName, fn?: (m: MethodTsDsl) => void) => MethodTsDsl;
26+
2427
const Mixed = AbstractMixin(
2528
AsyncMixin(
2629
DecoratorMixin(
@@ -78,3 +81,5 @@ export class MethodTsDsl extends Mixed {
7881
return this.$docs(node);
7982
}
8083
}
84+
85+
f.method.set((...args) => new MethodTsDsl(...args));

packages/openapi-ts/src/ts-dsl/decl/param.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ export class ParamTsDsl extends Mixed {
4545
}
4646

4747
/** Sets the parameter type. */
48-
type(type: string | TypeTsDsl): this {
49-
this._type = type instanceof TypeTsDsl ? type : new TypeExprTsDsl(type);
48+
type(node: NodeName | TypeTsDsl): this {
49+
this._type = node instanceof TypeTsDsl ? node : new TypeExprTsDsl(node);
5050
return this;
5151
}
5252

packages/openapi-ts/src/ts-dsl/expr/array.ts

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,60 +6,49 @@ import { TsDsl } from '../base';
66
import { AsMixin } from '../mixins/as';
77
import { ExprMixin } from '../mixins/expr';
88
import { LayoutMixin } from '../mixins/layout';
9+
import { SpreadMixin } from '../mixins/spread';
910
import { LiteralTsDsl } from './literal';
1011

11-
const Mixed = AsMixin(ExprMixin(LayoutMixin(TsDsl<ts.ArrayLiteralExpression>)));
12+
export type ArrayExpr = string | number | boolean | MaybeTsDsl<ts.Expression>;
13+
14+
const Mixed = AsMixin(ExprMixin(LayoutMixin(SpreadMixin(TsDsl<ts.ArrayLiteralExpression>))));
1215

1316
export class ArrayTsDsl extends Mixed {
1417
readonly '~dsl' = 'ArrayTsDsl';
1518

16-
protected _elements: Array<
17-
| { expr: MaybeTsDsl<ts.Expression>; kind: 'element' }
18-
| { expr: MaybeTsDsl<ts.Expression>; kind: 'spread' }
19-
> = [];
19+
protected _elements: Array<MaybeTsDsl<ts.Expression>> = [];
2020

21-
constructor(...exprs: Array<string | number | boolean | MaybeTsDsl<ts.Expression>>) {
21+
constructor(...exprs: Array<ArrayExpr>) {
2222
super();
2323
this.elements(...exprs);
2424
}
2525

2626
override analyze(ctx: AnalysisContext): void {
2727
super.analyze(ctx);
2828
for (const item of this._elements) {
29-
ctx.analyze(item.expr);
29+
ctx.analyze(item);
3030
}
3131
}
3232

3333
/** Adds a single array element. */
34-
element(expr: string | number | boolean | MaybeTsDsl<ts.Expression>): this {
34+
element(expr: ArrayExpr): this {
3535
const node =
3636
typeof expr === 'string' || typeof expr === 'number' || typeof expr === 'boolean'
3737
? new LiteralTsDsl(expr)
3838
: expr;
39-
this._elements.push({ expr: node, kind: 'element' });
39+
this._elements.push(node);
4040
return this;
4141
}
4242

4343
/** Adds multiple array elements. */
44-
elements(...exprs: ReadonlyArray<string | number | boolean | MaybeTsDsl<ts.Expression>>): this {
44+
elements(...exprs: ReadonlyArray<ArrayExpr>): this {
4545
for (const expr of exprs) this.element(expr);
4646
return this;
4747
}
4848

49-
/** Adds a spread element (`...expr`). */
50-
spread(expr: MaybeTsDsl<ts.Expression>): this {
51-
this._elements.push({ expr, kind: 'spread' });
52-
return this;
53-
}
54-
5549
override toAst() {
56-
const elements = this._elements.map((item) => {
57-
const node = this.$node(item.expr);
58-
return item.kind === 'spread' ? ts.factory.createSpreadElement(node) : node;
59-
});
60-
6150
return ts.factory.createArrayLiteralExpression(
62-
elements,
51+
this._elements.map((item) => this.$node(item)),
6352
this.$multiline(this._elements.length),
6453
);
6554
}

packages/openapi-ts/src/ts-dsl/expr/attr.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { AsMixin } from '../mixins/as';
88
import { ExprMixin } from '../mixins/expr';
99
import { OperatorMixin } from '../mixins/operator';
1010
import { OptionalMixin } from '../mixins/optional';
11+
import { SpreadMixin } from '../mixins/spread';
1112
import { TokenTsDsl } from '../token';
1213
import { f } from '../utils/factories';
1314
import { regexp } from '../utils/regexp';
@@ -18,7 +19,9 @@ export type AttrCtor = (left: AttrLeft, right: NodeName) => AttrTsDsl;
1819

1920
const Mixed = AsMixin(
2021
ExprMixin(
21-
OperatorMixin(OptionalMixin(TsDsl<ts.PropertyAccessExpression | ts.ElementAccessExpression>)),
22+
OperatorMixin(
23+
OptionalMixin(SpreadMixin(TsDsl<ts.PropertyAccessExpression | ts.ElementAccessExpression>)),
24+
),
2225
),
2326
);
2427

packages/openapi-ts/src/ts-dsl/expr/call.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ import { TsDsl } from '../base';
77
import { ArgsMixin } from '../mixins/args';
88
import { AsMixin } from '../mixins/as';
99
import { ExprMixin } from '../mixins/expr';
10+
import { SpreadMixin } from '../mixins/spread';
1011
import { TypeArgsMixin } from '../mixins/type-args';
1112
import { f } from '../utils/factories';
1213

1314
export type CallArgs = ReadonlyArray<CallCallee | undefined>;
1415
export type CallCallee = NodeName | MaybeTsDsl<ts.Expression>;
1516
export type CallCtor = (callee: CallCallee, ...args: CallArgs) => CallTsDsl;
1617

17-
const Mixed = ArgsMixin(AsMixin(ExprMixin(TypeArgsMixin(TsDsl<ts.CallExpression>))));
18+
const Mixed = ArgsMixin(AsMixin(ExprMixin(SpreadMixin(TypeArgsMixin(TsDsl<ts.CallExpression>)))));
1819

1920
export class CallTsDsl extends Mixed {
2021
readonly '~dsl' = 'CallTsDsl';

packages/openapi-ts/src/ts-dsl/expr/expr.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ import { TsDsl } from '../base';
77
import { AsMixin } from '../mixins/as';
88
import { ExprMixin } from '../mixins/expr';
99
import { OperatorMixin } from '../mixins/operator';
10+
import { SpreadMixin } from '../mixins/spread';
1011
import { TypeExprMixin } from '../mixins/type-expr';
1112

1213
type Id = NodeName | MaybeTsDsl<ts.Expression>;
1314

14-
const Mixed = AsMixin(ExprMixin(OperatorMixin(TypeExprMixin(TsDsl<ts.Expression>))));
15+
const Mixed = AsMixin(ExprMixin(OperatorMixin(SpreadMixin(TypeExprMixin(TsDsl<ts.Expression>)))));
1516

1617
export class ExprTsDsl extends Mixed {
1718
readonly '~dsl' = 'ExprTsDsl';

packages/openapi-ts/src/ts-dsl/expr/new.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ import { TsDsl } from '../base';
77
import { ArgsMixin } from '../mixins/args';
88
import { AsMixin } from '../mixins/as';
99
import { ExprMixin } from '../mixins/expr';
10+
import { SpreadMixin } from '../mixins/spread';
1011
import { TypeArgsMixin } from '../mixins/type-args';
1112
import { f } from '../utils/factories';
1213

1314
export type NewArgs = ReadonlyArray<NewExpr | undefined>;
1415
export type NewExpr = NodeName | MaybeTsDsl<ts.Expression>;
1516
export type NewCtor = (expr: NewExpr, ...args: NewArgs) => NewTsDsl;
1617

17-
const Mixed = ArgsMixin(AsMixin(ExprMixin(TypeArgsMixin(TsDsl<ts.NewExpression>))));
18+
const Mixed = ArgsMixin(AsMixin(ExprMixin(SpreadMixin(TypeArgsMixin(TsDsl<ts.NewExpression>)))));
1819

1920
export class NewTsDsl extends Mixed {
2021
readonly '~dsl' = 'NewTsDsl';

packages/openapi-ts/src/ts-dsl/expr/object.ts

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ import ts from 'typescript';
33

44
import type { MaybeTsDsl } from '../base';
55
import { TsDsl } from '../base';
6+
import type { MethodTsDsl } from '../decl/method';
67
import { AsMixin } from '../mixins/as';
78
import { ExprMixin } from '../mixins/expr';
89
import { HintMixin } from '../mixins/hint';
910
import { LayoutMixin } from '../mixins/layout';
11+
import { f } from '../utils/factories';
1012
import { ObjectPropTsDsl } from './prop';
1113

1214
type Expr = NodeName | MaybeTsDsl<ts.Expression>;
1315
type Stmt = NodeName | MaybeTsDsl<ts.Statement>;
14-
type ExprFn = Expr | ((p: ObjectPropTsDsl) => void);
15-
type StmtFn = Stmt | ((p: ObjectPropTsDsl) => void);
1616

1717
const Mixed = AsMixin(ExprMixin(HintMixin(LayoutMixin(TsDsl<ts.ObjectLiteralExpression>))));
1818

@@ -22,9 +22,13 @@ export class ObjectTsDsl extends Mixed {
2222
protected _props = new Map<string, ObjectPropTsDsl>();
2323
protected _spreadCounter = 0;
2424

25-
constructor(...props: Array<ObjectPropTsDsl>) {
25+
constructor(...props: Array<ObjectPropTsDsl> | [(o: ObjectTsDsl) => void]) {
2626
super();
27-
this.props(...props);
27+
if (props.length === 1 && typeof props[0] === 'function') {
28+
props[0](this);
29+
} else {
30+
this.props(...(props as Array<ObjectPropTsDsl>));
31+
}
2832
}
2933

3034
override analyze(ctx: AnalysisContext): void {
@@ -43,7 +47,7 @@ export class ObjectTsDsl extends Mixed {
4347
}
4448

4549
/** Adds a computed property (e.g., `{ [expr]: value }`), or removes if null. */
46-
computed(name: string, expr: ExprFn | null): this {
50+
computed(name: string, expr: Expr | null): this {
4751
if (expr === null) {
4852
this._props.delete(`computed:${name}`);
4953
} else {
@@ -56,7 +60,7 @@ export class ObjectTsDsl extends Mixed {
5660
}
5761

5862
/** Adds a getter property (e.g., `{ get foo() { ... } }`), or removes if null. */
59-
getter(name: string, stmt: StmtFn | null): this {
63+
getter(name: string, stmt: Stmt | null): this {
6064
if (stmt === null) {
6165
this._props.delete(`getter:${name}`);
6266
} else {
@@ -75,8 +79,21 @@ export class ObjectTsDsl extends Mixed {
7579
return this._props.size === 0;
7680
}
7781

82+
/** Adds a method property (e.g., `{ foo() { ... } }`), or removes if null. */
83+
method(name: string, fn: ((m: MethodTsDsl) => void) | null): this {
84+
if (fn === null) {
85+
this._props.delete(`method:${name}`);
86+
} else {
87+
this._props.set(
88+
`method:${name}`,
89+
new ObjectPropTsDsl({ kind: 'method', name }).value(f.method(name, fn)),
90+
);
91+
}
92+
return this;
93+
}
94+
7895
/** Adds a property assignment, or removes if null. */
79-
prop(name: string, expr: ExprFn | null): this {
96+
prop(name: string, expr: Expr | null): this {
8097
if (expr === null) {
8198
this._props.delete(`prop:${name}`);
8299
} else {
@@ -94,7 +111,7 @@ export class ObjectTsDsl extends Mixed {
94111
}
95112

96113
/** Adds a setter property (e.g., `{ set foo(v) { ... } }`), or removes if null. */
97-
setter(name: string, stmt: StmtFn | null): this {
114+
setter(name: string, stmt: Stmt | null): this {
98115
if (stmt === null) {
99116
this._props.delete(`setter:${name}`);
100117
} else {
@@ -104,7 +121,7 @@ export class ObjectTsDsl extends Mixed {
104121
}
105122

106123
/** Adds a spread property (e.g., `{ ...options }`). */
107-
spread(expr: ExprFn): this {
124+
spread(expr: Expr): this {
108125
const key = `spread:${this._spreadCounter++}`;
109126
this._props.set(key, new ObjectPropTsDsl({ kind: 'spread' }).value(expr));
110127
return this;

0 commit comments

Comments
 (0)