Skip to content

Commit

Permalink
JS: allow strings in import/export statement lists
Browse files Browse the repository at this point in the history
  • Loading branch information
tdewolff committed Jan 18, 2022
1 parent 1e0dbbe commit e4c6163
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
16 changes: 8 additions & 8 deletions js/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ func (p *Parser) parseImportStmt() (importStmt ImportStmt) {
p.next()
} else if p.tt == OpenBraceToken {
p.next()
for IsIdentifierName(p.tt) {
for IsIdentifierName(p.tt) || p.tt == StringToken {
tt := p.tt
var name, binding []byte = nil, p.data
p.next()
Expand All @@ -655,8 +655,8 @@ func (p *Parser) parseImportStmt() (importStmt ImportStmt) {
name = binding
binding = p.data
p.next()
} else if !IsIdentifier(tt) && tt != YieldToken {
p.fail("import statement", IdentifierToken)
} else if !IsIdentifier(tt) && tt != YieldToken || tt == StringToken {
p.fail("import statement", IdentifierToken, StringToken)
return
}
importStmt.List = append(importStmt.List, Alias{name, binding})
Expand Down Expand Up @@ -702,8 +702,8 @@ func (p *Parser) parseExportStmt() (exportStmt ExportStmt) {
p.next()
if p.tt == AsToken {
p.next()
if !IsIdentifierName(p.tt) {
p.fail("export statement", IdentifierToken)
if !IsIdentifierName(p.tt) && p.tt != StringToken {
p.fail("export statement", IdentifierToken, StringToken)
return
}
exportStmt.List = []Alias{Alias{star, p.data}}
Expand All @@ -717,13 +717,13 @@ func (p *Parser) parseExportStmt() (exportStmt ExportStmt) {
}
} else {
p.next()
for IsIdentifierName(p.tt) {
for IsIdentifierName(p.tt) || p.tt == StringToken {
var name, binding []byte = nil, p.data
p.next()
if p.tt == AsToken {
p.next()
if !IsIdentifierName(p.tt) {
p.fail("export statement", IdentifierToken)
if !IsIdentifierName(p.tt) && p.tt != StringToken {
p.fail("export statement", IdentifierToken, StringToken)
return
}
name = binding
Expand Down
27 changes: 15 additions & 12 deletions js/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,20 @@ func TestParse(t *testing.T) {
{"`tmpl`", "Stmt(`tmpl`)"},
{"`tmpl${x}`", "Stmt(`tmpl${x}`)"},
{"`tmpl${x}tmpl${x}`", "Stmt(`tmpl${x}tmpl${x}`)"},
{"import \"pkg\";", "Stmt(import \"pkg\")"},
{"import yield from \"pkg\"", "Stmt(import yield from \"pkg\")"},
{"import * as yield from \"pkg\"", "Stmt(import * as yield from \"pkg\")"},
{"import {yield, for as yield,} from \"pkg\"", "Stmt(import { yield , for as yield , } from \"pkg\")"},
{"import yield, * as yield from \"pkg\"", "Stmt(import yield , * as yield from \"pkg\")"},
{"import yield, {yield} from \"pkg\"", "Stmt(import yield , { yield } from \"pkg\")"},
{"import {yield,} from \"pkg\"", "Stmt(import { yield , } from \"pkg\")"},
{"export * from \"pkg\";", "Stmt(export * from \"pkg\")"},
{"export * as for from \"pkg\"", "Stmt(export * as for from \"pkg\")"},
{"export {if, for as switch} from \"pkg\"", "Stmt(export { if , for as switch } from \"pkg\")"},
{`import "pkg";`, `Stmt(import "pkg")`},
{`import yield from "pkg"`, `Stmt(import yield from "pkg")`},
{`import * as yield from "pkg"`, `Stmt(import * as yield from "pkg")`},
{`import {yield, for as yield,} from "pkg"`, `Stmt(import { yield , for as yield , } from "pkg")`},
{`import yield, * as yield from "pkg"`, `Stmt(import yield , * as yield from "pkg")`},
{`import yield, {yield} from "pkg"`, `Stmt(import yield , { yield } from "pkg")`},
{`import {yield,} from "pkg"`, `Stmt(import { yield , } from "pkg")`},
{`import {"abc'def" as a} from "pkg"`, `Stmt(import { "abc'def" as a } from "pkg")`},
{`export * from "pkg";`, `Stmt(export * from "pkg")`},
{`export * as for from "pkg"`, `Stmt(export * as for from "pkg")`},
{`export * as "abc'def" from "pkg"`, `Stmt(export * as "abc'def" from "pkg")`},
{`export {if, for as switch} from "pkg"`, `Stmt(export { if , for as switch } from "pkg")`},
{"export {if, for as switch,}", "Stmt(export { if , for as switch , })"},
{`export {"abc'def", "a" as 'b'}`, `Stmt(export { "abc'def" , "a" as 'b' })`},
{"export var a", "Stmt(export Decl(var Binding(a)))"},
{"export function a(b){}", "Stmt(export Decl(function a Params(Binding(b)) Stmt({ })))"},
{"export async function a(b){}", "Stmt(export Decl(async function a Params(Binding(b)) Stmt({ })))"},
Expand Down Expand Up @@ -535,12 +538,12 @@ func TestParseError(t *testing.T) {
{"import yield from", "expected String instead of EOF in import statement"},
{"export", "expected *, {, var, let, const, function, async, class, or default instead of EOF in export statement"},
{"export *", "expected from instead of EOF in export statement"},
{"export * as", "expected Identifier instead of EOF in export statement"},
{"export * as", "expected Identifier or String instead of EOF in export statement"},
{"export * as if", "expected from instead of EOF in export statement"},
{"export {", "expected } instead of EOF in export statement"},
{"export {yield", "expected } instead of EOF in export statement"},
{"export {yield,", "expected } instead of EOF in export statement"},
{"export {yield as", "expected Identifier instead of EOF in export statement"},
{"export {yield as", "expected Identifier or String instead of EOF in export statement"},
{"export {} from", "expected String instead of EOF in export statement"},
{"export {} from", "expected String instead of EOF in export statement"},
{"export async", "expected function instead of EOF in export statement"},
Expand Down

0 comments on commit e4c6163

Please sign in to comment.