Skip to content

Commit cd659dc

Browse files
TrySoundgregberge
authored andcommitted
feat: allow to spread props at the start (#166)
The use case is flow which warns about inexact spread. My template looks like this and I need to spread props before any other prop. ```js const template = (code, options, state) => ` // @flow // Generated from ${state.filePath} import * as React from "react"; type Props = { size?: string | number, fill?: string }; const style = { display: "block", flex: "0 0 auto", }; export const ${state.componentName} = ({ size, fill, ...props }: Props) => { return ( ${code} ); }; `; ```
1 parent 62847ec commit cd659dc

6 files changed

Lines changed: 74 additions & 7 deletions

File tree

packages/core/src/__snapshots__/convert.test.js.snap

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,27 @@ export default SvgComponent
2121
"
2222
`;
2323

24+
exports[`convert config should support options { expandProps: 'start' } 1`] = `
25+
"import React from 'react'
26+
27+
const SvgComponent = props => (
28+
<svg {...props} width={88} height={88}>
29+
<g
30+
stroke=\\"#063855\\"
31+
strokeWidth={2}
32+
fill=\\"none\\"
33+
fillRule=\\"evenodd\\"
34+
strokeLinecap=\\"square\\"
35+
>
36+
<path d=\\"M51 37L37 51M51 51L37 37\\" />
37+
</g>
38+
</svg>
39+
)
40+
41+
export default SvgComponent
42+
"
43+
`;
44+
2445
exports[`convert config should support options { expandProps: false } 1`] = `
2546
"import React from 'react'
2647

packages/core/src/convert.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ describe('convert', () => {
212212
const configs = [
213213
[{ dimensions: false }],
214214
[{ expandProps: false }],
215+
[{ expandProps: 'start' }],
215216
[{ icon: true }],
216217
[{ native: true }],
217218
[{ native: true, icon: true }],

packages/core/src/h2x/__snapshots__/expandProps.test.js.snap

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`expandProps should expand props 1`] = `
4-
"<svg {...props}>
3+
exports[`expandProps should expand props at the start with option 1`] = `
4+
"<svg {...props} width={10} height={10}>
5+
<g stroke=\\"#063855\\" strokeWidth={2} />
6+
</svg>
7+
"
8+
`;
9+
10+
exports[`expandProps should expand props in the end by default 1`] = `
11+
"<svg width={10} height={10} {...props}>
12+
<g stroke=\\"#063855\\" strokeWidth={2} />
13+
</svg>
14+
"
15+
`;
16+
17+
exports[`expandProps should expand props in the end with option 1`] = `
18+
"<svg width={10} height={10} {...props}>
519
<g stroke=\\"#063855\\" strokeWidth={2} />
620
</svg>
721
"

packages/core/src/h2x/expandProps.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { JSXAttribute } from 'h2x-plugin-jsx'
22

3-
const expandProps = () => () => ({
3+
const expandProps = (place = 'end') => () => ({
44
visitor: {
55
JSXElement: {
66
enter(path) {
@@ -11,7 +11,12 @@ const expandProps = () => () => ({
1111
const props = new JSXAttribute()
1212
props.name = 'props'
1313
props.spread = true
14-
path.node.attributes.push(props)
14+
if (place === 'start') {
15+
path.node.attributes.unshift(props)
16+
}
17+
if (place === 'end') {
18+
path.node.attributes.push(props)
19+
}
1520
path.replace(path.node)
1621
}
1722
},

packages/core/src/h2x/expandProps.test.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,36 @@ import { transform } from 'h2x-core'
33
import expandProps from './expandProps'
44

55
describe('expandProps', () => {
6-
it('should expand props', () => {
6+
it('should expand props in the end by default', () => {
77
const result = transform(
8-
`<svg>
8+
`<svg width="10" height="10">
99
<g stroke="#063855" stroke-width="2" />
1010
</svg>`,
1111
{ plugins: [jsx, expandProps()] },
1212
)
1313

1414
expect(result).toMatchSnapshot()
1515
})
16+
17+
it('should expand props in the end with option', () => {
18+
const result = transform(
19+
`<svg width="10" height="10">
20+
<g stroke="#063855" stroke-width="2" />
21+
</svg>`,
22+
{ plugins: [jsx, expandProps('end')] },
23+
)
24+
25+
expect(result).toMatchSnapshot()
26+
})
27+
28+
it('should expand props at the start with option', () => {
29+
const result = transform(
30+
`<svg width="10" height="10">
31+
<g stroke="#063855" stroke-width="2" />
32+
</svg>`,
33+
{ plugins: [jsx, expandProps('start')] },
34+
)
35+
36+
expect(result).toMatchSnapshot()
37+
})
1638
})

packages/core/src/plugins/h2x.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ function configToPlugins(config) {
2727
if (config.icon) plugins.push(emSize())
2828
if (config.ref) plugins.push(svgRef())
2929
if (config.svgAttributes) plugins.push(svgAttributes(config.svgAttributes))
30-
if (config.expandProps) plugins.push(expandProps())
30+
// TODO remove boolean value in the next major release
31+
if (config.expandProps)
32+
plugins.push(
33+
expandProps(config.expandProps === true ? 'end' : config.expandProps),
34+
)
3135
if (config.native) plugins.push(toReactNative())
3236
if (config.titleProp) plugins.push(titleProp())
3337
return plugins

0 commit comments

Comments
 (0)