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

fix: define stringified object value will cause panic #1349

Merged
merged 3 commits into from
Jul 3, 2024
Merged
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
25 changes: 24 additions & 1 deletion crates/mako/src/visitors/env_replacer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::HashMap;
use std::str::FromStr;
use std::sync::Arc;

use anyhow::{anyhow, Result};
Expand Down Expand Up @@ -190,8 +191,15 @@ pub fn build_env_map(
fn get_env_expr(v: Value, context: &Arc<Context>) -> Result<Expr> {
match v {
Value::String(v) => {
let safe_value = if Value::from_str(&v).map_or(false, |t| t.is_object()) {
format!("({})", v)
} else {
v.clone()
};

// the string content is treat as expression, so it has to be parsed
let ast = JsAst::build("_mako_internal/_define_.js", &v, context.clone()).unwrap();
let ast =
JsAst::build("_mako_internal/_define_.js", &safe_value, context.clone()).unwrap();
xusd320 marked this conversation as resolved.
Show resolved Hide resolved
let module = ast.ast.body.first().unwrap();

match module {
Expand Down Expand Up @@ -361,6 +369,21 @@ log([
);
}

#[test]
fn test_stringified_env() {
assert_eq!(
run(
r#"log(A)"#,
hashmap! {
"A".to_string() => json!("{\"v\": 1}")
}
),
r#"log(({
"v": 1
}));"#
);
}

fn run(js_code: &str, envs: HashMap<String, Value>) -> String {
let mut test_utils = TestUtils::gen_js_ast(js_code);
let envs = build_env_map(envs, &test_utils.context).unwrap();
Expand Down
19 changes: 5 additions & 14 deletions e2e/fixtures/config.define/expect.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
const assert = require("assert");
const { parseBuildResult, moduleReg } = require("../../../scripts/test-utils");
const { files } = parseBuildResult(__dirname);
const { parseBuildResult, injectSimpleJest } = require("../../../scripts/test-utils");
const { distDir } = parseBuildResult(__dirname);

injectSimpleJest()
require(path.join(distDir, 'index.js'));

let content = files["index.js"];
content = content.replace(/\s/g, "");

assert(content.includes("\"development\";"), "support process.env.NODE_ENV");
assert(content.includes("\"aaa\""), "support String");
assert(content.includes("value:\"bbb\"") && content.includes("ccc:{"), "support Object");
assert(content.includes("[\"a\",1]"), "support Array");
assert(content.includes("console.log(1);"), "support Number");
assert(content.includes("console.log(true);"), "support Boolean");
assert(content.includes("console.log(false);"), "support Boolean");
assert(content.includes("console.log(null);"), "support Null");
assert(content.includes("console.log(2);"), "support expression");
24 changes: 16 additions & 8 deletions e2e/fixtures/config.define/mako.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,27 @@
"minify": false,
"define": {
"AAA": "\"aaa\"",
"BBB": {
"BBB": 1,
"CCC": true,
"DDD": false,
"EEE": null,
"FFF": [
"\"a\"",
1
],
"GGG": "1+1",
"HHH": {
"value": "\"bbb\"",
"ccc": {
"d": 1,
"e": "\"2\"",
"c": [1, "\"2\"", true]
"c": [
1,
"\"2\"",
true
]
}
},
"CCC": 1,
"DDD": true,
"EEE": false,
"FFF": null,
"GGG": ["\"a\"", 1],
"HHH": "1+1"
"III": "{\"v\": 1}"
}
}
48 changes: 40 additions & 8 deletions e2e/fixtures/config.define/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,41 @@
process.env.NODE_ENV;
console.log(AAA);
console.log(BBB);
console.log(CCC);
console.log(DDD);
console.log(EEE);
console.log(FFF);
console.log(GGG);
console.log(HHH);

it("defined string value should be right", () => {
expect(AAA).toEqual("aaa")
});
it("defined number value should be right", () => {
expect(BBB).toEqual(1)
});
it("defined boolean true value should be right", () => {
expect(CCC).toEqual(true)
});
it("defined boolean false value should be right", () => {
expect(DDD).toEqual(false)
});
xusd320 marked this conversation as resolved.
Show resolved Hide resolved
it("defined null value should be right", () => {
expect(EEE).toEqual(null)
});
it("defined array value should be right", () => {
expect(FFF).toEqual(["a", 1])
});
it("defined caculcation value should be right", () => {
expect(GGG).toEqual(2)
});
it("defined complex object value should be right", () => {
expect(HHH).toEqual({
"value": "bbb",
"ccc": {
"d": 1,
"e": "2",
"c": [
1,
"2",
true
]
}
})
});
it("defined stringified object value should be right", () => {
expect(III).toEqual({ v: 1 })
});

Loading