Skip to content

Commit

Permalink
JS: support inline JavaScript e.g. for HTML onclick attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
tdewolff committed Oct 31, 2023
1 parent 79249a3 commit de6a78e
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions js/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

type Options struct {
WhileToFor bool
Inline bool
}

// Parser is the state for the parser.
Expand Down Expand Up @@ -44,20 +45,33 @@ func Parse(r *parse.Input, o Options) (*AST, error) {
await: true,
}

// catch shebang in first line
var shebang []byte
if r.Peek(0) == '#' && r.Peek(1) == '!' {
r.Move(2)
p.l.consumeSingleLineComment() // consume till end-of-line
shebang = r.Shift()
}
if o.Inline {
p.next()
p.retrn = true
p.allowDirectivePrologue = true
p.enterScope(&ast.BlockStmt.Scope, true)
for {
if p.tt == ErrorToken {
break
}
ast.BlockStmt.List = append(ast.BlockStmt.List, p.parseStmt(true))
}
} else {
// catch shebang in first line
var shebang []byte
if r.Peek(0) == '#' && r.Peek(1) == '!' {
r.Move(2)
p.l.consumeSingleLineComment() // consume till end-of-line
shebang = r.Shift()
}

// parse JS module
p.next()
ast.BlockStmt = p.parseModule()
// parse JS module
p.next()
ast.BlockStmt = p.parseModule()

if 0 < len(shebang) {
ast.BlockStmt.List = append([]IStmt{&Comment{shebang}}, ast.BlockStmt.List...)
if 0 < len(shebang) {
ast.BlockStmt.List = append([]IStmt{&Comment{shebang}}, ast.BlockStmt.List...)
}
}

if p.err == nil {
Expand Down

0 comments on commit de6a78e

Please sign in to comment.