From 1365a07a2088b703241f34342b2d3892fd769d78 Mon Sep 17 00:00:00 2001 From: Evan Wallace Date: Sat, 22 Apr 2023 14:43:08 -0400 Subject: [PATCH] fix #3070: fix detection of non-default re-exports --- CHANGELOG.md | 6 ++++++ internal/bundler_tests/bundler_default_test.go | 10 ++++++++++ internal/js_parser/js_parser.go | 8 ++++---- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aca2af23e41..60f97a47507 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## Unreleased + +* Fix non-default JSON import error with `export {} from` ([#3070](https://github.com/evanw/esbuild/issues/3070)) + + This release fixes a bug where esbuild incorrectly identified statements of the form `export { default as x } from "y" assert { type: "json" }` as a non-default import. The bug did not affect code of the form `import { default as x } from ...` (only code that used the `export` keyword). + ## 0.17.17 * Fix CSS nesting transform for top-level `&` ([#3052](https://github.com/evanw/esbuild/issues/3052)) diff --git a/internal/bundler_tests/bundler_default_test.go b/internal/bundler_tests/bundler_default_test.go index 69065bde48c..ed3f302e7f4 100644 --- a/internal/bundler_tests/bundler_default_test.go +++ b/internal/bundler_tests/bundler_default_test.go @@ -7616,6 +7616,8 @@ func TestErrorsForAssertTypeJSON(t *testing.T) { import * as ns from './foo.json' assert { type: 'json' } use(used, ns.prop) export { exported } from './foo.json' assert { type: 'json' } + export { default as def2 } from './foo.json' assert { type: 'json' } + export { def3 as default } from './foo.json' assert { type: 'json' } import text from './foo.text' assert { type: 'json' } import file from './foo.file' assert { type: 'json' } import copy from './foo.copy' assert { type: 'json' } @@ -7628,6 +7630,8 @@ func TestErrorsForAssertTypeJSON(t *testing.T) { import * as ns from './foo.json' assert { type: 'json' } use(used, ns.prop) export { exported } from './foo.json' assert { type: 'json' } + export { default as def2 } from './foo.json' assert { type: 'json' } + export { def3 as default } from './foo.json' assert { type: 'json' } import text from './foo.text' assert { type: 'json' } import file from './foo.file' assert { type: 'json' } import copy from './foo.copy' assert { type: 'json' } @@ -7665,6 +7669,9 @@ NOTE: You can either keep the import assertion and only use the "default" import js-entry.js: ERROR: Cannot use non-default import "exported" with a standard JSON module js-entry.js: NOTE: This is considered an import of a standard JSON module because of the import assertion here: NOTE: You can either keep the import assertion and only use the "default" import, or you can remove the import assertion and use the "exported" import (which is non-standard behavior). +js-entry.js: ERROR: Cannot use non-default import "def3" with a standard JSON module +js-entry.js: NOTE: This is considered an import of a standard JSON module because of the import assertion here: +NOTE: You can either keep the import assertion and only use the "default" import, or you can remove the import assertion and use the "def3" import (which is non-standard behavior). js-entry.js: ERROR: The file "foo.text" was loaded with the "text" loader js-entry.js: NOTE: This import assertion requires the loader to be "json" instead: NOTE: You need to either reconfigure esbuild to ensure that the loader for this file is "json" or you need to remove this import assertion. @@ -7680,6 +7687,9 @@ NOTE: You can either keep the import assertion and only use the "default" import ts-entry.ts: ERROR: Cannot use non-default import "exported" with a standard JSON module ts-entry.ts: NOTE: This is considered an import of a standard JSON module because of the import assertion here: NOTE: You can either keep the import assertion and only use the "default" import, or you can remove the import assertion and use the "exported" import (which is non-standard behavior). +ts-entry.ts: ERROR: Cannot use non-default import "def3" with a standard JSON module +ts-entry.ts: NOTE: This is considered an import of a standard JSON module because of the import assertion here: +NOTE: You can either keep the import assertion and only use the "default" import, or you can remove the import assertion and use the "def3" import (which is non-standard behavior). `, }) } diff --git a/internal/js_parser/js_parser.go b/internal/js_parser/js_parser.go index 63efda79bb0..2b083be4974 100644 --- a/internal/js_parser/js_parser.go +++ b/internal/js_parser/js_parser.go @@ -15569,10 +15569,10 @@ func (p *parser) scanForImportsAndExports(stmts []js_ast.Stmt) (result importsEx // Forbid non-default imports for standard JSON modules if (record.Flags&ast.AssertTypeJSON) != 0 && p.options.mode == config.ModeBundle { for _, item := range s.Items { - if item.Alias != "default" { - p.log.AddErrorWithNotes(&p.tracker, js_lexer.RangeOfIdentifier(p.source, item.AliasLoc), - fmt.Sprintf("Cannot use non-default import %q with a standard JSON module", item.Alias), - p.notesForAssertTypeJSON(record, item.Alias)) + if item.OriginalName != "default" { + p.log.AddErrorWithNotes(&p.tracker, js_lexer.RangeOfIdentifier(p.source, item.Name.Loc), + fmt.Sprintf("Cannot use non-default import %q with a standard JSON module", item.OriginalName), + p.notesForAssertTypeJSON(record, item.OriginalName)) } } }