diff --git a/packages/core/src/__snapshots__/convert.test.js.snap b/packages/core/src/__snapshots__/convert.test.js.snap index 62a7eea6..f2166fac 100644 --- a/packages/core/src/__snapshots__/convert.test.js.snap +++ b/packages/core/src/__snapshots__/convert.test.js.snap @@ -21,6 +21,27 @@ export default SvgComponent " `; +exports[`convert config should support options { expandProps: 'start' } 1`] = ` +"import React from 'react' + +const SvgComponent = props => ( + + + + + +) + +export default SvgComponent +" +`; + exports[`convert config should support options { expandProps: false } 1`] = ` "import React from 'react' diff --git a/packages/core/src/convert.test.js b/packages/core/src/convert.test.js index a1035cee..a69b2656 100644 --- a/packages/core/src/convert.test.js +++ b/packages/core/src/convert.test.js @@ -212,6 +212,7 @@ describe('convert', () => { const configs = [ [{ dimensions: false }], [{ expandProps: false }], + [{ expandProps: 'start' }], [{ icon: true }], [{ native: true }], [{ native: true, icon: true }], diff --git a/packages/core/src/h2x/__snapshots__/expandProps.test.js.snap b/packages/core/src/h2x/__snapshots__/expandProps.test.js.snap index e0ef6553..1ef0f9e2 100644 --- a/packages/core/src/h2x/__snapshots__/expandProps.test.js.snap +++ b/packages/core/src/h2x/__snapshots__/expandProps.test.js.snap @@ -1,7 +1,21 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`expandProps should expand props 1`] = ` -" +exports[`expandProps should expand props at the start with option 1`] = ` +" + + +" +`; + +exports[`expandProps should expand props in the end by default 1`] = ` +" + + +" +`; + +exports[`expandProps should expand props in the end with option 1`] = ` +" " diff --git a/packages/core/src/h2x/expandProps.js b/packages/core/src/h2x/expandProps.js index 94a531e8..085f783b 100644 --- a/packages/core/src/h2x/expandProps.js +++ b/packages/core/src/h2x/expandProps.js @@ -1,6 +1,6 @@ import { JSXAttribute } from 'h2x-plugin-jsx' -const expandProps = () => () => ({ +const expandProps = (place = 'end') => () => ({ visitor: { JSXElement: { enter(path) { @@ -11,7 +11,12 @@ const expandProps = () => () => ({ const props = new JSXAttribute() props.name = 'props' props.spread = true - path.node.attributes.push(props) + if (place === 'start') { + path.node.attributes.unshift(props) + } + if (place === 'end') { + path.node.attributes.push(props) + } path.replace(path.node) } }, diff --git a/packages/core/src/h2x/expandProps.test.js b/packages/core/src/h2x/expandProps.test.js index 571adee0..930d69e6 100644 --- a/packages/core/src/h2x/expandProps.test.js +++ b/packages/core/src/h2x/expandProps.test.js @@ -3,9 +3,9 @@ import { transform } from 'h2x-core' import expandProps from './expandProps' describe('expandProps', () => { - it('should expand props', () => { + it('should expand props in the end by default', () => { const result = transform( - ` + ` `, { plugins: [jsx, expandProps()] }, @@ -13,4 +13,26 @@ describe('expandProps', () => { expect(result).toMatchSnapshot() }) + + it('should expand props in the end with option', () => { + const result = transform( + ` + + `, + { plugins: [jsx, expandProps('end')] }, + ) + + expect(result).toMatchSnapshot() + }) + + it('should expand props at the start with option', () => { + const result = transform( + ` + + `, + { plugins: [jsx, expandProps('start')] }, + ) + + expect(result).toMatchSnapshot() + }) }) diff --git a/packages/core/src/plugins/h2x.js b/packages/core/src/plugins/h2x.js index b87c352e..1a9f618d 100644 --- a/packages/core/src/plugins/h2x.js +++ b/packages/core/src/plugins/h2x.js @@ -27,7 +27,11 @@ function configToPlugins(config) { if (config.icon) plugins.push(emSize()) if (config.ref) plugins.push(svgRef()) if (config.svgAttributes) plugins.push(svgAttributes(config.svgAttributes)) - if (config.expandProps) plugins.push(expandProps()) + // TODO remove boolean value in the next major release + if (config.expandProps) + plugins.push( + expandProps(config.expandProps === true ? 'end' : config.expandProps), + ) if (config.native) plugins.push(toReactNative()) if (config.titleProp) plugins.push(titleProp()) return plugins