-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat(codemod): Add codemod for Flex direction values
- Loading branch information
1 parent
9af1f88
commit ed0f6c9
Showing
5 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
...ges/codemods/src/transforms/v4/web-react/__testfixtures__/flex-direction-values.input.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import React from 'react'; | ||
// @ts-ignore: No declaration -- The library is not installed; we don't need to install it for fixtures. | ||
import { Flex } from '@lmc-eu/spirit-web-react'; | ||
|
||
export const MyComponent = () => ( | ||
<> | ||
<Flex>Flex</Flex> | ||
<Flex direction="row">Flex</Flex> | ||
<Flex direction="column">Flex</Flex> | ||
<Flex direction={{ mobile: 'row', tablet: 'column' }}>Flex</Flex> | ||
</> | ||
); |
12 changes: 12 additions & 0 deletions
12
...es/codemods/src/transforms/v4/web-react/__testfixtures__/flex-direction-values.output.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import React from 'react'; | ||
// @ts-ignore: No declaration -- The library is not installed; we don't need to install it for fixtures. | ||
import { Flex } from '@lmc-eu/spirit-web-react'; | ||
|
||
export const MyComponent = () => ( | ||
<> | ||
<Flex>Flex</Flex> | ||
<Flex direction="horizontal">Flex</Flex> | ||
<Flex direction="vertical">Flex</Flex> | ||
<Flex direction={{ mobile: 'horizontal', tablet: 'vertical' }}>Flex</Flex> | ||
</> | ||
); |
3 changes: 3 additions & 0 deletions
3
packages/codemods/src/transforms/v4/web-react/__tests__/flex-direction-values.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { testTransform } from '../../../../../tests/testUtils'; | ||
|
||
testTransform(__dirname, 'flex-direction-values'); |
66 changes: 66 additions & 0 deletions
66
packages/codemods/src/transforms/v4/web-react/flex-direction-values.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { API, FileInfo } from 'jscodeshift'; | ||
import { removeParentheses } from '../../../helpers'; | ||
|
||
const transform = (fileInfo: FileInfo, api: API) => { | ||
const j = api.jscodeshift; | ||
const root = j(fileInfo.source); | ||
|
||
// Define the mapping from old direction values to new ones | ||
const directionMap: { [key: string]: string } = { | ||
row: 'horizontal', | ||
column: 'vertical', | ||
}; | ||
|
||
// Find all Flex components | ||
root | ||
.find(j.JSXOpeningElement, { name: { type: 'JSXIdentifier', name: 'Flex' } }) | ||
.find(j.JSXAttribute, { name: { type: 'JSXIdentifier', name: 'direction' } }) | ||
.forEach((attributePath) => { | ||
const attribute = attributePath.node; | ||
|
||
if (attribute.value) { | ||
// Handle string literal values | ||
if (attribute.value.type === 'StringLiteral') { | ||
const newValue = directionMap[attribute.value.value]; | ||
if (newValue) { | ||
attribute.value = j.stringLiteral(newValue); | ||
} | ||
} | ||
|
||
// Handle object values | ||
else if ( | ||
attribute.value.type === 'JSXExpressionContainer' && | ||
attribute.value.expression.type === 'ObjectExpression' | ||
) { | ||
const objectExpression = attribute.value.expression; | ||
|
||
objectExpression.properties.forEach((property) => { | ||
if ( | ||
property.type === 'ObjectProperty' && | ||
(property.key.type === 'Identifier' || property.key.type === 'Literal') && | ||
property.value.type === 'StringLiteral' | ||
) { | ||
const oldValue = property.value.value; | ||
|
||
// Update the value if it matches a key in directionMap | ||
if (oldValue in directionMap) { | ||
// Manually create a single-quoted Literal | ||
property.value = j.literal(directionMap[oldValue]); | ||
// We need single quotes in object values | ||
// @ts-expect-error extra is not defined on Literal | ||
property.value.extra = { | ||
raw: `'${directionMap[oldValue]}'`, | ||
rawValue: directionMap[oldValue], | ||
}; | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
}); | ||
|
||
// Convert the transformed code to source with double quotes for strings | ||
return removeParentheses(root.toSource({ quote: 'double' })); | ||
}; | ||
|
||
export default transform; |