Skip to content

Commit

Permalink
JS: add newline after comments fos JS() methods
Browse files Browse the repository at this point in the history
  • Loading branch information
tdewolff committed Jan 23, 2024
1 parent 94b6e86 commit 6ce907f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
18 changes: 12 additions & 6 deletions js/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,9 +431,11 @@ func (n Comment) String() string {
// JS writes JavaScript to writer.
func (n Comment) JS(w io.Writer) {
if wi, ok := w.(Indenter); ok {
w = wi.w
wi.w.Write(n.Value)
} else {
w.Write(n.Value)
}
w.Write(n.Value)
w.Write([]byte("\n"))
}

// BlockStmt is a block statement.
Expand Down Expand Up @@ -557,9 +559,11 @@ func (n DoWhileStmt) JS(w io.Writer) {
}
n.Body.JS(w)
if _, ok := n.Body.(*VarDecl); ok {
w.Write([]byte(";"))
w.Write([]byte("; "))
} else if _, ok := n.Body.(*Comment); !ok {
w.Write([]byte(" "))
}
w.Write([]byte(" while ("))
w.Write([]byte("while ("))
n.Cond.JS(w)
w.Write([]byte(");"))
}
Expand All @@ -579,9 +583,11 @@ func (n WhileStmt) JS(w io.Writer) {
w.Write([]byte("while ("))
n.Cond.JS(w)
w.Write([]byte(")"))
if _, ok := n.Body.(*EmptyStmt); !ok {
w.Write([]byte(" "))
if _, ok := n.Body.(*EmptyStmt); ok {
w.Write([]byte(";"))
return
}
w.Write([]byte(" "))
n.Body.JS(w)
if _, ok := n.Body.(*VarDecl); ok {
w.Write([]byte(";"))
Expand Down
3 changes: 2 additions & 1 deletion js/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,10 @@ func TestJS(t *testing.T) {
{"{}\n;", "{} ;"},
{"- - --3", "- - --3;"},
{"([,,])=>P", "([,,]) => { return P; };"},
{"(t)=>{//!\n}", "(t) => { //! };"},
{"(t)=>{//!\n}", "(t) => { //! };"}, // space after //! is newline
{"import();", "import();"},
{"0\n.k", "(0).k;"},
{"do//!\nwhile(1)", "do //! while (1);"}, // space after //! is newline
}

re := regexp.MustCompile("\n *")
Expand Down

0 comments on commit 6ce907f

Please sign in to comment.