Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow value changes in prop migration #10246

Merged
merged 1 commit into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/breezy-badgers-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/polaris-migrator': minor
---

Added the ability to migrate prop values
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react';

declare function MyComponent(props: any): JSX.Element;

export function App() {
return (
<>
<MyComponent foo="bar" prop="non-targetted-value">
Hello world
</MyComponent>
<MyComponent foo="bar" prop="targetted-value">
Hello world
</MyComponent>
</>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react';

declare function MyComponent(props: any): JSX.Element;

export function App() {
return (
<>
<MyComponent foo="bar" variant="non-targetted-value">
Hello world
</MyComponent>
<MyComponent foo="bar" variant="new-targetted-value">
Hello world
</MyComponent>
</>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const fixtures = [
componentName: 'MyComponent',
from: 'prop',
to: 'newProp',
newValue: 'new-value',
toValue: 'new-value',
},
},
{
Expand All @@ -25,7 +25,7 @@ const fixtures = [
componentName: 'MyComponent.CompoundComponent',
from: 'prop',
to: 'newProp',
newValue: 'new-value',
toValue: 'new-value',
},
},
{
Expand All @@ -34,7 +34,17 @@ const fixtures = [
componentName: 'MyComponent',
from: 'booleanProp',
to: 'variant',
newValue: 'boolean-prop-value',
toValue: 'boolean-prop-value',
},
},
{
name: 'react-rename-component-prop-with-specific-value',
options: {
componentName: 'MyComponent',
from: 'prop',
to: 'variant',
fromValue: 'targetted-value',
toValue: 'new-targetted-value',
},
},
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,17 @@ export default function transformer(
options: Options,
) {
const componentParts = options.componentName?.split('.');
if (
!options.componentName ||
!options.from ||
!options.to ||
componentParts?.length > 2
) {
const {componentName, from, to, fromValue, toValue} = options;
if (!componentName || !from || !to || componentParts?.length > 2) {
throw new Error(
'Missing required options: componentName, from, to, or your compound component exceeds 2 levels',
);
}

const source = j(file.source);
const componentName = options.componentName;
const props = {[options.from]: options.to};
const props = {[from]: to};

renameProps(j, source, componentName, props, options.newValue);
renameProps(j, source, componentName, props, fromValue, toValue);

return source.toSource();
}
13 changes: 8 additions & 5 deletions polaris-migrator/src/utilities/jsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ export function renameProps(
source: Collection<any>,
componentName: string,
props: {[from: string]: string},
newValue?: string,
fromValue?: string,
toValue?: string,
) {
const [component, subcomponent] = componentName.split('.');

Expand All @@ -141,7 +142,7 @@ export function renameProps(
element.node.openingElement.name.property.name === subcomponent
) {
element.node.openingElement.attributes?.forEach((node) =>
updateNode(node, props, newValue),
updateNode(node, props, fromValue, toValue),
);
}
});
Expand All @@ -151,7 +152,7 @@ export function renameProps(
// Handle basic components
source.findJSXElements(componentName)?.forEach((element) => {
element.node.openingElement.attributes?.forEach((node) =>
updateNode(node, props, newValue),
updateNode(node, props, fromValue, toValue),
);
});

Expand All @@ -160,7 +161,8 @@ export function renameProps(
function updateNode(
node: any,
props: {[from: string]: string},
newValue?: string,
fromValue?: string,
toValue?: string,
) {
const isFromProp = (prop: unknown): prop is keyof typeof props =>
Object.keys(props).includes(prop as string);
Expand All @@ -170,7 +172,8 @@ export function renameProps(
}

node.name.name = props[node.name.name];
node.value = j.stringLiteral(newValue ?? node.value.value);
if (fromValue && node.value.value !== fromValue) return node;
node.value = j.stringLiteral(toValue ?? node.value.value);
return node;
}
}
Expand Down