Skip to content

Commit

Permalink
fix #2036: strip unsupported import() assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Feb 21, 2022
1 parent bf341f7 commit f93f488
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
# Changelog

## Unreleased

* Strip import assertions from unrecognized `import()` expressions ([#2036](https://github.com/evanw/esbuild/issues/2036))

The new "import assertions" JavaScript language feature adds an optional second argument to dynamic `import()` expressions, which esbuild does support. However, this optional argument must be stripped when targeting older JavaScript environments for which this second argument would be a syntax error. Previously esbuild failed to strip this second argument in cases when the first argument to `import()` wasn't a string literal. This problem is now fixed:

```js
// Original code
console.log(import(foo, { assert: { type: 'json' } }))

// Old output (with --target=es6)
console.log(import(foo, { assert: { type: "json" } }));

// New output (with --target=es6)
console.log(import(foo));
```

## 0.14.23

* Update feature database to indicate that node 16.14+ supports import assertions ([#2030](https://github.com/evanw/esbuild/issues/2030))
Expand Down
3 changes: 3 additions & 0 deletions internal/js_parser/js_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4741,6 +4741,9 @@ func TestImportAssertions(t *testing.T) {
expectPrinted(t, "import(x ? 'y' : 'z', {assert: {x: 1}})", "import(x ? \"y\" : \"z\", { assert: { x: 1 } });\n")

expectPrintedTarget(t, 2015, "import 'x' assert {x: 'y'}", "import \"x\";\n")
expectPrintedTarget(t, 2015, "import(x, {assert: {x: 'y'}})", "import(x);\n")
expectPrintedTarget(t, 2015, "import(x, {assert: {x: 1}})", "import(x);\n")
expectPrintedTarget(t, 2015, "import(x ? 'y' : 'z', {assert: {x: 'y'}})", "x ? import(\"y\") : import(\"z\");\n")
expectPrintedTarget(t, 2015, "import(x ? 'y' : 'z', {assert: {x: 1}})", "import(x ? \"y\" : \"z\");\n")
expectParseErrorTarget(t, 2015, "import(x ? 'y' : 'z', {assert: {x: foo()}})",
"<stdin>: ERROR: Using an arbitrary value as the second argument to \"import()\" is not possible in the configured target environment\n")
Expand Down
5 changes: 4 additions & 1 deletion internal/js_printer/js_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1890,11 +1890,14 @@ func (p *printer) printExpr(expr js_ast.Expr, level js_ast.L, flags printExprFla
p.printIndent()
}
p.printExpr(e.Expr, js_ast.LComma, 0)
if e.OptionsOrNil.Data != nil {

// Just omit import assertions if they aren't supported
if e.OptionsOrNil.Data != nil && !p.options.UnsupportedFeatures.Has(compat.ImportAssertions) {
p.print(",")
p.printSpace()
p.printExpr(e.OptionsOrNil, js_ast.LComma, 0)
}

if len(leadingInteriorComments) > 0 {
p.printNewline()
p.options.Indent--
Expand Down

0 comments on commit f93f488

Please sign in to comment.