Skip to content

Commit

Permalink
switch define data to flags
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Dec 29, 2023
1 parent f5f8ff8 commit 0811058
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 22 deletions.
4 changes: 2 additions & 2 deletions internal/bundler_tests/bundler_dce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1273,8 +1273,8 @@ func TestTreeShakingReactElements(t *testing.T) {

func TestDisableTreeShaking(t *testing.T) {
defines := config.ProcessDefines(map[string]config.DefineData{
"pure": {CallCanBeUnwrappedIfUnused: true},
"some.fn": {CallCanBeUnwrappedIfUnused: true},
"pure": {Flags: config.CallCanBeUnwrappedIfUnused},
"some.fn": {Flags: config.CallCanBeUnwrappedIfUnused},
})
dce_suite.expectBundled(t, bundled{
files: map[string]string{
Expand Down
26 changes: 15 additions & 11 deletions internal/config/globals.go
Original file line number Diff line number Diff line change
Expand Up @@ -869,31 +869,35 @@ type DefineExpr struct {

type DefineData struct {
DefineExpr *DefineExpr
Flags DefineFlags
}

type DefineFlags uint8

const (
// True if accessing this value is known to not have any side effects. For
// example, a bare reference to "Object.create" can be removed because it
// does not have any observable side effects.
CanBeRemovedIfUnused bool
CanBeRemovedIfUnused DefineFlags = 1 << iota

// True if a call to this value is known to not have any side effects. For
// example, a bare call to "Object()" can be removed because it does not
// have any observable side effects.
CallCanBeUnwrappedIfUnused bool
CallCanBeUnwrappedIfUnused

// If true, the user has indicated that every direct calls to a property on
// this object and all of that call's arguments are to be removed from the
// output, even when the arguments have side effects. This is used to
// implement the "--drop:console" flag.
MethodCallsMustBeReplacedWithUndefined bool
MethodCallsMustBeReplacedWithUndefined
)

func (flags DefineFlags) Has(flag DefineFlags) bool {
return (flags & flag) != 0
}

func mergeDefineData(old DefineData, new DefineData) DefineData {
if old.CanBeRemovedIfUnused {
new.CanBeRemovedIfUnused = true
}
if old.CallCanBeUnwrappedIfUnused {
new.CallCanBeUnwrappedIfUnused = true
}
new.Flags |= old.Flags
return new
}

Expand Down Expand Up @@ -937,9 +941,9 @@ func ProcessDefines(userDefines map[string]DefineData) ProcessedDefines {
for _, parts := range knownGlobals {
tail := parts[len(parts)-1]
if len(parts) == 1 {
result.IdentifierDefines[tail] = DefineData{CanBeRemovedIfUnused: true}
result.IdentifierDefines[tail] = DefineData{Flags: CanBeRemovedIfUnused}
} else {
result.DotDefines[tail] = append(result.DotDefines[tail], DotDefine{Parts: parts, Data: DefineData{CanBeRemovedIfUnused: true}})
result.DotDefines[tail] = append(result.DotDefines[tail], DotDefine{Parts: parts, Data: DefineData{Flags: CanBeRemovedIfUnused}})
}
}

Expand Down
14 changes: 7 additions & 7 deletions internal/js_parser/js_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -12880,13 +12880,13 @@ func (p *parser) visitExprInOut(expr js_ast.Expr, in exprIn) (js_ast.Expr, exprO
}

// Copy the side effect flags over in case this expression is unused
if data.CanBeRemovedIfUnused {
if data.Flags.Has(config.CanBeRemovedIfUnused) {
e.CanBeRemovedIfUnused = true
}
if data.CallCanBeUnwrappedIfUnused && !p.options.ignoreDCEAnnotations {
if data.Flags.Has(config.CallCanBeUnwrappedIfUnused) && !p.options.ignoreDCEAnnotations {
e.CallCanBeUnwrappedIfUnused = true
}
if data.MethodCallsMustBeReplacedWithUndefined {
if data.Flags.Has(config.MethodCallsMustBeReplacedWithUndefined) {
methodCallMustBeReplacedWithUndefined = true
}
}
Expand Down Expand Up @@ -13406,10 +13406,10 @@ func (p *parser) visitExprInOut(expr js_ast.Expr, in exprIn) (js_ast.Expr, exprO
}

// Copy the side effect flags over in case this expression is unused
if define.Data.CanBeRemovedIfUnused {
if define.Data.Flags.Has(config.CanBeRemovedIfUnused) {
e.CanBeRemovedIfUnused = true
}
if define.Data.CallCanBeUnwrappedIfUnused && !p.options.ignoreDCEAnnotations {
if define.Data.Flags.Has(config.CallCanBeUnwrappedIfUnused) && !p.options.ignoreDCEAnnotations {
e.CallCanBeUnwrappedIfUnused = true
}
break
Expand Down Expand Up @@ -13529,10 +13529,10 @@ func (p *parser) visitExprInOut(expr js_ast.Expr, in exprIn) (js_ast.Expr, exprO
}

// Copy the side effect flags over in case this expression is unused
if define.Data.CanBeRemovedIfUnused {
if define.Data.Flags.Has(config.CanBeRemovedIfUnused) {
e.CanBeRemovedIfUnused = true
}
if define.Data.CallCanBeUnwrappedIfUnused && !p.options.ignoreDCEAnnotations {
if define.Data.Flags.Has(config.CallCanBeUnwrappedIfUnused) && !p.options.ignoreDCEAnnotations {
e.CallCanBeUnwrappedIfUnused = true
}
break
Expand Down
4 changes: 2 additions & 2 deletions pkg/api/api_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ func validateDefines(
// If we're dropping all console API calls, replace each one with undefined
if (drop & DropConsole) != 0 {
define := rawDefines["console"]
define.MethodCallsMustBeReplacedWithUndefined = true
define.Flags |= config.MethodCallsMustBeReplacedWithUndefined
rawDefines["console"] = define
}

Expand All @@ -709,7 +709,7 @@ func validateDefines(

// Merge with any previously-specified defines
define := rawDefines[key]
define.CallCanBeUnwrappedIfUnused = true
define.Flags |= config.CallCanBeUnwrappedIfUnused
rawDefines[key] = define
}

Expand Down

0 comments on commit 0811058

Please sign in to comment.