Skip to content

Commit

Permalink
visitor(calculate-constant-exp): support more unary conditions.
Browse files Browse the repository at this point in the history
Signed-off-by: echo094 <[email protected]>
  • Loading branch information
echo094 committed Sep 22, 2024
1 parent b103980 commit ac09610
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/visitor/calculate-constant-exp.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ function calculateBinaryExpression(path) {
* - the operator is `!` and the argument is ArrayExpression or Literal.
* - the operator is `-` and the argument is a negative number
* - the operator is `+`, or `~`, and the argument is a number
* - the operator is 'void' and the argument is Literal.
* - the operator is 'typeof' and the argument is Literal.
*
* Otherwise, the expression can't be simplified.
* For example, `typeof window` can be calculated but it's not constant.
Expand Down Expand Up @@ -84,6 +86,19 @@ function calculateUnaryExpression(path) {
}
return
}
if (node0.operator === 'void') {
if (isLiteral) {
path.replaceWith(t.identifier('undefined'))
}
return
}
if (node0.operator === 'typeof') {
if (isLiteral) {
const code = generator(node0).code
path.replaceWith(t.stringLiteral(eval(code)))
}
return
}
}

module.exports = {
Expand Down

0 comments on commit ac09610

Please sign in to comment.