@@ -4,51 +4,43 @@ import { py } from '../../ts-python';
44import type { MaybePyDsl } from '../base' ;
55import type { BaseCtor , MixinCtor } from './types' ;
66
7+ type DecoratorInput = {
8+ args : ReadonlyArray < MaybePyDsl < py . Expression > > ;
9+ name : NodeName | MaybePyDsl < py . Expression > ;
10+ } ;
11+
712export interface DecoratorMethods extends Node {
813 $decorators ( ) : ReadonlyArray < py . Expression > ;
9- decorator (
10- name : NodeName | MaybePyDsl < py . Expression > ,
11- ...args : ReadonlyArray < MaybePyDsl < py . Expression > >
12- ) : this;
14+ decorator ( name : DecoratorInput [ 'name' ] , ...args : DecoratorInput [ 'args' ] ) : this;
1315}
1416
1517export function DecoratorMixin < T extends py . Node , TBase extends BaseCtor < T > > ( Base : TBase ) {
1618 abstract class Decorator extends Base {
17- protected decorators : Array < py . Expression > = [ ] ;
19+ protected _decorators : Array < DecoratorInput > = [ ] ;
1820
1921 override analyze ( ctx : AnalysisContext ) : void {
2022 super . analyze ( ctx ) ;
21- for ( const decorator of this . decorators ) {
22- ctx . analyze ( decorator ) ;
23+ for ( const decorator of this . _decorators ) {
24+ ctx . analyze ( decorator . name ) ;
25+ for ( const arg of decorator . args ) {
26+ ctx . analyze ( arg ) ;
27+ }
2328 }
2429 }
2530
26- protected decorator (
27- name : NodeName | MaybePyDsl < py . Expression > ,
28- ...args : ReadonlyArray < MaybePyDsl < py . Expression > >
29- ) : this {
30- const nameNode =
31- typeof name === 'string' || isPyNode ( name )
32- ? name
33- : py . factory . createIdentifier ( String ( name ) ) ;
34- const decoratorExpr = args . length
35- ? py . factory . createCallExpression (
36- nameNode as py . Expression ,
37- args . map ( ( a ) => this . $node ( a ) ) ,
38- )
39- : ( nameNode as py . Expression ) ;
40- this . decorators . push ( decoratorExpr ) ;
31+ protected decorator ( name : DecoratorInput [ 'name' ] , ...args : DecoratorInput [ 'args' ] ) : this {
32+ this . _decorators . push ( { args, name } ) ;
4133 return this ;
4234 }
4335
4436 protected $decorators ( ) : ReadonlyArray < py . Expression > {
45- return this . decorators ;
37+ return this . _decorators . map ( ( decorator ) =>
38+ decorator . args . length > 0
39+ ? py . factory . createCallExpression ( this . $node ( decorator . name ) , this . $node ( decorator . args ) )
40+ : this . $node ( decorator . name ) ,
41+ ) ;
4642 }
4743 }
4844
4945 return Decorator as unknown as MixinCtor < TBase , DecoratorMethods > ;
5046}
51-
52- function isPyNode ( value : unknown ) : value is { toAst ( ) : unknown } {
53- return typeof value === 'object' && value !== null && 'toAst' in value ;
54- }
0 commit comments