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

Commit 1e2c3c7

Browse files
committed
feat: add new JSXInterpolation node
1 parent f27d2ff commit 1e2c3c7

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { NODE_TYPE, VISITOR_KEYS } from 'h2x-types'
2+
3+
class JSXInterpolation {
4+
static [NODE_TYPE] = 'JSXInterpolation';
5+
static [VISITOR_KEYS] = null
6+
7+
value = null
8+
}
9+
10+
export default JSXInterpolation

packages/h2x-plugin-jsx/src/generator.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,9 @@ export default {
4747
if (trimmedText) generator.writeLine(`{\`${formatText(path.node)}\`}`)
4848
},
4949
},
50+
JSXInterpolation: {
51+
enter(path, generator) {
52+
generator.writeLine(`{${path.node.value}}`)
53+
},
54+
},
5055
}

packages/h2x-plugin-jsx/src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export { default as JSXElement } from './JSXElement'
55
export { default as JSXAttribute } from './JSXAttribute'
66
export { default as JSXComment } from './JSXComment'
77
export { default as JSXText } from './JSXText'
8+
export { default as JSXInterpolation } from './JSXInterpolation'
89

910
export default function transformJsx() {
1011
return { visitor, generator }

packages/h2x-plugin-jsx/src/index.test.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { transform } from 'h2x-core'
2-
import transformJsx from '.'
2+
import transformJsx, { JSXInterpolation } from '.'
33

44
describe('transformJsx', () => {
55
it('should transform into jsx', () => {
@@ -108,4 +108,24 @@ describe('transformJsx', () => {
108108
'<svg autoReverse="false" externalResourcesRequired="true" focusable="true" preserveAlpha="false" />',
109109
)
110110
})
111+
112+
it('should handle interpolation', () => {
113+
const code = `<div></div>`
114+
const addInterpolation = () => ({
115+
visitor: {
116+
JSXElement: {
117+
enter(path) {
118+
const interpolation = new JSXInterpolation()
119+
interpolation.value = 'title'
120+
path.node.children.push(interpolation)
121+
},
122+
},
123+
},
124+
})
125+
expect(
126+
transform(code, { plugins: [transformJsx, addInterpolation] }).trim(),
127+
).toBe(`<div>
128+
{title}
129+
</div>`)
130+
})
111131
})

0 commit comments

Comments
 (0)