-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
325e776
commit 6b82480
Showing
4 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
130 changes: 130 additions & 0 deletions
130
...blade/codemods/migrate-motion-tokens/transformers/__tests__/migrate-motion-tokens.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,130 @@ | ||
import { applyTransform } from '@hypermod/utils'; | ||
import transformer from '..'; | ||
|
||
it('should update token for dashboard sidebar example', async () => { | ||
const result = await applyTransform( | ||
transformer, | ||
` | ||
export const SidebarContainer = styled.div<SidebarContainerProps>( | ||
({ theme, isRTUXHomepage, isVisible, isMobile }) => \` | ||
display: flex; | ||
flex-direction: column; | ||
position: fixed; | ||
top: 0; | ||
bottom: 0; | ||
background-color: \${ | ||
isRTUXHomepage | ||
? isMobile | ||
? theme.colors.surface.background.cloud.subtle | ||
: 'transparent' | ||
: '#2e3345' | ||
}; | ||
transform: translate(-100%,0); | ||
transition: transform \${makeMotionTime(theme.motion.delay.short)} \${ | ||
theme.motion.easing.standard.effective | ||
}; | ||
\`); | ||
`, | ||
{ parser: 'tsx' }, | ||
); | ||
|
||
expect(result).toMatchInlineSnapshot(` | ||
"export const SidebarContainer = styled.div<SidebarContainerProps>( | ||
({ theme, isRTUXHomepage, isVisible, isMobile }) => \` | ||
display: flex; | ||
flex-direction: column; | ||
position: fixed; | ||
top: 0; | ||
bottom: 0; | ||
background-color: \${ | ||
isRTUXHomepage | ||
? isMobile | ||
? theme.colors.surface.background.cloud.subtle | ||
: 'transparent' | ||
: '#2e3345' | ||
}; | ||
transform: translate(-100%,0); | ||
transition: transform \${makeMotionTime(theme.motion.delay.short)} \${ | ||
theme.motion.easing.standard | ||
}; | ||
\`);" | ||
`); | ||
}); | ||
|
||
it('should update token when used as prop', async () => { | ||
const result = await applyTransform( | ||
transformer, | ||
` | ||
function App() { | ||
const { theme } = useTheme(); | ||
return <Box easing={theme.motion.easing.standard.wary} /> | ||
} | ||
`, | ||
{ parser: 'tsx' }, | ||
); | ||
|
||
expect(result).toMatchInlineSnapshot(` | ||
"function App() { | ||
const { theme } = useTheme(); | ||
return <Box easing={theme.motion.easing.shake} /> | ||
}" | ||
`); | ||
}); | ||
|
||
it('should update token as used in X payroll', async () => { | ||
const result = await applyTransform( | ||
transformer, | ||
` | ||
export const AnimatedContainer = styled.div<{ delay?: number }>(({ theme, delay = 0 }) => { | ||
return css\` | ||
animation: \${entry} \${makeMotionTime(theme.motion.duration['2xgentle'])} | ||
\${theme.motion.easing.entrance.revealing} forwards; | ||
animation-delay: \${delay}ms; | ||
opacity: 0; | ||
\`; | ||
}); | ||
`, | ||
{ parser: 'tsx' }, | ||
); | ||
|
||
expect(result).toMatchInlineSnapshot(` | ||
"export const AnimatedContainer = styled.div<{ delay?: number }>(({ theme, delay = 0 }) => { | ||
return css\` | ||
animation: \${entry} \${makeMotionTime(theme.motion.duration['2xgentle'])} | ||
\${theme.motion.easing.entrance} forwards; | ||
animation-delay: \${delay}ms; | ||
opacity: 0; | ||
\`; | ||
});" | ||
`); | ||
}); | ||
|
||
it('should update token when variable name is not "theme"', async () => { | ||
const result = await applyTransform( | ||
transformer, | ||
` | ||
export const AnimatedContainer = styled.div<{ delay?: number }>(({ theme, delay = 0 }) => { | ||
return css\` | ||
animation: \${entry} \${makeMotionTime(paymentTheme.motion.duration['2xgentle'])} | ||
\${paymentTheme.motion.easing.exit.effective} forwards; | ||
animation-delay: \${delay}ms; | ||
opacity: 0; | ||
\`; | ||
}); | ||
`, | ||
{ parser: 'tsx' }, | ||
); | ||
|
||
expect(result).toMatchInlineSnapshot(` | ||
"export const AnimatedContainer = styled.div<{ delay?: number }>(({ theme, delay = 0 }) => { | ||
return css\` | ||
animation: \${entry} \${makeMotionTime(paymentTheme.motion.duration['2xgentle'])} | ||
\${paymentTheme.motion.easing.exit} forwards; | ||
animation-delay: \${delay}ms; | ||
opacity: 0; | ||
\`; | ||
});" | ||
`); | ||
}); |
1 change: 1 addition & 0 deletions
1
packages/blade/codemods/migrate-motion-tokens/transformers/index.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 @@ | ||
export { default } from './migrate-motion'; |
20 changes: 20 additions & 0 deletions
20
packages/blade/codemods/migrate-motion-tokens/transformers/migrate-motion.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,20 @@ | ||
import type { FileInfo } from 'jscodeshift'; | ||
import * as tokenMapping from './motionTokenMapping.json'; | ||
|
||
export default function transformer(file: FileInfo) { | ||
// Fairly simple usecases of motion tokens in razorpay files so this works. | ||
// .replace has also worked well during rebranding | ||
const newSource = file.source.replace(/motion\.easing\.[\w\.]+/g, (matchingToken) => { | ||
const match = Object.entries(tokenMapping).find( | ||
([oldToken, _newToken]) => oldToken === matchingToken, | ||
); | ||
|
||
if (match) { | ||
return match[1]; | ||
} | ||
|
||
return matchingToken; | ||
}); | ||
|
||
return newSource; | ||
} |
11 changes: 11 additions & 0 deletions
11
packages/blade/codemods/migrate-motion-tokens/transformers/motionTokenMapping.json
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,11 @@ | ||
{ | ||
"motion.easing.entrance.effective": "motion.easing.entrance", | ||
"motion.easing.standard.effective": "motion.easing.standard", | ||
"motion.easing.exit.effective": "motion.easing.exit", | ||
"motion.easing.entrance.revealing": "motion.easing.entrance", | ||
"motion.easing.standard.revealing": "motion.easing.emphasized", | ||
"motion.easing.exit.revealing": "motion.easing.exit", | ||
"motion.easing.entrance.attentive": "motion.easing.overshoot", | ||
"motion.easing.exit.attentive": "motion.easing.exit", | ||
"motion.easing.standard.wary": "motion.easing.shake" | ||
} |