Skip to content

Commit

Permalink
feat: support eval undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
h-a-n-a committed Feb 23, 2024
1 parent 3057057 commit 988a5e5
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ fn eval_typeof(
let mut res = BasicEvaluatedExpression::with_range(expr.span.real_lo(), expr.span.hi.0);
res.set_string("string".to_string());
Some(res)
} else if arg.is_undefined() {
let mut res = BasicEvaluatedExpression::with_range(expr.span.real_lo(), expr.span.hi.0);
res.set_string("undefined".to_string());
Some(res)
} else {
// TODO: `arg.is_wrapped()`...
None
Expand Down
7 changes: 6 additions & 1 deletion crates/rspack_plugin_javascript/src/utils/eval/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ impl BasicEvaluatedExpression {
pub fn as_bool(&self) -> Option<Boolean> {
if self.truthy {
Some(true)
} else if self.falsy || self.nullish == Some(true) || self.is_null() {
} else if self.falsy || self.nullish == Some(true) || self.is_null() || self.is_undefined() {
Some(false)
} else {
self.boolean
Expand Down Expand Up @@ -284,6 +284,11 @@ impl BasicEvaluatedExpression {
self.side_effects = false
}

pub fn set_undefined(&mut self) {
self.ty = Ty::Undefined;
self.side_effects = false;
}

pub fn set_number(&mut self, number: Number) {
self.ty = Ty::Number;
self.number = Some(number);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -758,10 +758,16 @@ impl JavascriptParser<'_> {
.or_else(|| {
let mut eval =
BasicEvaluatedExpression::with_range(ident.span.real_lo(), ident.span.hi.0);
eval.set_identifier(
ident.sym.to_string(),
ExportedVariableInfo::Name(ident.sym.to_string()),
);

if ident.sym.eq("undefined") {
eval.set_undefined();
} else {
eval.set_identifier(
ident.sym.to_string(),
ExportedVariableInfo::Name(ident.sym.to_string()),
);
}

Some(eval)
});
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "a";
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
it("should evaluate null", function () {
expect(null ? require("fail") : require("./a.js")).toBe("a");
if (null) require("fail");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
mode: "none"
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "a";
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
it("should evaluate undefined", function () {
expect(undefined ? require("fail") : require("./a")).toBe("a");
if (undefined) require("fail");
undefined && require("fail");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
mode: "none"
};

0 comments on commit 988a5e5

Please sign in to comment.