Skip to content

Commit

Permalink
follow-up to #2371
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Jul 6, 2022
1 parent 46d352e commit 8b5e048
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 14 deletions.
34 changes: 34 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,39 @@
# Changelog

## Unreleased

* Fix generated TypeScript `enum` comments containing `*/` ([#2369](https://github.com/evanw/esbuild/issues/2369), [#2371](https://github.com/evanw/esbuild/pull/2371))

TypeScript `enum` values that are equal to a number or string literal are inlined (references to the enum are replaced with the literal value) and have a `/* ... */` comment after them with the original enum name to improve readability. However, this comment is omitted if the enum name contains the character sequence `*/` because that would end the comment early and cause a syntax error:

```ts
// Original TypeScript
enum Foo { '/*' = 1, '*/' = 2 }
console.log(Foo['/*'], Foo['*/'])

// Generated JavaScript
console.log(1 /* /* */, 2);
```

This was originally handled correctly when TypeScript `enum` inlining was initially implemented since it was only supported within a single file. However, when esbuild was later extended to support TypeScript `enum` inlining across files, this special case where the enum name contains `*/` was not handled in that new code. Starting with this release, esbuild will now handle enums with names containing `*/` correctly when they are inlined across files:

```ts
// foo.ts
export enum Foo { '/*' = 1, '*/' = 2 }

// bar.ts
import { Foo } from './foo'
console.log(Foo['/*'], Foo['*/'])

// Old output (with --bundle --format=esm)
console.log(1 /* /* */, 2 /* */ */);

// New output (with --bundle --format=esm)
console.log(1 /* /* */, 2);
```

This fix was contributed by [@magic-akari](https://github.com/magic-akari).

## 0.14.48

* Enable using esbuild in Deno via WebAssembly ([#2323](https://github.com/evanw/esbuild/issues/2323))
Expand Down
27 changes: 20 additions & 7 deletions internal/bundler/bundler_ts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,18 +223,31 @@ func TestTSDeclareConstEnum(t *testing.T) {
})
}

func TestConstEnumComments(t *testing.T) {
func TestTSConstEnumComments(t *testing.T) {
ts_suite.expectBundled(t, bundled{
files: map[string]string{
"/bar.ts": `
export const enum PRCD {
"*/%" = 14,
}
export const enum Foo {
"%/*" = 1,
"*/%" = 2,
}
`,
"/foo.ts": `
import { PRCD } from "./bar";
console.log(PRCD["*/%"]);
import { Foo } from "./bar";
const enum Bar {
"%/*" = 1,
"*/%" = 2,
}
console.log({
'should have comments': [
Foo["%/*"],
Bar["%/*"],
],
'should not have comments': [
Foo["*/%"],
Bar["*/%"],
],
});
`,
},
entryPaths: []string{"/foo.ts"},
Expand Down
21 changes: 15 additions & 6 deletions internal/bundler/snapshots/snapshots_ts.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
TestConstEnumComments
---------- /out.js ----------
// foo.ts
console.log(14);

================================================================================
TestExportTypeIssue379
---------- /out.js ----------
// a.ts
Expand Down Expand Up @@ -37,6 +31,21 @@ var foo4 = 123;
// entry.ts
console.log(a_exports, b_exports, c_exports, d_exports);

================================================================================
TestTSConstEnumComments
---------- /out.js ----------
// foo.ts
console.log({
"should have comments": [
1 /* %/* */,
1 /* %/* */
],
"should not have comments": [
2,
2
]
});

================================================================================
TestTSDeclareClass
---------- /out.js ----------
Expand Down
2 changes: 1 addition & 1 deletion internal/js_printer/js_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1940,7 +1940,7 @@ func (p *printer) printExpr(expr js_ast.Expr, level js_ast.L, flags printExprFla
} else {
p.printNumber(value.Number, level)
}
if !p.options.MinifyWhitespace && !p.options.MinifyIdentifiers {
if !p.options.MinifyWhitespace && !p.options.MinifyIdentifiers && !strings.Contains(e.Name, "*/") {
p.print(" /* ")
p.print(e.Name)
p.print(" */")
Expand Down

0 comments on commit 8b5e048

Please sign in to comment.