Skip to content

Commit

Permalink
use args struct for define implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Dec 29, 2020
1 parent 36bb205 commit b26c0fa
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 22 deletions.
2 changes: 1 addition & 1 deletion internal/bundler/bundler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1750,7 +1750,7 @@ func (cache *runtimeCache) processedDefines(key config.Platform) (defines *confi
}
result := config.ProcessDefines(map[string]config.DefineData{
"__platform": {
DefineFunc: func(logger.Loc, config.FindSymbol) js_ast.E {
DefineFunc: func(config.DefineArgs) js_ast.E {
return &js_ast.EString{Value: js_lexer.StringToUTF16(platform)}
},
},
Expand Down
15 changes: 7 additions & 8 deletions internal/bundler/bundler_default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/evanw/esbuild/internal/config"
"github.com/evanw/esbuild/internal/js_ast"
"github.com/evanw/esbuild/internal/logger"
)

var default_suite = suite{
Expand Down Expand Up @@ -3251,8 +3250,8 @@ func TestInjectDuplicate(t *testing.T) {
func TestInject(t *testing.T) {
defines := config.ProcessDefines(map[string]config.DefineData{
"chain.prop": {
DefineFunc: func(loc logger.Loc, findSymbol config.FindSymbol) js_ast.E {
return &js_ast.EIdentifier{Ref: findSymbol(loc, "replace")}
DefineFunc: func(args config.DefineArgs) js_ast.E {
return &js_ast.EIdentifier{Ref: args.FindSymbol(args.Loc, "replace")}
},
},
})
Expand Down Expand Up @@ -3318,8 +3317,8 @@ func TestInject(t *testing.T) {
func TestInjectNoBundle(t *testing.T) {
defines := config.ProcessDefines(map[string]config.DefineData{
"chain.prop": {
DefineFunc: func(loc logger.Loc, findSymbol config.FindSymbol) js_ast.E {
return &js_ast.EIdentifier{Ref: findSymbol(loc, "replace")}
DefineFunc: func(args config.DefineArgs) js_ast.E {
return &js_ast.EIdentifier{Ref: args.FindSymbol(args.Loc, "replace")}
},
},
})
Expand Down Expand Up @@ -3379,8 +3378,8 @@ func TestInjectNoBundle(t *testing.T) {
func TestInjectJSX(t *testing.T) {
defines := config.ProcessDefines(map[string]config.DefineData{
"React.createElement": {
DefineFunc: func(loc logger.Loc, findSymbol config.FindSymbol) js_ast.E {
return &js_ast.EIdentifier{Ref: findSymbol(loc, "el")}
DefineFunc: func(args config.DefineArgs) js_ast.E {
return &js_ast.EIdentifier{Ref: args.FindSymbol(args.Loc, "el")}
},
},
})
Expand Down Expand Up @@ -3567,7 +3566,7 @@ func TestProcessEnvNodeEnvWarningNode(t *testing.T) {
func TestProcessEnvNodeEnvWarningDefine(t *testing.T) {
defines := config.ProcessDefines(map[string]config.DefineData{
"process.env.NODE_ENV": {
DefineFunc: func(loc logger.Loc, findSymbol config.FindSymbol) js_ast.E {
DefineFunc: func(args config.DefineArgs) js_ast.E {
return &js_ast.ENull{}
},
},
Expand Down
14 changes: 9 additions & 5 deletions internal/config/globals.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,12 @@ var knownGlobals = [][]string{
{"Math", "trunc"},
}

type FindSymbol func(logger.Loc, string) js_ast.Ref
type DefineFunc func(logger.Loc, FindSymbol) js_ast.E
type DefineArgs struct {
Loc logger.Loc
FindSymbol func(logger.Loc, string) js_ast.Ref
}

type DefineFunc func(DefineArgs) js_ast.E

type DefineData struct {
DefineFunc DefineFunc
Expand Down Expand Up @@ -193,13 +197,13 @@ func ProcessDefines(userDefines map[string]DefineData) ProcessedDefines {

// Swap in certain literal values because those can be constant folded
result.IdentifierDefines["undefined"] = DefineData{
DefineFunc: func(logger.Loc, FindSymbol) js_ast.E { return &js_ast.EUndefined{} },
DefineFunc: func(DefineArgs) js_ast.E { return &js_ast.EUndefined{} },
}
result.IdentifierDefines["NaN"] = DefineData{
DefineFunc: func(logger.Loc, FindSymbol) js_ast.E { return &js_ast.ENumber{Value: math.NaN()} },
DefineFunc: func(DefineArgs) js_ast.E { return &js_ast.ENumber{Value: math.NaN()} },
}
result.IdentifierDefines["Infinity"] = DefineData{
DefineFunc: func(logger.Loc, FindSymbol) js_ast.E { return &js_ast.ENumber{Value: math.Inf(1)} },
DefineFunc: func(DefineArgs) js_ast.E { return &js_ast.ENumber{Value: math.Inf(1)} },
}

// Warn about use of this without a define
Expand Down
7 changes: 5 additions & 2 deletions internal/js_parser/js_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type parser struct {
moduleRef js_ast.Ref
importMetaRef js_ast.Ref
promiseRef js_ast.Ref
findSymbolHelper config.FindSymbol
findSymbolHelper func(loc logger.Loc, name string) js_ast.Ref
symbolUses map[js_ast.Ref]js_ast.SymbolUse
declaredSymbols []js_ast.DeclaredSymbol
runtimeImports map[string]js_ast.Ref
Expand Down Expand Up @@ -9986,7 +9986,10 @@ func (p *parser) visitExprInOut(expr js_ast.Expr, in exprIn) (js_ast.Expr, exprO
}

func (p *parser) valueForDefine(loc logger.Loc, assignTarget js_ast.AssignTarget, isDeleteTarget bool, defineFunc config.DefineFunc) js_ast.Expr {
expr := js_ast.Expr{Loc: loc, Data: defineFunc(loc, p.findSymbolHelper)}
expr := js_ast.Expr{Loc: loc, Data: defineFunc(config.DefineArgs{
Loc: loc,
FindSymbol: p.findSymbolHelper,
})}
if id, ok := expr.Data.(*js_ast.EIdentifier); ok {
return p.handleIdentifier(loc, assignTarget, isDeleteTarget, id)
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/api/api_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,8 @@ func validateDefines(log logger.Log, defines map[string]string, pureFns []string
if _, ok := js_lexer.Keywords[value]; !ok {
name := value // The closure must close over a variable inside the loop
rawDefines[key] = config.DefineData{
DefineFunc: func(loc logger.Loc, findSymbol config.FindSymbol) js_ast.E {
return &js_ast.EIdentifier{Ref: findSymbol(loc, name)}
DefineFunc: func(args config.DefineArgs) js_ast.E {
return &js_ast.EIdentifier{Ref: args.FindSymbol(args.Loc, name)}
},
}

Expand All @@ -363,13 +363,13 @@ func validateDefines(log logger.Log, defines map[string]string, pureFns []string
var fn config.DefineFunc
switch e := expr.Data.(type) {
case *js_ast.ENull:
fn = func(logger.Loc, config.FindSymbol) js_ast.E { return &js_ast.ENull{} }
fn = func(config.DefineArgs) js_ast.E { return &js_ast.ENull{} }
case *js_ast.EBoolean:
fn = func(logger.Loc, config.FindSymbol) js_ast.E { return &js_ast.EBoolean{Value: e.Value} }
fn = func(config.DefineArgs) js_ast.E { return &js_ast.EBoolean{Value: e.Value} }
case *js_ast.EString:
fn = func(logger.Loc, config.FindSymbol) js_ast.E { return &js_ast.EString{Value: e.Value} }
fn = func(config.DefineArgs) js_ast.E { return &js_ast.EString{Value: e.Value} }
case *js_ast.ENumber:
fn = func(logger.Loc, config.FindSymbol) js_ast.E { return &js_ast.ENumber{Value: e.Value} }
fn = func(config.DefineArgs) js_ast.E { return &js_ast.ENumber{Value: e.Value} }
default:
log.AddError(nil, logger.Loc{}, fmt.Sprintf("Invalid define value: %q", value))
continue
Expand Down

0 comments on commit b26c0fa

Please sign in to comment.