Skip to content

Commit

Permalink
fix: #119
Browse files Browse the repository at this point in the history
  • Loading branch information
meixg committed Aug 30, 2021
1 parent b8ba4c2 commit abf73e3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/compilers/san-expr-compiler.ts
Expand Up @@ -57,6 +57,7 @@ const binaryOp = {
function unary (e: ExprUnaryNode) {
if (e.operator === 33) return new UnaryExpression('!', sanExpr(e.expr))
if (e.operator === 45) return new UnaryExpression('-', sanExpr(e.expr))
if (e.operator === 43) return new UnaryExpression('+', sanExpr(e.expr))
throw new Error(`unexpected unary operator "${String.fromCharCode(e.operator)}"`)
}
function binary (e: ExprBinaryNode) {
Expand Down
22 changes: 22 additions & 0 deletions test/unit/compilers/san-expr-compiler.spec.ts
Expand Up @@ -20,6 +20,28 @@ describe('compilers/san-expr-compiler', () => {
rhs: dataItem('b')
}))
})
it('should compile unary expression', () => {
const e = parseExpr('+num === 123')
expect(expr(e)).toMatchObject({
kind: SyntaxKind.BinaryExpression,
lhs: {
kind: SyntaxKind.UnaryExpression,
op: '+',
value: {
kind: SyntaxKind.BinaryExpression,
rhs: {
kind: SyntaxKind.Literal,
value: 'num'
}
}
},
op: '===',
rhs: {
kind: SyntaxKind.Literal,
value: 123
}
})
})
it('should throw for unexpected expression type', () => {
const e = parseExpr('!b')
e.type = 222
Expand Down

0 comments on commit abf73e3

Please sign in to comment.