Skip to content

Commit 5adc1da

Browse files
committed
fix #4041: keep void x when minify is off
1 parent 96d64ca commit 5adc1da

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

internal/js_parser/js_parser.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14033,7 +14033,25 @@ func (p *parser) visitExprInOut(expr js_ast.Expr, in exprIn) (js_ast.Expr, exprO
1403314033
}
1403414034

1403514035
case js_ast.UnOpVoid:
14036-
if p.astHelpers.ExprCanBeRemovedIfUnused(e.Value) {
14036+
var shouldRemove bool
14037+
if p.options.minifySyntax {
14038+
shouldRemove = p.astHelpers.ExprCanBeRemovedIfUnused(e.Value)
14039+
} else {
14040+
// This special case was added for a very obscure reason. There's a
14041+
// custom dialect of JavaScript called Svelte that uses JavaScript
14042+
// syntax with different semantics. Specifically variable accesses
14043+
// have side effects (!). And someone wants to use "void x" instead
14044+
// of just "x" to trigger the side effect for some reason.
14045+
//
14046+
// Arguably this should not be supported, because you shouldn't be
14047+
// running esbuild on weird kinda-JavaScript-but-not languages and
14048+
// expecting it to work correctly. But this one special case seems
14049+
// harmless enough. This is definitely not fully supported though.
14050+
//
14051+
// More info: https://github.com/evanw/esbuild/issues/4041
14052+
shouldRemove = isUnsightlyPrimitive(e.Value.Data)
14053+
}
14054+
if shouldRemove {
1403714055
return js_ast.Expr{Loc: expr.Loc, Data: js_ast.EUndefinedShared}, exprOut{}
1403814056
}
1403914057

internal/js_parser/js_parser_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,13 @@ func expectPrintedJSXAutomatic(t *testing.T, options JSXAutomaticTestOptions, co
230230
})
231231
}
232232

233+
func TestUnOp(t *testing.T) {
234+
// This was important to someone for a very obscure reason. See
235+
// https://github.com/evanw/esbuild/issues/4041 for more info.
236+
expectPrinted(t, "let x; void 0; x", "let x;\nx;\n")
237+
expectPrinted(t, "let x; void x; x", "let x;\nvoid x;\nx;\n")
238+
}
239+
233240
func TestBinOp(t *testing.T) {
234241
for code, entry := range js_ast.OpTable {
235242
opCode := js_ast.OpCode(code)

0 commit comments

Comments
 (0)