@@ -839,6 +839,7 @@ func (p *printer) printFnArgs(args []js_ast.Arg, opts fnArgsOpts) {
839
839
p .print ("," )
840
840
p .printSpace ()
841
841
}
842
+ p .printDecorators (arg .Decorators , printDecoratorsAllOnOneLine )
842
843
if opts .hasRestArg && i + 1 == len (args ) {
843
844
p .print ("..." )
844
845
}
@@ -863,6 +864,67 @@ func (p *printer) printFn(fn js_ast.Fn) {
863
864
p .printBlock (fn .Body .Loc , fn .Body .Block )
864
865
}
865
866
867
+ type printDecorators uint8
868
+
869
+ const (
870
+ printDecoratorsOnSeparateLines printDecorators = iota
871
+ printDecoratorsAllOnOneLine
872
+ )
873
+
874
+ func (p * printer ) printDecorators (decorators []js_ast.Expr , how printDecorators ) {
875
+ for _ , decorator := range decorators {
876
+ wrap := false
877
+ expr := decorator
878
+
879
+ outer:
880
+ for {
881
+ switch e := expr .Data .(type ) {
882
+ case * js_ast.EIdentifier , * js_ast.ECall :
883
+ // "@foo"
884
+ break outer
885
+
886
+ case * js_ast.EDot :
887
+ // "@foo.bar"
888
+ expr = e .Target
889
+
890
+ case * js_ast.EIndex :
891
+ if _ , ok := e .Index .Data .(* js_ast.EPrivateIdentifier ); ! ok {
892
+ // "@(foo[bar])"
893
+ wrap = true
894
+ break outer
895
+ }
896
+
897
+ // "@foo.#bar"
898
+ expr = e .Target
899
+
900
+ default :
901
+ // "@(foo + bar)"
902
+ // "@(() => {})"
903
+ wrap = true
904
+ break outer
905
+ }
906
+ }
907
+
908
+ p .print ("@" )
909
+ if wrap {
910
+ p .print ("(" )
911
+ }
912
+ p .printExpr (decorator , js_ast .LLowest , 0 )
913
+ if wrap {
914
+ p .print (")" )
915
+ }
916
+
917
+ switch how {
918
+ case printDecoratorsOnSeparateLines :
919
+ p .printNewline ()
920
+ p .printIndent ()
921
+
922
+ case printDecoratorsAllOnOneLine :
923
+ p .printSpace ()
924
+ }
925
+ }
926
+ }
927
+
866
928
func (p * printer ) printClass (class js_ast.Class ) {
867
929
if class .ExtendsOrNil .Data != nil {
868
930
p .print (" extends" )
@@ -879,6 +941,7 @@ func (p *printer) printClass(class js_ast.Class) {
879
941
for _ , item := range class .Properties {
880
942
p .printSemicolonIfNeeded ()
881
943
p .printIndent ()
944
+ p .printDecorators (item .Decorators , printDecoratorsOnSeparateLines )
882
945
883
946
if item .Kind == js_ast .PropertyClassStaticBlock {
884
947
p .addSourceMapping (item .Loc )
@@ -2507,6 +2570,7 @@ func (p *printer) printExpr(expr js_ast.Expr, level js_ast.L, flags printExprFla
2507
2570
if wrap {
2508
2571
p .print ("(" )
2509
2572
}
2573
+ p .printDecorators (e .Class .Decorators , printDecoratorsAllOnOneLine )
2510
2574
p .printSpaceBeforeIdentifier ()
2511
2575
p .addSourceMapping (expr .Loc )
2512
2576
p .print ("class" )
@@ -3737,6 +3801,7 @@ func (p *printer) printStmt(stmt js_ast.Stmt, flags printStmtFlags) {
3737
3801
p .printNewline ()
3738
3802
3739
3803
case * js_ast.SClass :
3804
+ p .printDecorators (s .Class .Decorators , printDecoratorsOnSeparateLines )
3740
3805
p .addSourceMapping (stmt .Loc )
3741
3806
p .printIndent ()
3742
3807
p .printSpaceBeforeIdentifier ()
@@ -3759,6 +3824,9 @@ func (p *printer) printStmt(stmt js_ast.Stmt, flags printStmtFlags) {
3759
3824
case * js_ast.SExportDefault :
3760
3825
p .addSourceMapping (stmt .Loc )
3761
3826
p .printIndent ()
3827
+ if s2 , ok := s .Value .Data .(* js_ast.SClass ); ok {
3828
+ p .printDecorators (s2 .Class .Decorators , printDecoratorsOnSeparateLines )
3829
+ }
3762
3830
p .printSpaceBeforeIdentifier ()
3763
3831
p .print ("export default" )
3764
3832
p .printSpace ()
0 commit comments