Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support JSON objects and arrays in define #582

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 1 addition & 17 deletions pkg/api/api_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,23 +359,7 @@ func validateDefines(log logger.Log, defines map[string]string, pureFns []string
continue
}

// Only allow atoms for now
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{} }
case *js_ast.EBoolean:
fn = func(logger.Loc, config.FindSymbol) 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} }
case *js_ast.ENumber:
fn = func(logger.Loc, config.FindSymbol) js_ast.E { return &js_ast.ENumber{Value: e.Value} }
default:
log.AddError(nil, logger.Loc{}, fmt.Sprintf("Invalid define value: %q", value))
continue
}

rawDefines[key] = config.DefineData{DefineFunc: fn}
rawDefines[key] = config.DefineData{DefineFunc: func(logger.Loc, config.FindSymbol) js_ast.E { return expr.Data }}
}

for _, key := range pureFns {
Expand Down
4 changes: 4 additions & 0 deletions scripts/end-to-end-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@
test(['--define:x=y', '--define:y=x', 'in.js', '--outfile=node.js'], {
'in.js': `eval('var x="x",y="y"'); if (x + y !== 'yx') throw 'fail'`,
}),
test(['--define:foo=[1, 2, 3]', 'in.js', '--outfile=node.js'], { 'in.js': `if (JSON.stringify(foo) != "[1,2,3]") throw 'fail'` }),
test(['--define:foo={"a":1, "b":2}', 'in.js', '--outfile=node.js'], {
'in.js': `if (JSON.stringify(foo) != "{\\"a\\":1,\\"b\\":2}") throw 'fail'`
}),
)

// Test recursive directory creation
Expand Down