Skip to content

Commit bb6a3a8

Browse files
committed
[core][rewriter] fix behavior of unary rewrites
1 parent 7953311 commit bb6a3a8

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

rewriter/js/src/visitor.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,8 +382,17 @@ where
382382

383383
fn visit_unary_expression(&mut self, it: &UnaryExpression<'data>) {
384384
if matches!(it.operator, UnaryOperator::Typeof) {
385-
// don't walk to identifier rewrites since it won't matter
386-
return;
385+
match it.argument {
386+
Expression::Identifier(_) =>{
387+
// `typeof location` -> `typeof $wrap(location)` seems like a sane rewrite but it's incorrect
388+
// typeof has the special property of not caring whether the identifier is undefined
389+
return;
390+
}
391+
_=>{
392+
// `typeof (location)` / `typeof location.href` / `typeof function()`
393+
// this is safe to rewrite
394+
}
395+
}
387396
}
388397
walk::walk_unary_expression(self, it);
389398
}
@@ -517,5 +526,6 @@ where
517526
}
518527
_ => {}
519528
}
529+
walk::walk_expression(self, &it.right);
520530
}
521531
}

0 commit comments

Comments
 (0)