From 16c522eb20c7312b5a9f47914ce28ea4e91a5dff Mon Sep 17 00:00:00 2001 From: bardrop Date: Thu, 12 Nov 2026 20:22:23 +0530 Subject: [PATCH] JS: parse import attributes (with clause) in import and export statements `import json from "./a.json" with { type: "json" }` failed with "expected ( instead of { in with statement". Parse the WithClause after the module specifier of import and export-from statements into a new Attributes field (nil when there is no clause) and write it back in String() and JS(). Fixes tdewolff/minify#786 --- js/ast.go | 77 +++++++++++++++++++++++++++++++++++++++++++----- js/ast_test.go | 3 ++ js/parse.go | 44 +++++++++++++++++++++++++++ js/parse_test.go | 12 ++++++++ 4 files changed, 128 insertions(+), 8 deletions(-) diff --git a/js/ast.go b/js/ast.go index 35618a2..3ba221a 100644 --- a/js/ast.go +++ b/js/ast.go @@ -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 { @@ -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. @@ -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 { @@ -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 + ")" } @@ -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(";")) } diff --git a/js/ast_test.go b/js/ast_test.go index 955543c..b756b70 100644 --- a/js/ast_test.go +++ b/js/ast_test.go @@ -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;"}, diff --git a/js/parse.go b/js/parse.go index e397649..07fbeed 100644 --- a/js/parse.go +++ b/js/parse.go @@ -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() @@ -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 diff --git a/js/parse_test.go b/js/parse_test.go index 3843afb..ca834fc 100644 --- a/js/parse_test.go +++ b/js/parse_test.go @@ -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")`}, @@ -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"},