@@ -84,8 +84,17 @@ module PrinterExtensions =
8484 if i < nodes.Length - 1 then
8585 printSeparator printer
8686
87- member printer.PrintCommaSeparatedArray ( nodes : #Node array ) =
88- printer.PrintArray( nodes, ( fun p x -> x.Print( p)), ( fun p -> p.Print( " , " )))
87+ member printer.PrintCommaSeparatedArray ( nodes : ExportSpecifier array ) =
88+ printer.PrintArray( nodes, ( fun p x -> p.Print( x)), ( fun p -> p.Print( " , " )))
89+
90+ member printer.PrintCommaSeparatedArray ( nodes : #ImportSpecifier array ) =
91+ printer.PrintArray( nodes, ( fun p x -> p.Print( x)), ( fun p -> p.Print( " , " )))
92+
93+ member printer.PrintCommaSeparatedArray ( nodes : Pattern array ) =
94+ printer.PrintArray( nodes, ( fun p x -> p.Print( x)), ( fun p -> p.Print( " , " )))
95+
96+ member printer.PrintCommaSeparatedArray ( nodes : Expression array ) =
97+ printer.PrintArray( nodes, ( fun p x -> p.SequenceExpressionWithParens( x)), ( fun p -> p.Print( " , " )))
8998
9099 // TODO: (super) type parameters, implements
91100 member printer.PrintClass ( id : Identifier option , superClass : Expression option , body : ClassBody , loc ) =
@@ -127,12 +136,13 @@ module PrinterExtensions =
127136 | Some e -> e.Print( printer)
128137 | None ->
129138 if isArrow then
130- // TODO: Remove parens if we only have one argument (and no annotation)
139+ // Remove parens if we only have one argument? (and no annotation)
131140 printer.Print( " (" )
132141 printer.PrintCommaSeparatedArray( parameters)
133142 printer.Print( " ) => " )
134143 match body.Body with
135- | [|:? ReturnStatement as r |] -> r.Argument.Print( printer)
144+ | [|:? ReturnStatement as r |] ->
145+ printer.ComplexExpressionWithParens( r.Argument, objExpr= true )
136146 | _ -> printer.PrintBlock( body.Body, skipNewLineAtEnd= true )
137147 else
138148 printer.Print( " function " )
@@ -153,12 +163,23 @@ module PrinterExtensions =
153163 | _ -> printer.Print( expr)
154164
155165 /// Surround with parens anything that can potentially conflict with operator precedence
156- member printer.ComplexExpressionWithParens ( expr : Expression ) =
166+ member printer.ComplexExpressionWithParens ( expr : Expression , ? objExpr ) =
157167 match expr with
158- | :? PatternExpression
159- | :? Literal
168+ | :? Undefined
169+ | :? NullLiteral
170+ | :? StringLiteral
171+ | :? BooleanLiteral
172+ | :? NumericLiteral
173+ | :? Identifier
174+ | :? MemberExpression
160175 | :? CallExpression
176+ // | :? NewExpression // Safe?
177+ | :? ThisExpression
161178 | :? Super -> expr.Print( printer)
179+ | :? ObjectExpression ->
180+ match objExpr with
181+ | Some true -> printer.WithParens( expr)
182+ | _ -> expr.Print( printer)
162183 | _ -> printer.WithParens( expr)
163184
164185 member printer.PrintOperation ( left , operator , right , loc ) =
@@ -524,31 +545,33 @@ type TryStatement(block, ?handler, ?finalizer, ?loc) =
524545 printer.PrintOptional( " finally " , finalizer)
525546
526547// Declarations
527- type VariableDeclarator ( id , ? init , ? loc ) =
548+ type VariableDeclarator ( id , ? init ) =
528549 member _.Id : Pattern = id
529550 member _.Init : Expression option = init
530- interface Node with
531- member _.Print ( printer ) =
532- printer.AddLocation( loc)
533- id.Print( printer)
534- match init with
535- | None -> ()
536- | Some e ->
537- printer.Print( " = " )
538- printer.SequenceExpressionWithParens( e)
539551
540552type VariableDeclarationKind = Var | Let | Const
541553
542554type VariableDeclaration ( kind_ , declarations , ? loc ) =
543555 let kind = match kind_ with Var -> " var" | Let -> " let" | Const -> " const"
544556 new ( var , ? init , ? kind , ? loc ) =
545- VariableDeclaration( defaultArg kind Let, [| VariableDeclarator( var, ?init= init, ?loc = loc )|], ?loc= loc)
557+ VariableDeclaration( defaultArg kind Let, [| VariableDeclarator( var, ?init= init)|], ?loc= loc)
546558 member _.Declarations : VariableDeclarator array = declarations
547559 member _.Kind : string = kind
548560 interface Declaration with
549561 member _.Print ( printer ) =
550562 printer.Print( kind + " " , ?loc= loc)
551- printer.PrintCommaSeparatedArray( declarations)
563+ let canConflict = declarations.Length > 1
564+ for i = 0 to declarations.Length - 1 do
565+ let decl = declarations.[ i]
566+ printer.Print( decl.Id)
567+ match decl.Init with
568+ | None -> ()
569+ | Some e ->
570+ printer.Print( " = " )
571+ if canConflict then printer.ComplexExpressionWithParens( e)
572+ else printer.SequenceExpressionWithParens( e)
573+ if i < declarations.Length - 1 then
574+ printer.Print( " , " )
552575
553576// Loops
554577type WhileStatement ( test , body , ? loc ) =
0 commit comments