Skip to content

Commit

Permalink
JS: improve output of ast.JS, see #108
Browse files Browse the repository at this point in the history
  • Loading branch information
tdewolff committed Oct 26, 2023
1 parent b6af36b commit 5d3f1f2
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 139 deletions.
93 changes: 50 additions & 43 deletions js/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,14 @@ func (ast AST) String() string {

// JS writes JavaScript to writer.
func (ast AST) JS(w io.Writer) {
for _, item := range ast.List {
for i, item := range ast.List {
if i != 0 {
w.Write([]byte("\n"))
}
item.JS(w)
w.Write([]byte("; "))
if _, ok := item.(*VarDecl); ok {
w.Write([]byte(";"))
}
}
}

Expand Down Expand Up @@ -414,6 +419,7 @@ func (n Comment) String() string {
// JS writes JavaScript to writer.
func (n Comment) JS(w io.Writer) {
w.Write(n.Value)
w.Write([]byte("\n"))
}

// BlockStmt is a block statement.
Expand All @@ -432,15 +438,21 @@ func (n BlockStmt) String() string {

// JS writes JavaScript to writer.
func (n BlockStmt) JS(w io.Writer) {
if n.Scope.Parent == nil {
panic("X")
if len(n.List) == 0 {
w.Write([]byte("{}"))
return
}
w.Write([]byte("{ "))

w.Write([]byte("{"))
wi := NewIndenter(w, 4)
for _, item := range n.List {
item.JS(w)
w.Write([]byte("; "))
wi.Write([]byte("\n"))
item.JS(wi)
if _, ok := item.(*VarDecl); ok {
w.Write([]byte(";"))
}
}
w.Write([]byte("}"))
w.Write([]byte("\n}"))
}

// EmptyStmt is an empty statement.
Expand All @@ -451,7 +463,9 @@ func (n EmptyStmt) String() string {
}

// JS writes JavaScript to writer.
func (n EmptyStmt) JS(w io.Writer) {}
func (n EmptyStmt) JS(w io.Writer) {
w.Write([]byte(";"))
}

// ExprStmt is an expression statement.
type ExprStmt struct {
Expand All @@ -469,6 +483,7 @@ func (n ExprStmt) String() string {
// JS writes JavaScript to writer.
func (n ExprStmt) JS(w io.Writer) {
n.Value.JS(w)
w.Write([]byte(";"))
}

// IfStmt is an if statement.
Expand All @@ -490,25 +505,14 @@ func (n IfStmt) String() string {
func (n IfStmt) JS(w io.Writer) {
w.Write([]byte("if ("))
n.Cond.JS(w)
w.Write([]byte(") "))
switch n.Body.(type) {
case *BlockStmt:
n.Body.JS(w)
default:
w.Write([]byte("{ "))
n.Body.JS(w)
w.Write([]byte(" }"))
w.Write([]byte(")"))
if _, ok := n.Body.(*EmptyStmt); !ok {
w.Write([]byte(" "))
}
n.Body.JS(w)
if n.Else != nil {
switch n.Else.(type) {
case *BlockStmt:
w.Write([]byte(" else "))
n.Else.JS(w)
default:
w.Write([]byte(" else { "))
n.Else.JS(w)
w.Write([]byte(" }"))
}
w.Write([]byte(" else "))
n.Else.JS(w)
}
}

Expand All @@ -524,18 +528,14 @@ func (n DoWhileStmt) String() string {

// JS writes JavaScript to writer.
func (n DoWhileStmt) JS(w io.Writer) {
w.Write([]byte("do "))
switch n.Body.(type) {
case *BlockStmt:
n.Body.JS(w)
default:
w.Write([]byte("{ "))
n.Body.JS(w)
w.Write([]byte(" }"))
w.Write([]byte("do"))
if _, ok := n.Body.(*EmptyStmt); !ok {
w.Write([]byte(" "))
}
n.Body.JS(w)
w.Write([]byte(" while ("))
n.Cond.JS(w)
w.Write([]byte(")"))
w.Write([]byte(");"))
}

// WhileStmt is a while iteration statement.
Expand All @@ -552,10 +552,11 @@ func (n WhileStmt) String() string {
func (n WhileStmt) JS(w io.Writer) {
w.Write([]byte("while ("))
n.Cond.JS(w)
w.Write([]byte(") "))
if n.Body != nil {
n.Body.JS(w)
w.Write([]byte(")"))
if _, ok := n.Body.(*EmptyStmt); !ok {
w.Write([]byte(" "))
}
n.Body.JS(w)
}

// ForStmt is a regular for iteration statement.
Expand Down Expand Up @@ -684,7 +685,6 @@ func (n CaseClause) JS(w io.Writer) {
for _, item := range n.List {
w.Write([]byte(" "))
item.JS(w)
w.Write([]byte(";"))
}
}

Expand Down Expand Up @@ -735,6 +735,7 @@ func (n BranchStmt) JS(w io.Writer) {
w.Write([]byte(" "))
w.Write(n.Label)
}
w.Write([]byte(";"))
}

// ReturnStmt is a return statement.
Expand All @@ -757,6 +758,7 @@ func (n ReturnStmt) JS(w io.Writer) {
w.Write([]byte(" "))
n.Value.JS(w)
}
w.Write([]byte(";"))
}

// WithStmt is a with statement.
Expand Down Expand Up @@ -807,6 +809,7 @@ func (n ThrowStmt) String() string {
func (n ThrowStmt) JS(w io.Writer) {
w.Write([]byte("throw "))
n.Value.JS(w)
w.Write([]byte(";"))
}

// TryStmt is a try statement.
Expand Down Expand Up @@ -861,7 +864,7 @@ func (n DebuggerStmt) String() string {

// JS writes JavaScript to writer.
func (n DebuggerStmt) JS(w io.Writer) {
w.Write([]byte("debugger"))
w.Write([]byte("debugger;"))
}

// Alias is a name space import or import/export specifier for import/export statements.
Expand Down Expand Up @@ -953,6 +956,7 @@ func (n ImportStmt) JS(w io.Writer) {
}
w.Write([]byte(" "))
w.Write(n.Module)
w.Write([]byte(";"))
}

// ExportStmt is an export statement.
Expand Down Expand Up @@ -999,6 +1003,7 @@ func (n ExportStmt) JS(w io.Writer) {
}
w.Write([]byte(" "))
n.Decl.JS(w)
w.Write([]byte(";"))
return
} else if len(n.List) == 1 && (len(n.List[0].Name) == 1 && n.List[0].Name[0] == '*' || n.List[0].Name == nil && len(n.List[0].Binding) == 1 && n.List[0].Binding[0] == '*') {
w.Write([]byte(" "))
Expand Down Expand Up @@ -1034,6 +1039,7 @@ func (n DirectivePrologueStmt) String() string {
// JS writes JavaScript to writer.
func (n DirectivePrologueStmt) JS(w io.Writer) {
w.Write(n.Value)
w.Write([]byte(";"))
}

func (n Comment) stmtNode() {}
Expand Down Expand Up @@ -1502,6 +1508,7 @@ func (n ClassElement) JS(w io.Writer) {
return
}
n.Field.JS(w)
w.Write([]byte(";"))
}

// ClassDecl is a class declaration.
Expand Down Expand Up @@ -1536,12 +1543,12 @@ func (n ClassDecl) JS(w io.Writer) {
w.Write([]byte(" extends "))
n.Extends.JS(w)
}
w.Write([]byte(" { "))
w.Write([]byte(" {"))
for _, item := range n.List {
w.Write([]byte(" "))
item.JS(w)
w.Write([]byte("; "))
}
w.Write([]byte("}"))
w.Write([]byte(" }"))
}

func (n VarDecl) stmtNode() {}
Expand Down
Loading

0 comments on commit 5d3f1f2

Please sign in to comment.