Skip to content

Commit

Permalink
feat: support BinaryExpression
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Jun 18, 2022
1 parent c139052 commit fac1e3f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
11 changes: 8 additions & 3 deletions src/core/convert.ts
Expand Up @@ -31,6 +31,7 @@ import type {
JSXText,
Literal,
Node,
PrivateName,
TemplateLiteral,
} from '@babel/types'

Expand Down Expand Up @@ -214,7 +215,9 @@ export const convert = (code: string, debug?: boolean) => {
return value
}

function expressionToString(node: Expression | JSXEmptyExpression) {
function expressionToString(
node: Expression | JSXEmptyExpression | PrivateName
): string {
if (isLiteral(node)) {
return literalToString(node)
} else if (isJSX(node)) {
Expand All @@ -223,9 +226,11 @@ export const convert = (code: string, debug?: boolean) => {

switch (node.type) {
case 'ArrayExpression':
case 'ObjectExpression': {
case 'ObjectExpression':
return normalizeObjectString(getSource(node))
}

case 'BinaryExpression':
return expressionToString(node.left) + expressionToString(node.right)

default:
return notSupported(node)
Expand Down
14 changes: 9 additions & 5 deletions tests/convert.test.tsx
Expand Up @@ -137,6 +137,15 @@ describe('convert', () => {
)
})

test('BinaryExpression', () => {
// eslint-disable-next-line prefer-template
expect(jsxToString(<>{`a` + 1 + 'b'}</>)).toMatchInlineSnapshot('"a1b"')
// eslint-disable-next-line prefer-template
expect(jsxToString(<>{`a` + true + false + 'b'}</>)).toMatchInlineSnapshot(
'"ab"'
)
})

describe('error', () => {
test('without function', () => {
expect(() => <div />).toThrowError()
Expand All @@ -160,11 +169,6 @@ describe('convert', () => {
).toThrowErrorMatchingInlineSnapshot(
'"Error: not supported Identifier: n"'
)
expect(() =>
jsxToString(<div id={'a' + 'b'} />)
).toThrowErrorMatchingInlineSnapshot(
"\"Error: not supported BinaryExpression: 'a' + 'b'\""
)
expect(() =>
jsxToString(<div tabIndex={n++} />)
).toThrowErrorMatchingInlineSnapshot(
Expand Down

0 comments on commit fac1e3f

Please sign in to comment.