Skip to content

Commit c02fc90

Browse files
authored
Merge pull request #691 from rofrischmann/fix/codemods
Fix FelaComponent codemod bugs
2 parents b26e8f3 + f5a8f2c commit c02fc90

2 files changed

Lines changed: 65 additions & 30 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ If you're searching for older logs, please check the [old changelog](https://git
55

66
## 10.0
77

8+
### 10.2.3
9+
| Package | Changes |
10+
| --- | --- |
11+
| fela-codemods | ([#691](https://github.com/rofrischmann/fela/pull/691)) Fix `render` -> `as` conversion edge cases. Don't convert args of callbacks _inside_ an inline `style` function, only the args to the `style` callback itself. |
12+
13+
814
### 10.2.2
915
| Package | Changes |
1016
| --- | --- |

packages/fela-codemods/src/v10/FelaComponent.js

Lines changed: 59 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,28 @@ export default function transformer(file, api) {
1414
.find(j.JSXElement)
1515
.forEach(path => {
1616
if (path.node.openingElement.name.name === importName) {
17-
const styleProp = path.node.openingElement.attributes.find(
17+
const openingElement = path.node.openingElement
18+
19+
const styleProp = openingElement.attributes.find(
1820
prop => prop.name.name === 'style'
1921
)
20-
21-
const ruleProp = path.node.openingElement.attributes.find(
22+
const ruleProp = openingElement.attributes.find(
2223
prop => prop.name.name === 'rule'
2324
)
24-
25-
const renderProp = path.node.openingElement.attributes.find(
25+
const renderProp = openingElement.attributes.find(
2626
prop => prop.name.name === 'render'
2727
)
2828

2929
// handle render/as transformation to children/as
3030
if (renderProp) {
31-
if (
32-
renderProp.value.type === 'Literal' ||
33-
(renderProp.value.type === 'JSXExpressionContainer' &&
34-
renderProp.value.expression.type === 'Literal')
35-
) {
36-
renderProp.name.name = 'as'
37-
} else {
31+
const hasChildren =
32+
path.node.children.length > 0 ||
33+
!!openingElement.attributes.find(
34+
prop => prop.name.name === 'children'
35+
)
36+
37+
if (hasChildren) renderProp.name.name = 'as'
38+
else {
3839
j(path).replaceWith(
3940
j.jsxElement(
4041
j.jsxOpeningElement(
@@ -54,27 +55,55 @@ export default function transformer(file, api) {
5455

5556
// replace inline style expression from theme
5657
if (styleProp) {
57-
j(styleProp)
58-
.find(j.ArrowFunctionExpression)
59-
.forEach(stylePath => {
60-
const param = stylePath.node.params[0]
58+
const { value } = styleProp
59+
if (value && value.expression.type === 'Identifier') {
60+
console.warn(
61+
`The style value in line ${
62+
value.expression.loc.start.line
63+
} is a reference to an external variable. ` +
64+
'We have no way to check and convert it to the new syntax. Please do so manually'
65+
)
66+
}
67+
const styleIsArrowFn =
68+
value && value.expression.type === 'ArrowFunctionExpression'
69+
const styleIsFn =
70+
value && value.expression.type === 'FunctionExpression'
71+
const styleTransformer = (stylePath, i) => {
72+
const param = stylePath.node.params[0]
6173

62-
if (param.type === 'Identifier') {
63-
j(stylePath).replaceWith(
64-
j.arrowFunctionExpression(
65-
[
66-
j.objectPattern([
67-
j.objectProperty(param, param, false, true),
68-
]),
69-
],
70-
stylePath.node.body
71-
)
74+
if (param.type === 'Identifier' && i === 0) {
75+
j(stylePath).replaceWith(
76+
j.arrowFunctionExpression(
77+
[
78+
j.objectPattern([
79+
j.objectProperty(param, param, false, true),
80+
]),
81+
],
82+
stylePath.node.body
7283
)
84+
)
85+
86+
// make property truly shorthand
87+
stylePath.node.params[0].properties[0].shorthand = true
88+
}
89+
}
90+
if (styleIsArrowFn) {
91+
j(styleProp)
92+
.find(j.ArrowFunctionExpression)
93+
.forEach(styleTransformer)
94+
}
7395

74-
// make property truly shorthand
75-
stylePath.node.params[0].properties[0].shorthand = true
76-
}
77-
})
96+
if (styleIsFn) {
97+
// TODO: Don't convert to arrow function and remove the warning
98+
console.warn(
99+
`Converting a function expression style function into an arrow function in line ${
100+
value.expression.loc.start.line
101+
}`
102+
)
103+
j(styleProp)
104+
.find(j.FunctionExpression)
105+
.forEach(styleTransformer)
106+
}
78107
}
79108

80109
// rename rule to style

0 commit comments

Comments
 (0)