Skip to content

Commit

Permalink
fix #1903: bug with enum inlining and "export {}"
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Jan 8, 2022
1 parent 86e6278 commit 08c9aa9
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

* Fix a bug with enum inlining ([#1903](https://github.com/evanw/esbuild/issues/1903))

The new TypeScript enum inlining behavior had a bug where it worked correctly if you used `export enum Foo` but not if you used `enum Foo` and then later `export { Foo }`. This release fixes the bug so enum inlining now works correctly in this case.

* Warn about `module.exports.foo = ...` in ESM ([#1907](https://github.com/evanw/esbuild/issues/1907))

The `module` variable is treated as a global variable reference instead of as a CommonJS module reference in ESM code, which can cause problems for people that try to use both CommonJS and ESM exports in the same file. There has been a warning about this since version 0.14.9. However, the warning only covered cases like `exports.foo = bar` and `module.exports = bar` but not `module.exports.foo = bar`. This last case is now handled;
Expand Down
34 changes: 34 additions & 0 deletions internal/bundler/bundler_ts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1860,3 +1860,37 @@ func TestTSEnumCrossModuleTreeShaking(t *testing.T) {
},
})
}

func TestTSEnumExportClause(t *testing.T) {
ts_suite.expectBundled(t, bundled{
files: map[string]string{
"/entry.ts": `
import {
A,
B,
C as c,
d as dd,
} from './enums'
console.log([
A.A,
B.B,
c.C,
dd.D,
])
`,
"/enums.ts": `
export enum A { A = 1 }
enum B { B = 2 }
export enum C { C = 3 }
enum D { D = 4 }
export { B, D as d }
`,
},
entryPaths: []string{"/entry.ts"},
options: config.Options{
Mode: config.ModeBundle,
AbsOutputDir: "/out",
},
})
}
11 changes: 11 additions & 0 deletions internal/bundler/snapshots/snapshots_ts.txt
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,17 @@ var a = /* @__PURE__ */ ((a2) => {
return a2;
})(a || {});

================================================================================
TestTSEnumExportClause
---------- /out/entry.js ----------
// entry.ts
console.log([
1 /* A */,
2 /* B */,
3 /* C */,
4 /* D */
]);

================================================================================
TestTSEnumJSX
---------- /out/element.js ----------
Expand Down
4 changes: 4 additions & 0 deletions internal/js_ast/js_ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -1578,6 +1578,10 @@ const (
// it's not safe to make assumptions about this symbol from the initializer.
CouldPotentiallyBeMutated

// This flags all symbols that were exported from the module using the ES6
// "export" keyword, either directly on the declaration or using "export {}".
WasExported

// This means the symbol is a normal function that has no body statements.
IsEmptyFunction

Expand Down
22 changes: 11 additions & 11 deletions internal/js_parser/js_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -9554,9 +9554,9 @@ func (p *parser) visitAndAppendStmt(stmts []js_ast.Stmt, stmt js_ast.Stmt) []js_

case *js_ast.SEnum:
// Track cross-module enum constants during bundling
var tsExportedTopLevelEnumValues map[string]js_ast.TSEnumValue
if s.IsExport && p.currentScope == p.moduleScope && p.options.mode == config.ModeBundle {
tsExportedTopLevelEnumValues = make(map[string]js_ast.TSEnumValue)
var tsTopLevelEnumValues map[string]js_ast.TSEnumValue
if p.currentScope == p.moduleScope && p.options.mode == config.ModeBundle {
tsTopLevelEnumValues = make(map[string]js_ast.TSEnumValue)
}

p.recordDeclaredSymbol(s.Name.Ref)
Expand Down Expand Up @@ -9602,8 +9602,8 @@ func (p *parser) visitAndAppendStmt(stmts []js_ast.Stmt, stmt js_ast.Stmt) []js_

switch e := value.ValueOrNil.Data.(type) {
case *js_ast.ENumber:
if tsExportedTopLevelEnumValues != nil {
tsExportedTopLevelEnumValues[name] = js_ast.TSEnumValue{Number: e.Value}
if tsTopLevelEnumValues != nil {
tsTopLevelEnumValues[name] = js_ast.TSEnumValue{Number: e.Value}
}
member := exportedMembers[name]
member.Data = &js_ast.TSNamespaceMemberEnumNumber{Value: e.Value}
Expand All @@ -9613,8 +9613,8 @@ func (p *parser) visitAndAppendStmt(stmts []js_ast.Stmt, stmt js_ast.Stmt) []js_
nextNumericValue = e.Value + 1

case *js_ast.EString:
if tsExportedTopLevelEnumValues != nil {
tsExportedTopLevelEnumValues[name] = js_ast.TSEnumValue{String: e.Value}
if tsTopLevelEnumValues != nil {
tsTopLevelEnumValues[name] = js_ast.TSEnumValue{String: e.Value}
}
member := exportedMembers[name]
member.Data = &js_ast.TSNamespaceMemberEnumString{Value: e.Value}
Expand All @@ -9628,8 +9628,8 @@ func (p *parser) visitAndAppendStmt(stmts []js_ast.Stmt, stmt js_ast.Stmt) []js_
}
}
} else if hasNumericValue {
if tsExportedTopLevelEnumValues != nil {
tsExportedTopLevelEnumValues[name] = js_ast.TSEnumValue{Number: nextNumericValue}
if tsTopLevelEnumValues != nil {
tsTopLevelEnumValues[name] = js_ast.TSEnumValue{Number: nextNumericValue}
}
member := exportedMembers[name]
member.Data = &js_ast.TSNamespaceMemberEnumNumber{Value: nextNumericValue}
Expand Down Expand Up @@ -9683,11 +9683,11 @@ func (p *parser) visitAndAppendStmt(stmts []js_ast.Stmt, stmt js_ast.Stmt) []js_
p.shouldFoldNumericConstants = oldShouldFoldNumericConstants

// Track all exported top-level enums for cross-module inlining
if tsExportedTopLevelEnumValues != nil {
if tsTopLevelEnumValues != nil {
if p.tsEnums == nil {
p.tsEnums = make(map[js_ast.Ref]map[string]js_ast.TSEnumValue)
}
p.tsEnums[s.Name.Ref] = tsExportedTopLevelEnumValues
p.tsEnums[s.Name.Ref] = tsTopLevelEnumValues
}

// Wrap this enum definition in a closure
Expand Down

0 comments on commit 08c9aa9

Please sign in to comment.