Skip to content

Commit 3b75c5d

Browse files
committed
feat: convert jsx attributes and object properties in replacements
1 parent 6738384 commit 3b75c5d

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Node } from '../types'
2+
import * as t from '@babel/types'
3+
import convertToJSXIdentifierNameExpressionPair from './convertToJSXIdentifierNameExpressionPair'
4+
import { ReplacementConverter } from '.'
5+
6+
export default function convertJSXAttributeReplacement(): ReplacementConverter {
7+
const convert = (node: Node): Node | Node[] => {
8+
const converted = convertToJSXIdentifierNameExpressionPair(node)
9+
if (converted) {
10+
const [name, value] = converted
11+
return t.jsxAttribute(
12+
t.jsxIdentifier(name),
13+
t.jsxExpressionContainer(value as any)
14+
)
15+
}
16+
return node
17+
}
18+
return convert
19+
}

src/convertReplacement/convertToJSXIdentifierNameExpressionPair.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ export default function convertToJSXIdentifierNameExpressionPair(
2323
}
2424
case 'JSXAttribute': {
2525
const key = convertToJSXIdentifierName(node.name)
26-
const value = convertToExpression(node.value || node.name)
26+
const value = node.value
27+
? convertToExpression(node.value)
28+
: t.booleanLiteral(true)
2729
if (key && value) return [key, value]
2830
break
2931
}

src/convertReplacement/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Node, NodePath } from '../types'
22
import convertJSXChildReplacement from './convertJSXChildReplacement'
3+
import convertJSXAttributeReplacement from './convertJSXAttributeReplacement'
34
import convertJSXAttributeValueReplacement from './convertJSXAttributeValueReplacement'
45
import TypeParameter from './convertTypeParameterReplacement'
56
import TSTypeParameter from './convertTSTypeParameterReplacement'
@@ -25,6 +26,7 @@ const nodeConverters: Record<string, (path: NodePath) => ReplacementConverter> =
2526
TSTypeParameter,
2627
ImportSpecifier: convertImportSpecifierReplacement,
2728
ImportDefaultSpecifier: convertImportSpecifierReplacement,
29+
JSXAttribute: convertJSXAttributeReplacement,
2830
}
2931

3032
export default function createReplacementConverter(
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { findReplaceTestcase } from '../findReplaceTestcase'
2+
import dedent from 'dedent-js'
3+
4+
findReplaceTestcase({
5+
file: __filename,
6+
input: dedent`
7+
expect(
8+
new ReduxFormMaterialUICheckbox({
9+
input: {
10+
name: 'myCheckbox',
11+
onChange: noop,
12+
value: true,
13+
},
14+
}).render()
15+
).toEqualJSX(<Checkbox name="myCheckbox" checked onChange={noop} />)
16+
`,
17+
find: dedent`
18+
expect(new $X({$$props}).render()).toEqualJSX(<$C $$props2 />)
19+
`,
20+
replace: dedent`
21+
expect(create(<$X $$props />).root.findByType($C).props).to.containSubset({ $$props2 })
22+
`,
23+
expectedReplace: dedent`
24+
expect(
25+
create(
26+
<ReduxFormMaterialUICheckbox
27+
input={{
28+
name: 'myCheckbox',
29+
onChange: noop,
30+
value: true,
31+
}}
32+
/>
33+
).root.findByType(Checkbox).props
34+
).to.containSubset({
35+
name: "myCheckbox",
36+
checked: true,
37+
onChange: noop,
38+
})
39+
`,
40+
})

0 commit comments

Comments
 (0)