Skip to content

Commit

Permalink
print "__PURE__" comments when not minifying (#221)
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Jul 6, 2020
1 parent bfaf350 commit b96fc64
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
28 changes: 14 additions & 14 deletions internal/bundler/bundler_dce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -816,24 +816,24 @@ func TestRemoveUnusedPureCommentCalls(t *testing.T) {
function bar() {
}
let bare = foo(bar);
let at_no = foo(bar());
let new_at_no = new foo(bar());
let num_no = foo(bar());
let new_num_no = new foo(bar());
let dot_no = foo(sideEffect()).dot(bar());
let new_dot_no = new foo(sideEffect()).dot(bar());
let nested_no = [1, foo(bar()), 2];
let new_nested_no = [1, new foo(bar()), 2];
let single_at_no = foo(bar());
let new_single_at_no = new foo(bar());
let single_num_no = foo(bar());
let new_single_num_no = new foo(bar());
let at_no = /* @__PURE__ */ foo(bar());
let new_at_no = /* @__PURE__ */ new foo(bar());
let num_no = /* @__PURE__ */ foo(bar());
let new_num_no = /* @__PURE__ */ new foo(bar());
let dot_no = /* @__PURE__ */ foo(sideEffect()).dot(bar());
let new_dot_no = /* @__PURE__ */ new foo(sideEffect()).dot(bar());
let nested_no = [1, /* @__PURE__ */ foo(bar()), 2];
let new_nested_no = [1, /* @__PURE__ */ new foo(bar()), 2];
let single_at_no = /* @__PURE__ */ foo(bar());
let new_single_at_no = /* @__PURE__ */ new foo(bar());
let single_num_no = /* @__PURE__ */ foo(bar());
let new_single_num_no = /* @__PURE__ */ new foo(bar());
let bad_no = foo(bar);
let new_bad_no = new foo(bar);
let parens_no = foo(bar);
let new_parens_no = new foo(bar);
let exp_no = foo() ** foo();
let new_exp_no = new foo() ** foo();
let exp_no = /* @__PURE__ */ foo() ** foo();
let new_exp_no = /* @__PURE__ */ new foo() ** foo();
`,
},
})
Expand Down
21 changes: 21 additions & 0 deletions internal/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1140,9 +1140,20 @@ func (p *printer) printExpr(expr ast.Expr, level ast.L, flags int) {

case *ast.ENew:
wrap := level >= ast.LCall

hasPureComment := !p.options.RemoveWhitespace && e.CanBeUnwrappedIfUnused
if hasPureComment && level >= ast.LPostfix {
wrap = true
}

if wrap {
p.print("(")
}

if hasPureComment {
p.print("/* @__PURE__ */ ")
}

p.printSpaceBeforeIdentifier()
p.print("new")
p.printSpace()
Expand Down Expand Up @@ -1171,10 +1182,20 @@ func (p *printer) printExpr(expr ast.Expr, level ast.L, flags int) {
} else if (flags & hasNonOptionalChainParent) != 0 {
wrap = true
}

hasPureComment := !p.options.RemoveWhitespace && e.CanBeUnwrappedIfUnused
if hasPureComment && level >= ast.LPostfix {
wrap = true
}

if wrap {
p.print("(")
}

if hasPureComment {
p.print("/* @__PURE__ */ ")
}

// We don't ever want to accidentally generate a direct eval expression here
if !e.IsDirectEval && p.isUnboundEvalIdentifier(e.Target) {
if p.options.RemoveWhitespace {
Expand Down

0 comments on commit b96fc64

Please sign in to comment.