Skip to content

Commit

Permalink
JS: fix allowDirectivePrologue propagation, reset exprLevel for conte…
Browse files Browse the repository at this point in the history
…nts of methods/functions to make bang-comments work
  • Loading branch information
tdewolff committed Nov 26, 2023
1 parent 3910eb8 commit 8e56e59
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
1 change: 0 additions & 1 deletion js/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,6 @@ func (n Comment) JS(w io.Writer) {
w = wi.w
}
w.Write(n.Value)
w.Write([]byte("\n"))
}

// BlockStmt is a block statement.
Expand Down
1 change: 1 addition & 0 deletions js/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func TestJS(t *testing.T) {
{"{}\n;", "{} ;"},
{"- - --3", "- - --3;"},
{"([,,])=>P", "([,,]) => { return P; };"},
{"(t)=>{//!\n}", "(t) => { //! };"},
}

re := regexp.MustCompile("\n *")
Expand Down
18 changes: 14 additions & 4 deletions js/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -946,8 +946,11 @@ func (p *Parser) parseFunc(async, expr bool) (funcDecl *FuncDecl) {
funcDecl.Name, _ = p.scope.Declare(ExprDecl, name) // cannot fail
}
funcDecl.Params = p.parseFuncParams("function declaration")
p.allowDirectivePrologue = true

prevAllowDirectivePrologue, prevExprLevel := p.allowDirectivePrologue, p.exprLevel
p.allowDirectivePrologue, p.exprLevel = true, 0
funcDecl.Body.List = p.parseStmtList("function declaration")
p.allowDirectivePrologue, p.exprLevel = prevAllowDirectivePrologue, prevExprLevel

p.await, p.yield, p.retrn = prevAwait, prevYield, prevRetrn
p.exitScope(parent)
Expand Down Expand Up @@ -1091,8 +1094,11 @@ func (p *Parser) parseClassElement() ClassElement {
p.await, p.yield, p.retrn = method.Async, method.Generator, true

method.Params = p.parseFuncParams("method definition")
p.allowDirectivePrologue = true
method.Body.List = p.parseStmtList("method definition")

prevAllowDirectivePrologue, prevExprLevel := p.allowDirectivePrologue, p.exprLevel
p.allowDirectivePrologue, p.exprLevel = true, 0
method.Body.List = p.parseStmtList("method function")
p.allowDirectivePrologue, p.exprLevel = prevAllowDirectivePrologue, prevExprLevel

p.await, p.yield, p.retrn = prevAwait, prevYield, prevRetrn
p.exitScope(parent)
Expand Down Expand Up @@ -1525,8 +1531,12 @@ func (p *Parser) parseArrowFuncBody() (list []IStmt) {
if p.tt == OpenBraceToken {
prevIn, prevRetrn := p.in, p.retrn
p.in, p.retrn = true, true
p.allowDirectivePrologue = true

prevAllowDirectivePrologue, prevExprLevel := p.allowDirectivePrologue, p.exprLevel
p.allowDirectivePrologue, p.exprLevel = true, 0
list = p.parseStmtList("arrow function")
p.allowDirectivePrologue, p.exprLevel = prevAllowDirectivePrologue, prevExprLevel

p.in, p.retrn = prevIn, prevRetrn
} else {
list = []IStmt{&ReturnStmt{p.parseExpression(OpAssign)}}
Expand Down

0 comments on commit 8e56e59

Please sign in to comment.