Skip to content

Commit 24811f1

Browse files
committed
avoid using the alias "*" for namespace objects
1 parent 491dd50 commit 24811f1

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
lines changed

internal/bundler/linker.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,8 @@ type fileMeta struct {
232232
//
233233
// Re-exports come from other files and are the result of resolving export
234234
// star statements (i.e. "export * from 'foo'").
235-
resolvedExports map[string]exportData
235+
resolvedExports map[string]exportData
236+
resolvedExportStar *exportData
236237

237238
// Never iterate over "resolvedExports" directly. Instead, iterate over this
238239
// array. Some exports in that map aren't meant to end up in generated code.
@@ -1248,10 +1249,10 @@ func (c *linkerContext) scanImportsAndExports() {
12481249
IsNamespaceExport: true,
12491250
}, partMeta{})
12501251

1251-
// Also add a special export called "*" so import stars can bind to it.
1252-
// This must be done in this step because it must come after CommonJS
1253-
// module discovery but before matching imports with exports.
1254-
repr.meta.resolvedExports["*"] = exportData{
1252+
// Also add a special export so import stars can bind to it. This must be
1253+
// done in this step because it must come after CommonJS module discovery
1254+
// but before matching imports with exports.
1255+
repr.meta.resolvedExportStar = &exportData{
12551256
ref: repr.ast.ExportsRef,
12561257
sourceIndex: sourceIndex,
12571258
}
@@ -1302,12 +1303,6 @@ func (c *linkerContext) scanImportsAndExports() {
13021303
aliases := make([]string, 0, len(repr.meta.resolvedExports))
13031304
nextAlias:
13041305
for alias, export := range repr.meta.resolvedExports {
1305-
// The automatically-generated namespace export is just for internal binding
1306-
// purposes and isn't meant to end up in generated code.
1307-
if alias == "*" {
1308-
continue
1309-
}
1310-
13111306
// Re-exporting multiple symbols with the same name causes an ambiguous
13121307
// export. These names cannot be used and should not end up in generated code.
13131308
otherRepr := c.files[export.sourceIndex].repr.(*reprJS)
@@ -2286,7 +2281,7 @@ func (c *linkerContext) advanceImportTracker(tracker importTracker) (importTrack
22862281

22872282
// Is this a named import of a file without any exports?
22882283
otherRepr := c.files[otherSourceIndex].repr.(*reprJS)
2289-
if namedImport.Alias != "*" && !otherRepr.ast.UsesCommonJSExports() && !otherRepr.ast.HasESMFeatures() && !otherRepr.ast.HasLazyExport {
2284+
if !namedImport.AliasIsStar && !otherRepr.ast.UsesCommonJSExports() && !otherRepr.ast.HasESMFeatures() && !otherRepr.ast.HasLazyExport {
22902285
// Just warn about it and replace the import with "undefined"
22912286
return importTracker{sourceIndex: otherSourceIndex, importRef: js_ast.InvalidRef}, importCommonJSWithoutExports, nil
22922287
}
@@ -2296,6 +2291,16 @@ func (c *linkerContext) advanceImportTracker(tracker importTracker) (importTrack
22962291
return importTracker{sourceIndex: otherSourceIndex, importRef: js_ast.InvalidRef}, importCommonJS, nil
22972292
}
22982293

2294+
// Match this import star with an export star from the imported file
2295+
if matchingExport := otherRepr.meta.resolvedExportStar; namedImport.AliasIsStar && matchingExport != nil {
2296+
// Check to see if this is a re-export of another import
2297+
return importTracker{
2298+
sourceIndex: matchingExport.sourceIndex,
2299+
importRef: matchingExport.ref,
2300+
nameLoc: matchingExport.nameLoc,
2301+
}, importFound, matchingExport.potentiallyAmbiguousExportStarRefs
2302+
}
2303+
22992304
// Match this import up with an export from the imported file
23002305
if matchingExport, ok := otherRepr.meta.resolvedExports[namedImport.Alias]; ok {
23012306
// Check to see if this is a re-export of another import
@@ -3813,9 +3818,7 @@ func (repr *chunkReprJS) generate(c *linkerContext, chunk *chunkInfo) func(gener
38133818
resolvedExports := fileRepr.meta.resolvedExports
38143819
aliases = make([]string, 0, len(resolvedExports))
38153820
for alias := range resolvedExports {
3816-
if alias != "*" {
3817-
aliases = append(aliases, alias)
3818-
}
3821+
aliases = append(aliases, alias)
38193822
}
38203823
}
38213824
} else {

internal/js_ast/js_ast.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,6 +1739,12 @@ type NamedImport struct {
17391739
NamespaceRef Ref
17401740
ImportRecordIndex uint32
17411741

1742+
// If true, the alias refers to the entire export namespace object of a
1743+
// module. This is no longer represented as an alias called "*" because of
1744+
// the upcoming "Arbitrary module namespace identifier names" feature:
1745+
// https://github.com/tc39/ecma262/pull/2154
1746+
AliasIsStar bool
1747+
17421748
// It's useful to flag exported imports because if they are in a TypeScript
17431749
// file, we can't tell if they are a type or a value.
17441750
IsExported bool

internal/js_parser/js_parser.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11980,7 +11980,7 @@ func (p *parser) scanForImportsAndExports(stmts []js_ast.Stmt) (result scanForIm
1198011980

1198111981
if s.StarNameLoc != nil {
1198211982
p.namedImports[s.NamespaceRef] = js_ast.NamedImport{
11983-
Alias: "*",
11983+
AliasIsStar: true,
1198411984
AliasLoc: *s.StarNameLoc,
1198511985
NamespaceRef: js_ast.InvalidRef,
1198611986
ImportRecordIndex: s.ImportRecordIndex,
@@ -12070,7 +12070,7 @@ func (p *parser) scanForImportsAndExports(stmts []js_ast.Stmt) (result scanForIm
1207012070
if s.Alias != nil {
1207112071
// "export * as ns from 'path'"
1207212072
p.namedImports[s.NamespaceRef] = js_ast.NamedImport{
12073-
Alias: "*",
12073+
AliasIsStar: true,
1207412074
AliasLoc: s.Alias.Loc,
1207512075
NamespaceRef: js_ast.InvalidRef,
1207612076
ImportRecordIndex: s.ImportRecordIndex,

0 commit comments

Comments
 (0)