Skip to content

Commit 566dc20

Browse files
Add the (missing) case for migrating DisplayText to Text when size is not specified (#7679)
### WHY are these changes introduced? Currently, `npx @shopify/polaris-migrator react-replace-text-components` transforms `<DisplayText>` into `<Text as="p">`. On build, this results in a type error: ``` Type error: Property 'variant' is missing in type '{ children: string; as: "p"; }' but required in type 'TextProps'. ``` The error is sane; the issue is that the migrator doesn't have a default case for DisplayText elements that omit the (optional) `size` attribute. ### WHAT is this pull request doing? Adding a default case for DisplayText components that don't have a size. With this change, `<DisplayText>` becomes `<Text variant="headingXl" as="p">`, which does successfully build. Co-authored-by: Sam Rose <[email protected]>
1 parent d282597 commit 566dc20

File tree

4 files changed

+16
-0
lines changed

4 files changed

+16
-0
lines changed

.changeset/twelve-turkeys-exist.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@shopify/polaris-migrator': patch
3+
---
4+
5+
Add the (missing) case for migrating DisplayText to Text when size is not specified

polaris-migrator/src/migrations/react-replace-text-components/steps/replace-display-text.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ const displayTextSizeMap = {
2121
extraLarge: 'heading4xl',
2222
};
2323

24+
const defaultDisplayTextSize = 'medium';
25+
2426
/**
2527
* Replace <DisplayText> with the <Text> component
2628
*/
@@ -51,6 +53,11 @@ export function replaceDisplayText<NodeType = ASTNode>(
5153

5254
source.findJSXElements(localElementName).forEach((element) => {
5355
replaceJSXElement(j, element, 'Text');
56+
57+
if (!hasJSXAttribute(j, element, 'size')) {
58+
insertJSXAttribute(j, element, 'size', defaultDisplayTextSize);
59+
}
60+
5461
replaceJSXAttributes(j, element, 'size', 'variant', displayTextSizeMap);
5562

5663
if (hasJSXAttribute(j, element, 'element')) {

polaris-migrator/src/migrations/react-replace-text-components/tests/react-replace-text-components.input.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export function App() {
1919
<DisplayText size="extraLarge">Display text</DisplayText>
2020
<DisplayText size="large">Display text</DisplayText>
2121
<DisplayText size="medium">Display text</DisplayText>
22+
<DisplayText>Display text</DisplayText>
2223
<DisplayText size="small">Display text</DisplayText>
2324
<Heading element="h1">Heading</Heading>
2425
<Heading>Heading</Heading>

polaris-migrator/src/migrations/react-replace-text-components/tests/react-replace-text-components.output.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ export function App() {
1818
<Text variant="headingXl" as="p">
1919
Display text
2020
</Text>
21+
<Text variant="headingXl" as="p">
22+
Display text
23+
</Text>
2124
<Text variant="headingLg" as="p">
2225
Display text
2326
</Text>

0 commit comments

Comments
 (0)