@@ -3,16 +3,16 @@ import ts from 'typescript';
33
44import type { MaybeTsDsl } from '../base' ;
55import { TsDsl } from '../base' ;
6+ import type { MethodTsDsl } from '../decl/method' ;
67import { AsMixin } from '../mixins/as' ;
78import { ExprMixin } from '../mixins/expr' ;
89import { HintMixin } from '../mixins/hint' ;
910import { LayoutMixin } from '../mixins/layout' ;
11+ import { f } from '../utils/factories' ;
1012import { ObjectPropTsDsl } from './prop' ;
1113
1214type Expr = NodeName | MaybeTsDsl < ts . Expression > ;
1315type Stmt = NodeName | MaybeTsDsl < ts . Statement > ;
14- type ExprFn = Expr | ( ( p : ObjectPropTsDsl ) => void ) ;
15- type StmtFn = Stmt | ( ( p : ObjectPropTsDsl ) => void ) ;
1616
1717const 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