Skip to content

Commit

Permalink
visitor/calculate-binary: check if left and right are both constants …
Browse files Browse the repository at this point in the history
…before replacing

Signed-off-by: echo094 <[email protected]>
  • Loading branch information
echo094 committed Oct 16, 2023
1 parent 4c7d411 commit c6b7283
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/visitor/calculate-binary.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
const generator = require('@babel/generator').default
const t = require('@babel/types')

/**
* Calculate BinaryExpression if left and right are both literals.
* Otherwise, the expression can't be simplified.
*
* For example, `typeof window` can be calculated but it's not constant.
*/
module.exports = {
BinaryExpression(path) {
const { left, right } = path.node
if (!t.isLiteral(left) || !t.isLiteral(right)) {
return
}
const code = generator(path.node).code
try {
const ret = eval(code)
Expand Down

0 comments on commit c6b7283

Please sign in to comment.