Skip to content

Commit

Permalink
fix #2203: export default of /* @__PURE__ */
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Apr 22, 2022
1 parent e9728a6 commit e876856
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
# Changelog

## Unreleased

* Fix code generation for `export default` and `/* @__PURE__ */` call ([#2203](https://github.com/evanw/esbuild/issues/2203))

The `/* @__PURE__ */` comment annotation can be added to function calls to indicate that they are side-effect free. These annotations are passed through into the output by esbuild since many JavaScript tools understand them. However, there was an edge case where printing this comment before a function call caused esbuild to fail to parenthesize a function literal because it thought it was no longer at the start of the expression. This problem has been fixed:

```js
// Original code
export default /* @__PURE__ */ (function() {
})()

// Old output
export default /* @__PURE__ */ function() {
}();

// New output
export default /* @__PURE__ */ (function() {
})();
```

## 0.14.38

* Further fixes to TypeScript 4.7 instantiation expression parsing ([#2201](https://github.com/evanw/esbuild/issues/2201))
Expand Down
4 changes: 4 additions & 0 deletions internal/js_printer/js_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1817,10 +1817,14 @@ func (p *printer) printExpr(expr js_ast.Expr, level js_ast.L, flags printExprFla

if hasPureComment {
wasStmtStart := p.stmtStart == len(p.js)
wasExportDefaultStart := p.exportDefaultStart == len(p.js)
p.print("/* @__PURE__ */ ")
if wasStmtStart {
p.stmtStart = len(p.js)
}
if wasExportDefaultStart {
p.exportDefaultStart = len(p.js)
}
}

// We don't ever want to accidentally generate a direct eval expression here
Expand Down
10 changes: 10 additions & 0 deletions internal/js_printer/js_printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,16 @@ func TestPureComment(t *testing.T) {
expectPrinted(t,
"/*@__PURE__*/new (function() {})()",
"/* @__PURE__ */ new function() {\n}();\n")

expectPrinted(t,
"export default (function() {})",
"export default (function() {\n});\n")
expectPrinted(t,
"export default (function() {})()",
"export default (function() {\n})();\n")
expectPrinted(t,
"export default /*@__PURE__*/(function() {})()",
"export default /* @__PURE__ */ (function() {\n})();\n")
}

func TestGenerator(t *testing.T) {
Expand Down

0 comments on commit e876856

Please sign in to comment.