Skip to content

Commit 471c3d5

Browse files
lgriffeelaurkim
andauthored
Add v12-react-avatar-component migration (#10874)
### WHY are these changes introduced? In order to run the `v12-react-avatar-component` migration as part of our beta release it needs to be merged and released on `main` instead of the `next` branch. ### WHAT is this pull request doing? Ports over `v12-react-avatar-component` migration from [[Avatar] Remove customer prop and experiemntal values #10283](#10283) to main branch --------- Co-authored-by: Lo Kim <lo.kim@shopify.com> Co-authored-by: Lo Kim <26749317+laurkim@users.noreply.github.com>
1 parent 046894b commit 471c3d5

File tree

5 files changed

+115
-0
lines changed

5 files changed

+115
-0
lines changed

.changeset/spotty-garlics-kick.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@shopify/polaris-migrator': minor
3+
---
4+
5+
Ported `v12-react-avatar-component` migration from [Avatar] Remove customer prop and experimental values [#10283](https://github.com/Shopify/polaris/pull/10283) to main branch
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import {check} from '../../../utilities/check';
2+
3+
const transform = 'v12-react-avatar-component';
4+
const fixtures = ['v12-react-avatar-component'];
5+
6+
for (const fixture of fixtures) {
7+
check(__dirname, {
8+
fixture,
9+
transform,
10+
options: {
11+
relative: fixture.includes('relative') ? true : undefined,
12+
},
13+
});
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import React from 'react';
2+
import {Avatar} from '@shopify/polaris';
3+
4+
export function App() {
5+
return (
6+
<>
7+
<Avatar customer size={undefined} />
8+
<Avatar customer size="extraSmall" />
9+
<Avatar customer size="small" />
10+
<Avatar customer size="medium" />
11+
<Avatar customer size="large" />
12+
<Avatar size="xl-experimental" />
13+
<Avatar size="2xl-experimental" />
14+
</>
15+
);
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import React from 'react';
2+
import {Avatar} from '@shopify/polaris';
3+
4+
export function App() {
5+
return (
6+
<>
7+
<Avatar customer size={undefined} />
8+
<Avatar customer size="xs" />
9+
<Avatar customer size="sm" />
10+
<Avatar customer size="md" />
11+
<Avatar customer size="lg" />
12+
<Avatar size="xl" />
13+
<Avatar size="xl" />
14+
</>
15+
);
16+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import type {API, FileInfo, Options} from 'jscodeshift';
2+
3+
import {
4+
getImportSpecifierName,
5+
hasImportDeclaration,
6+
hasImportSpecifier,
7+
normalizeImportSourcePaths,
8+
} from '../../utilities/imports';
9+
import {replaceJSXAttributes} from '../../utilities/jsx';
10+
11+
export interface MigrationOptions extends Options {
12+
relative?: boolean;
13+
}
14+
15+
export default function transformer(
16+
file: FileInfo,
17+
{jscodeshift: j}: API,
18+
options: MigrationOptions,
19+
) {
20+
const source = j(file.source);
21+
22+
if (
23+
!options.relative &&
24+
!hasImportDeclaration(j, source, '@shopify/polaris')
25+
) {
26+
return file.source;
27+
}
28+
29+
const sourcePaths = normalizeImportSourcePaths(j, source, {
30+
relative: options.relative,
31+
from: 'Avatar',
32+
to: 'Avatar',
33+
});
34+
35+
if (!sourcePaths) return;
36+
if (
37+
!hasImportSpecifier(j, source, 'Avatar', sourcePaths.from) &&
38+
!hasImportSpecifier(j, source, 'AvatarProps', sourcePaths.from)
39+
) {
40+
return;
41+
}
42+
43+
const localElementName =
44+
getImportSpecifierName(j, source, 'Avatar', sourcePaths.from) || 'Avatar';
45+
46+
// Find all JSX elements with the name 'Avatar'
47+
source.findJSXElements(localElementName).forEach((element) => {
48+
// Replace the 'size' prop value with the new size
49+
replaceJSXAttributes(j, element, 'size', 'size', sizeMapping);
50+
});
51+
52+
return source.toSource();
53+
}
54+
55+
// Define the mapping of old sizes to new sizes
56+
const sizeMapping = {
57+
extraSmall: 'xs',
58+
small: 'sm',
59+
medium: 'md',
60+
large: 'lg',
61+
'xl-experimental': 'xl',
62+
// 2xl-experimental is not supported in the new Avatar component
63+
'2xl-experimental': 'xl',
64+
};

0 commit comments

Comments
 (0)