Skip to content

Commit ac09610

Browse files
committed
visitor(calculate-constant-exp): support more unary conditions.
Signed-off-by: echo094 <[email protected]>
1 parent b103980 commit ac09610

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/visitor/calculate-constant-exp.js

+15
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ function calculateBinaryExpression(path) {
5050
* - the operator is `!` and the argument is ArrayExpression or Literal.
5151
* - the operator is `-` and the argument is a negative number
5252
* - the operator is `+`, or `~`, and the argument is a number
53+
* - the operator is 'void' and the argument is Literal.
54+
* - the operator is 'typeof' and the argument is Literal.
5355
*
5456
* Otherwise, the expression can't be simplified.
5557
* For example, `typeof window` can be calculated but it's not constant.
@@ -84,6 +86,19 @@ function calculateUnaryExpression(path) {
8486
}
8587
return
8688
}
89+
if (node0.operator === 'void') {
90+
if (isLiteral) {
91+
path.replaceWith(t.identifier('undefined'))
92+
}
93+
return
94+
}
95+
if (node0.operator === 'typeof') {
96+
if (isLiteral) {
97+
const code = generator(node0).code
98+
path.replaceWith(t.stringLiteral(eval(code)))
99+
}
100+
return
101+
}
87102
}
88103

89104
module.exports = {

0 commit comments

Comments
 (0)