Skip to content

Commit

Permalink
JS: keep all bang-comments (including in expressions) and move to top…
Browse files Browse the repository at this point in the history
… of statement list
  • Loading branch information
tdewolff committed Jan 26, 2024
1 parent 6ce907f commit f1831b3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
1 change: 0 additions & 1 deletion js/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,6 @@ func (n Comment) JS(w io.Writer) {
} else {
w.Write(n.Value)
}
w.Write([]byte("\n"))
}

// BlockStmt is a block statement.
Expand Down
4 changes: 2 additions & 2 deletions js/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ func TestJS(t *testing.T) {
{"{}\n;", "{} ;"},
{"- - --3", "- - --3;"},
{"([,,])=>P", "([,,]) => { return P; };"},
{"(t)=>{//!\n}", "(t) => { //! };"}, // space after //! is newline
{"(t)=>{//!\n}", "(t) => { //! };"}, // space after //! is newline
{"import();", "import();"},
{"0\n.k", "(0).k;"},
{"do//!\nwhile(1)", "do //! while (1);"}, // space after //! is newline
{"do//!\n; while(1)", "//! do; while (1);"}, // space after //! is newline
}

re := regexp.MustCompile("\n *")
Expand Down
30 changes: 24 additions & 6 deletions js/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Parser struct {
in, await, yield, deflt, retrn bool
assumeArrowFunc bool
allowDirectivePrologue bool
comments []IStmt

stmtLevel int
exprLevel int
Expand Down Expand Up @@ -91,9 +92,22 @@ func Parse(r *parse.Input, o Options) (*AST, error) {
func (p *Parser) next() {
p.prevLT = false
p.tt, p.data = p.l.Next()
for p.tt == WhitespaceToken || p.tt == LineTerminatorToken || (p.tt == CommentToken || p.tt == CommentLineTerminatorToken) && (p.exprLevel != 0 || len(p.data) < 3 || p.data[2] != '!') {
if p.tt == LineTerminatorToken || p.tt == CommentLineTerminatorToken {
Loop:
for {
switch p.tt {
case WhitespaceToken:
// no-op
case LineTerminatorToken:
p.prevLT = true
case CommentToken, CommentLineTerminatorToken:
if 2 < len(p.data) && p.data[2] == '!' {
p.comments = append(p.comments, &Comment{p.data})
}
if p.tt == CommentLineTerminatorToken {
p.prevLT = true
}
default:
break Loop
}
p.tt, p.data = p.l.Next()
}
Expand Down Expand Up @@ -180,6 +194,10 @@ func (p *Parser) parseModule() (module BlockStmt) {
for {
switch p.tt {
case ErrorToken:
if 0 < len(p.comments) {
module.List = append(p.comments, module.List...)
p.comments = p.comments[:0]
}
return
case ImportToken:
p.next()
Expand Down Expand Up @@ -567,10 +585,6 @@ func (p *Parser) parseStmt(allowDeclaration bool) (stmt IStmt) {
case SemicolonToken:
stmt = &EmptyStmt{}
p.next()
case CommentToken, CommentLineTerminatorToken:
// bang comment
stmt = &Comment{p.data}
p.next()
case ErrorToken:
stmt = &EmptyStmt{}
return
Expand Down Expand Up @@ -635,6 +649,10 @@ func (p *Parser) parseStmtList(in string) (list []IStmt) {
}
list = append(list, p.parseStmt(true))
}
if 0 < len(p.comments) {
list = append(p.comments, list...)
p.comments = p.comments[:0]
}
return
}

Expand Down
3 changes: 2 additions & 1 deletion js/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ func TestParse(t *testing.T) {
{"#!/usr/bin/env node", "Stmt(#!/usr/bin/env node)"},
{"/* comment */", ""},
{"/*! comment */", "Stmt(/*! comment */)"},
{"5 + /*! comment */ 6", "Stmt(5+6)"},
{"5 + /*! comment */ 6", "Stmt(/*! comment */) Stmt(5+6)"},
{"var a = /*! comment */ 6", "Stmt(/*! comment */) Decl(var Binding(a = 6))"},
{"{}", "Stmt({ })"},
{`"use strict"`, `Stmt("use strict")`},
{"var a = b;", "Decl(var Binding(a = b))"},
Expand Down

0 comments on commit f1831b3

Please sign in to comment.