Skip to content

Commit

Permalink
JS: fix import statement/call parsing at module level
Browse files Browse the repository at this point in the history
  • Loading branch information
tdewolff committed Apr 15, 2021
1 parent ac1ad92 commit 8c2ea15
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
17 changes: 13 additions & 4 deletions js/parse.go
Expand Up @@ -181,8 +181,18 @@ func (p *Parser) parseModule() (module BlockStmt) {
case ErrorToken:
return
case ImportToken:
importStmt := p.parseImportStmt()
module.List = append(module.List, &importStmt)
p.next()
if p.tt == OpenParenToken {
// could be an import call expression
left := &LiteralExpr{ImportToken, []byte("import")}
p.exprLevel++
suffix := p.parseExpressionSuffix(left, OpExpr, OpCall)
p.exprLevel--
module.List = append(module.List, &ExprStmt{suffix})
} else {
importStmt := p.parseImportStmt()
module.List = append(module.List, &importStmt)
}
case ExportToken:
exportStmt := p.parseExportStmt()
module.List = append(module.List, &exportStmt)
Expand Down Expand Up @@ -585,8 +595,7 @@ func (p *Parser) parseBlockStmt(in string) (blockStmt BlockStmt) {
}

func (p *Parser) parseImportStmt() (importStmt ImportStmt) {
// assume we're at import
p.next()
// assume we're passed import
if p.tt == StringToken {
importStmt.Module = p.data
p.next()
Expand Down
1 change: 1 addition & 0 deletions js/parse_test.go
Expand Up @@ -266,6 +266,7 @@ func TestParse(t *testing.T) {
{"x = new new.target", "Stmt(x=(new (new.target)))"},
{"x = new import.meta", "Stmt(x=(new (import.meta)))"},
{"x = import(a)", "Stmt(x=(import(a)))"},
{"import('module')", "Stmt(import('module'))"},
{"x = +a", "Stmt(x=(+a))"},
{"x = ++a", "Stmt(x=(++a))"},
{"x = -a", "Stmt(x=(-a))"},
Expand Down

0 comments on commit 8c2ea15

Please sign in to comment.