Skip to content
This repository has been archived by the owner on Aug 24, 2019. It is now read-only.

Commit

Permalink
feat: add new JSXInterpolation node
Browse files Browse the repository at this point in the history
  • Loading branch information
gregberge committed Oct 5, 2018
1 parent f27d2ff commit 1e2c3c7
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
10 changes: 10 additions & 0 deletions packages/h2x-plugin-jsx/src/JSXInterpolation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { NODE_TYPE, VISITOR_KEYS } from 'h2x-types'

class JSXInterpolation {
static [NODE_TYPE] = 'JSXInterpolation';
static [VISITOR_KEYS] = null

value = null
}

export default JSXInterpolation
5 changes: 5 additions & 0 deletions packages/h2x-plugin-jsx/src/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,9 @@ export default {
if (trimmedText) generator.writeLine(`{\`${formatText(path.node)}\`}`)
},
},
JSXInterpolation: {
enter(path, generator) {
generator.writeLine(`{${path.node.value}}`)
},
},
}
1 change: 1 addition & 0 deletions packages/h2x-plugin-jsx/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export { default as JSXElement } from './JSXElement'
export { default as JSXAttribute } from './JSXAttribute'
export { default as JSXComment } from './JSXComment'
export { default as JSXText } from './JSXText'
export { default as JSXInterpolation } from './JSXInterpolation'

export default function transformJsx() {
return { visitor, generator }
Expand Down
22 changes: 21 additions & 1 deletion packages/h2x-plugin-jsx/src/index.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { transform } from 'h2x-core'
import transformJsx from '.'
import transformJsx, { JSXInterpolation } from '.'

describe('transformJsx', () => {
it('should transform into jsx', () => {
Expand Down Expand Up @@ -108,4 +108,24 @@ describe('transformJsx', () => {
'<svg autoReverse="false" externalResourcesRequired="true" focusable="true" preserveAlpha="false" />',
)
})

it('should handle interpolation', () => {
const code = `<div></div>`
const addInterpolation = () => ({
visitor: {
JSXElement: {
enter(path) {
const interpolation = new JSXInterpolation()
interpolation.value = 'title'
path.node.children.push(interpolation)
},
},
},
})
expect(
transform(code, { plugins: [transformJsx, addInterpolation] }).trim(),
).toBe(`<div>
{title}
</div>`)
})
})

0 comments on commit 1e2c3c7

Please sign in to comment.