Skip to content

Commit 28ce093

Browse files
committed
[Avatar] Add v12 migration
1 parent 0d7b99d commit 28ce093

File tree

4 files changed

+113
-0
lines changed

4 files changed

+113
-0
lines changed
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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React from 'react';
2+
import {Avatar} from '@shopify/polaris';
3+
4+
export function App() {
5+
return (
6+
<>
7+
<Avatar customer size="extraSmall" />
8+
<Avatar customer size="small" />
9+
<Avatar customer size="medium" />
10+
<Avatar customer size="large" />
11+
</>
12+
);
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React from 'react';
2+
import {Avatar} from '@shopify/polaris';
3+
4+
export function App() {
5+
return (
6+
<>
7+
<Avatar size="small" />
8+
<Avatar size="large" />
9+
<Avatar size="xl" />
10+
<Avatar size="2xl" />
11+
</>
12+
);
13+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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 {
10+
getJSXAttributes,
11+
removeJSXAttributes,
12+
replaceJSXAttributes,
13+
} from '../../utilities/jsx';
14+
15+
export interface MigrationOptions extends Options {
16+
relative?: boolean;
17+
}
18+
19+
export default function transformer(
20+
file: FileInfo,
21+
{jscodeshift: j}: API,
22+
options: MigrationOptions,
23+
) {
24+
const source = j(file.source);
25+
26+
if (
27+
!options.relative &&
28+
!hasImportDeclaration(j, source, '@shopify/polaris')
29+
) {
30+
return file.source;
31+
}
32+
33+
const sourcePaths = normalizeImportSourcePaths(j, source, {
34+
relative: options.relative,
35+
from: 'Avatar',
36+
to: 'Avatar',
37+
});
38+
39+
if (!sourcePaths) return;
40+
if (
41+
!hasImportSpecifier(j, source, 'Avatar', sourcePaths.from) &&
42+
!hasImportSpecifier(j, source, 'AvatarProps', sourcePaths.from)
43+
) {
44+
return;
45+
}
46+
47+
const localElementName =
48+
getImportSpecifierName(j, source, 'Avatar', sourcePaths.from) || 'Avatar';
49+
50+
// Find all JSX elements with the name 'Avatar'
51+
const avatarElements = source.findJSXElements(localElementName);
52+
53+
// Define the mapping of old sizes to new sizes
54+
const sizeMapping: {[key: string]: string} = {
55+
extraSmall: 'small',
56+
small: 'large',
57+
medium: 'xl',
58+
large: '2xl',
59+
};
60+
61+
avatarElements.forEach((element) => {
62+
// Remove the 'customer' prop
63+
removeJSXAttributes(j, element, 'customer');
64+
65+
// Replace the 'size' prop value with the new size
66+
const sizeAttribute = getJSXAttributes(j, element, 'size').at(0).get();
67+
const oldSize = sizeAttribute.value.value;
68+
const newSize = sizeMapping[oldSize] || oldSize;
69+
replaceJSXAttributes(j, element, 'size', 'size', newSize);
70+
});
71+
72+
return source.toSource();
73+
}

0 commit comments

Comments
 (0)