-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1468 from cozy/codemod-refactor
Codemod to transform withBreakpoints into useBreakpoints
- Loading branch information
Showing
8 changed files
with
482 additions
and
398 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,16 @@ | ||
import React from 'react' | ||
import { useBreakpoints } from 'cozy-ui/transpiled/react' | ||
|
||
const MobileComponent = () => <>Mobile</> | ||
const DesktopComponent = () => <>Desktop</> | ||
|
||
const Component = () => { | ||
const { isMobile, isDesktop } = useBreakpoints() | ||
return isMobile ? ( | ||
<MobileComponent /> | ||
) : isDesktop ? ( | ||
<DesktopComponent /> | ||
) : null | ||
} | ||
|
||
export default otherHoc(Component) |
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,19 @@ | ||
import React from 'react' | ||
import compose from 'lodash/flowRight' | ||
import { withBreakpoints } from 'cozy-ui/transpiled/react' | ||
|
||
const MobileComponent = () => <>Mobile</> | ||
const DesktopComponent = () => <>Desktop</> | ||
|
||
const Component = ({ breakpoints: { isMobile, isDesktop } }) => { | ||
return isMobile ? ( | ||
<MobileComponent /> | ||
) : isDesktop ? ( | ||
<DesktopComponent /> | ||
) : null | ||
} | ||
|
||
export default compose( | ||
withBreakpoints(), | ||
otherHoc | ||
)(Component) |
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,49 @@ | ||
import { hocReplacer } from '@cozy/codemods' | ||
|
||
const isBreakpointProp = prop => { | ||
return prop.key && prop.key.name === 'breakpoints' | ||
} | ||
|
||
const findBreakpointsProp = objPattern => { | ||
if (!objPattern) { | ||
return | ||
} | ||
if (objPattern.type !== 'ObjectPattern') { | ||
return | ||
} | ||
|
||
return objPattern.properties | ||
? objPattern.properties.filter(isBreakpointProp).map(prop => prop) | ||
: [] | ||
} | ||
|
||
export default function transformer(file, api) { | ||
const j = api.jscodeshift | ||
const root = j(file.source) | ||
|
||
const replaceBreakpointsHOC = hocReplacer({ | ||
propsFilter: isBreakpointProp, | ||
propsFinder: findBreakpointsProp, | ||
hookUsage: foundProps => { | ||
return `const ${foundProps | ||
.map(p => j(p.value).toSource()) | ||
.join(', ')} = useBreakpoints()` | ||
}, | ||
hocName: 'withBreakpoints', | ||
j, | ||
importOptions: { | ||
filter: x => { | ||
return ( | ||
x.source.value == 'cozy-ui/transpiled/react' || | ||
x.source.value == 'cozy-ui/react' | ||
) | ||
}, | ||
specifiers: { | ||
useBreakpoints: true | ||
}, | ||
package: 'cozy-ui/transpiled/react' | ||
} | ||
}) | ||
|
||
return replaceBreakpointsHOC(root) | ||
} |
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
Oops, something went wrong.