Skip to content

Commit

Permalink
fix #3070: fix detection of non-default re-exports
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Apr 22, 2023
1 parent 81cb21c commit 1365a07
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
6 changes: 6 additions & 0 deletions 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))
Expand Down
10 changes: 10 additions & 0 deletions internal/bundler_tests/bundler_default_test.go
Expand Up @@ -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' }
Expand All @@ -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' }
Expand Down Expand Up @@ -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.
Expand All @@ -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).
`,
})
}
Expand Down
8 changes: 4 additions & 4 deletions internal/js_parser/js_parser.go
Expand Up @@ -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))
}
}
}
Expand Down

0 comments on commit 1365a07

Please sign in to comment.