-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): add codemod for styles prop
beta
- Loading branch information
1 parent
3e9f57e
commit ca3a948
Showing
8 changed files
with
191 additions
and
19 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
28 changes: 22 additions & 6 deletions
28
src/cli/migrate/__testfixtures__/input-deepref-prop.input.js
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 |
---|---|---|
@@ -1,17 +1,33 @@ | ||
import React, { useRef } from 'react'; | ||
import React, { useRef, Fragment } from 'react'; | ||
import styled from '@emotion/styled'; | ||
import { Input } from '@sumup/circuit-ui'; | ||
import { Input, TextArea } from '@sumup/circuit-ui'; | ||
|
||
const Form = () => { | ||
const ref = useRef(null); | ||
return <Input deepRef={ref} />; | ||
const inputRef = useRef(null); | ||
const textAreaRef = useRef(null); | ||
return ( | ||
<Fragment> | ||
<Input deepRef={inputRef} /> | ||
<TextArea deepRef={textAreaRef} /> | ||
</Fragment> | ||
); | ||
}; | ||
|
||
const RedInput = styled(Input)` | ||
color: red; | ||
`; | ||
|
||
const RedTextArea = styled(TextArea)` | ||
color: red; | ||
`; | ||
|
||
const RedForm = () => { | ||
const ref = useRef(null); | ||
return <RedInput deepRef={ref} />; | ||
const inputRef = useRef(null); | ||
const textAreaRef = useRef(null); | ||
return ( | ||
<Fragment> | ||
<RedInput deepRef={inputRef} /> | ||
<RedTextArea deepRef={textAreaRef} /> | ||
</Fragment> | ||
); | ||
}; |
28 changes: 22 additions & 6 deletions
28
src/cli/migrate/__testfixtures__/input-deepref-prop.output.js
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 |
---|---|---|
@@ -1,17 +1,33 @@ | ||
import React, { useRef } from 'react'; | ||
import React, { useRef, Fragment } from 'react'; | ||
import styled from '@emotion/styled'; | ||
import { Input } from '@sumup/circuit-ui'; | ||
import { Input, TextArea } from '@sumup/circuit-ui'; | ||
|
||
const Form = () => { | ||
const ref = useRef(null); | ||
return <Input ref={ref} />; | ||
const inputRef = useRef(null); | ||
const textAreaRef = useRef(null); | ||
return ( | ||
<Fragment> | ||
<Input ref={inputRef} /> | ||
<TextArea ref={textAreaRef} /> | ||
</Fragment> | ||
); | ||
}; | ||
|
||
const RedInput = styled(Input)` | ||
color: red; | ||
`; | ||
|
||
const RedTextArea = styled(TextArea)` | ||
color: red; | ||
`; | ||
|
||
const RedForm = () => { | ||
const ref = useRef(null); | ||
return <RedInput ref={ref} />; | ||
const inputRef = useRef(null); | ||
const textAreaRef = useRef(null); | ||
return ( | ||
<Fragment> | ||
<RedInput ref={inputRef} /> | ||
<RedTextArea ref={textAreaRef} /> | ||
</Fragment> | ||
); | ||
}; |
42 changes: 42 additions & 0 deletions
42
src/cli/migrate/__testfixtures__/input-styles-prop.input.js
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,42 @@ | ||
import React, { useRef } from 'react'; | ||
import styled from '@emotion/styled'; | ||
import { css } from '@emotion/core'; | ||
import { Input, TextArea } from '@sumup/circuit-ui'; | ||
|
||
const Form = () => ( | ||
<> | ||
<Input | ||
wrapperStyles={theme => css` | ||
color: red; | ||
`} | ||
/> | ||
<TextArea | ||
wrapperStyles={theme => css` | ||
color: red; | ||
`} | ||
/> | ||
</> | ||
); | ||
|
||
const RedInput = styled(Input)` | ||
color: red; | ||
`; | ||
|
||
const RedTextArea = styled(TextArea)` | ||
color: red; | ||
`; | ||
|
||
const RedForm = () => ( | ||
<> | ||
<RedInput | ||
wrapperStyles={theme => css` | ||
color: red; | ||
`} | ||
/> | ||
<RedTextArea | ||
wrapperStyles={theme => css` | ||
color: red; | ||
`} | ||
/> | ||
</> | ||
); |
42 changes: 42 additions & 0 deletions
42
src/cli/migrate/__testfixtures__/input-styles-prop.output.js
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,42 @@ | ||
import React, { useRef } from 'react'; | ||
import styled from '@emotion/styled'; | ||
import { css } from '@emotion/core'; | ||
import { Input, TextArea } from '@sumup/circuit-ui'; | ||
|
||
const Form = () => ( | ||
<> | ||
<Input | ||
labelStyles={theme => css` | ||
color: red; | ||
`} | ||
/> | ||
<TextArea | ||
labelStyles={theme => css` | ||
color: red; | ||
`} | ||
/> | ||
</> | ||
); | ||
|
||
const RedInput = styled(Input)` | ||
color: red; | ||
`; | ||
|
||
const RedTextArea = styled(TextArea)` | ||
color: red; | ||
`; | ||
|
||
const RedForm = () => ( | ||
<> | ||
<RedInput | ||
labelStyles={theme => css` | ||
color: red; | ||
`} | ||
/> | ||
<RedTextArea | ||
labelStyles={theme => css` | ||
color: red; | ||
`} | ||
/> | ||
</> | ||
); |
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
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
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,46 @@ | ||
/** | ||
* Copyright 2020, SumUp Ltd. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { Transform, JSCodeshift, Collection } from 'jscodeshift'; | ||
|
||
import { renameJSXAttribute, findLocalNames } from './utils'; | ||
|
||
function transformFactory( | ||
j: JSCodeshift, | ||
root: Collection, | ||
componentName: string | ||
): void { | ||
const components = findLocalNames(j, root, componentName); | ||
|
||
if (!components) { | ||
return; | ||
} | ||
|
||
components.forEach(component => { | ||
renameJSXAttribute(j, root, component, 'wrapperStyles', 'labelStyles'); | ||
}); | ||
} | ||
|
||
const transform: Transform = (file, api) => { | ||
const j = api.jscodeshift; | ||
const root = j(file.source); | ||
|
||
transformFactory(j, root, 'Input'); | ||
transformFactory(j, root, 'TextArea'); | ||
|
||
return root.toSource(); | ||
}; | ||
|
||
export default transform; |