Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 69 additions & 8 deletions js/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -1151,11 +1151,56 @@ func (alias Alias) JS(w io.Writer) {
w.Write(alias.Binding)
}

// ImportAttribute is a key and value in the with clause of an import or export statement, such as type: "json".
type ImportAttribute struct {
Key []byte // identifier name or string literal
Value []byte // string literal
}

func (attr ImportAttribute) String() string {
return string(attr.Key) + ": " + string(attr.Value)
}

// JS writes JavaScript to writer.
func (attr ImportAttribute) JS(w io.Writer) {
w.Write(attr.Key)
w.Write([]byte(": "))
w.Write(attr.Value)
}

func importAttributesString(attrs []ImportAttribute) string {
s := "with {"
for i, attr := range attrs {
if i != 0 {
s += " ,"
}
s += " " + attr.String()
}
return s + " }"
}

func importAttributesJS(w io.Writer, attrs []ImportAttribute) {
if len(attrs) == 0 {
w.Write([]byte("with {}"))
return
}
w.Write([]byte("with {"))
for i, attr := range attrs {
if i != 0 {
w.Write([]byte(","))
}
w.Write([]byte(" "))
attr.JS(w)
}
w.Write([]byte(" }"))
}

// ImportStmt is an import statement.
type ImportStmt struct {
List []Alias
Default []byte // can be nil
Module []byte
List []Alias
Default []byte // can be nil
Module []byte
Attributes []ImportAttribute // nil if there is no with clause
}

func (n ImportStmt) String() string {
Expand Down Expand Up @@ -1184,7 +1229,11 @@ func (n ImportStmt) String() string {
if n.Default != nil || n.List != nil {
s.WriteString(" from")
}
return s.String() + " " + string(n.Module) + ")"
s.WriteString(" " + string(n.Module))
if n.Attributes != nil {
s.WriteString(" " + importAttributesString(n.Attributes))
}
return s.String() + ")"
}

// JS writes JavaScript to writer.
Expand Down Expand Up @@ -1225,15 +1274,20 @@ func (n ImportStmt) JS(w io.Writer) {
}
w.Write([]byte(" "))
w.Write(n.Module)
if n.Attributes != nil {
w.Write([]byte(" "))
importAttributesJS(w, n.Attributes)
}
w.Write([]byte(";"))
}

// ExportStmt is an export statement.
type ExportStmt struct {
List []Alias
Module []byte // can be nil
Default bool
Decl IExpr
List []Alias
Module []byte // can be nil
Default bool
Decl IExpr
Attributes []ImportAttribute // nil if there is no with clause
}

func (n ExportStmt) String() string {
Expand All @@ -1260,6 +1314,9 @@ func (n ExportStmt) String() string {
if n.Module != nil {
s += " from " + string(n.Module)
}
if n.Attributes != nil {
s += " " + importAttributesString(n.Attributes)
}
return s + ")"
}

Expand Down Expand Up @@ -1299,6 +1356,10 @@ func (n ExportStmt) JS(w io.Writer) {
w.Write([]byte(" from "))
w.Write(n.Module)
}
if n.Attributes != nil {
w.Write([]byte(" "))
importAttributesJS(w, n.Attributes)
}
w.Write([]byte(";"))
}

Expand Down
3 changes: 3 additions & 0 deletions js/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ func TestJS(t *testing.T) {
{"import { foo , bar } from 'module-name/path/to/specific/un-exported/file';", "import { foo, bar } from 'module-name/path/to/specific/un-exported/file';"},
{"import defaultExport, * as name from 'module-name';", "import defaultExport, * as name from 'module-name';"},
{"import 'module-name';", "import 'module-name';"},
{"import json from './a.json' with {type:'json'};", "import json from './a.json' with { type: 'json' };"},
{"import 'a' with {};", "import 'a' with {};"},
{"export * from 'a' with {type:'json', 'b':'c'};", "export * from 'a' with { type: 'json', 'b': 'c' };"},
{"var promise = import('module-name');", "var promise = import('module-name');"},
{"export { myFunction as default }", "export { myFunction as default };"},
{"export default k = 12;", "export default k = 12;"},
Expand Down
44 changes: 44 additions & 0 deletions js/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -763,12 +763,51 @@ func (p *Parser) parseImportStmt() (importStmt ImportStmt) {
importStmt.Module = p.data
p.next()
}
if p.tt == WithToken {
if importStmt.Attributes = p.parseWithClause("import statement"); importStmt.Attributes == nil {
return
}
}
if p.tt == SemicolonToken {
p.next()
}
return
}

// parseWithClause parses the import attributes of an import or export statement, it returns nil on error
func (p *Parser) parseWithClause(in string) []ImportAttribute {
// assume we're at with
p.next()
if !p.consume(in, OpenBraceToken) {
return nil
}
attrs := []ImportAttribute{}
for p.tt != CloseBraceToken {
if !IsIdentifierName(p.tt) && p.tt != StringToken {
p.fail(in, IdentifierToken, StringToken, CloseBraceToken)
return nil
}
key := p.data
p.next()
if !p.consume(in, ColonToken) {
return nil
} else if p.tt != StringToken {
p.fail(in, StringToken)
return nil
}
attrs = append(attrs, ImportAttribute{key, p.data})
p.next()
if p.tt == CommaToken {
p.next()
} else if p.tt != CloseBraceToken {
p.fail(in, CommaToken, CloseBraceToken)
return nil
}
}
p.next()
return attrs
}

func (p *Parser) parseExportStmt() (exportStmt ExportStmt) {
// assume we're at export
p.next()
Expand Down Expand Up @@ -829,6 +868,11 @@ func (p *Parser) parseExportStmt() (exportStmt ExportStmt) {
}
exportStmt.Module = p.data
p.next()
if p.tt == WithToken {
if exportStmt.Attributes = p.parseWithClause("export statement"); exportStmt.Attributes == nil {
return
}
}
}
} else if p.tt == VarToken || p.tt == ConstToken || p.tt == LetToken {
tt := p.tt
Expand Down
12 changes: 12 additions & 0 deletions js/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ func TestParse(t *testing.T) {
{`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")`},
{`import json from "./a.json" with { type: "json" };`, `Stmt(import json from "./a.json" with { type: "json" })`},
{`import "pkg" with {}`, `Stmt(import "pkg" with { })`},
{`import * as a from "pkg" with {type: "json", "if": 'b',}`, `Stmt(import * as a from "pkg" with { type: "json" , "if": 'b' })`},
{`export * from "pkg" with {type: "json"}`, `Stmt(export * from "pkg" with { type: "json" })`},
{`export {a as b} from "pkg" with {type: "json"}`, `Stmt(export { a as b } from "pkg" with { type: "json" })`},
{`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")`},
Expand Down Expand Up @@ -564,6 +569,13 @@ func TestParseError(t *testing.T) {
{"import yield", "expected from instead of EOF in import statement"},
{"import yield,", "expected * or { instead of EOF in import statement"},
{"import yield from", "expected String instead of EOF in import statement"},
{`import "pkg" with`, "expected { instead of EOF in import statement"},
{`import "pkg" with {`, "expected Identifier, String, or } instead of EOF in import statement"},
{`import "pkg" with {type}`, "expected : instead of } in import statement"},
{`import "pkg" with {type: json}`, "expected String instead of json in import statement"},
{`import "pkg" with {type: "json" a: "b"}`, "expected , or } instead of a in import statement"},
{`export * from "pkg" with {1: "json"}`, "expected Identifier, String, or } instead of 1 in export statement"},
{`export {a} with {type: "json"}`, "expected ( instead of { in with 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 or String instead of EOF in export statement"},
Expand Down