diff --git a/src/cli/migrate/__testfixtures__/selector-props.input.js b/src/cli/migrate/__testfixtures__/selector-props.input.js new file mode 100644 index 0000000000..fa481d42a8 --- /dev/null +++ b/src/cli/migrate/__testfixtures__/selector-props.input.js @@ -0,0 +1,11 @@ +import React from 'react'; +import styled from '@emotion/styled'; +import { Selector } from '@sumup/circuit-ui'; + +const BaseSelector = () => ; + +const RedSelector = styled(Selector)` + color: red; +`; + +const StyledSelector = () => ; diff --git a/src/cli/migrate/__testfixtures__/selector-props.output.js b/src/cli/migrate/__testfixtures__/selector-props.output.js new file mode 100644 index 0000000000..806512a858 --- /dev/null +++ b/src/cli/migrate/__testfixtures__/selector-props.output.js @@ -0,0 +1,11 @@ +import React from 'react'; +import styled from '@emotion/styled'; +import { Selector } from '@sumup/circuit-ui'; + +const BaseSelector = () => ; + +const RedSelector = styled(Selector)` + color: red; +`; + +const StyledSelector = () => ; diff --git a/src/cli/migrate/__tests__/transforms.spec.js b/src/cli/migrate/__tests__/transforms.spec.js index a0edb4fa48..306134fb9a 100644 --- a/src/cli/migrate/__tests__/transforms.spec.js +++ b/src/cli/migrate/__tests__/transforms.spec.js @@ -21,3 +21,4 @@ defineTest(__dirname, 'button-variant-enum'); defineTest(__dirname, 'list-variant-enum'); defineTest(__dirname, 'onchange-prop'); defineTest(__dirname, 'as-prop'); +defineTest(__dirname, 'selector-props'); diff --git a/src/cli/migrate/selector-props.ts b/src/cli/migrate/selector-props.ts new file mode 100644 index 0000000000..5de0a80638 --- /dev/null +++ b/src/cli/migrate/selector-props.ts @@ -0,0 +1,38 @@ +/** + * 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 } from 'jscodeshift'; + +import { renameJSXAttribute, findLocalNames } from './utils'; + +const transform: Transform = (file, api) => { + const j = api.jscodeshift; + const root = j(file.source); + + const components = findLocalNames(j, root, 'Selector'); + + if (!components) { + return; + } + + components.forEach(component => { + renameJSXAttribute(j, root, component, 'onClick', 'onChange'); + renameJSXAttribute(j, root, component, 'selected', 'checked'); + }); + + return root.toSource(); +}; + +export default transform;