Skip to content

Commit

Permalink
feat: supports UnaryExpression
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Jun 18, 2022
1 parent 1954e64 commit 2f7c916
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/core/convert.ts
Expand Up @@ -40,6 +40,7 @@ import type {
Node,
ObjectExpression,
TemplateLiteral,
UnaryExpression,
} from '@babel/types'

export type EvaluatedValue =
Expand Down Expand Up @@ -236,7 +237,8 @@ function transformJsx(code: string, node: JSX) {
return resolveObjectExpression(node)
case 'BinaryExpression':
return resolveBinary(node)

case 'UnaryExpression':
return resolveUnaryExpression(node)
default:
return notSupported(node)
}
Expand Down Expand Up @@ -314,6 +316,27 @@ function transformJsx(code: string, node: JSX) {
}
}

function resolveUnaryExpression(node: UnaryExpression) {
const value: any = resolveExpression(node.argument)
switch (node.operator) {
case '!':
return !value
case '+':
return +value
case '-':
return -value
case 'typeof':
return typeof value
case 'void':
return undefined
case '~':
return ~value

default:
notSupported(node)
}
}

function resolveLiteral(node: Literal): EvaluatedValue {
switch (node.type) {
case 'TemplateLiteral':
Expand Down

0 comments on commit 2f7c916

Please sign in to comment.