Skip to content

Commit 9cd78bc

Browse files
committed
Initial POC
1 parent ea26158 commit 9cd78bc

File tree

5 files changed

+99
-10
lines changed

5 files changed

+99
-10
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import type {API, FileInfo} from 'jscodeshift';
2+
3+
import {replaceJSXElement} from '../../utilities/jsx';
4+
import {
5+
hasImportDeclaration,
6+
renameImportSpecifier,
7+
getImportSpecifierName,
8+
hasImportSpecifier,
9+
removeImportSpecifier,
10+
renameImportSourcePath,
11+
renameImportDeclaration,
12+
} from '../../utilities/imports';
13+
14+
/**
15+
* Replace <Card> with the <AlphaCard> component
16+
*/
17+
export default function replaceCardComponent(
18+
fileInfo: FileInfo,
19+
{jscodeshift: j}: API,
20+
) {
21+
const source = j(fileInfo.source);
22+
const sourcePathRegex = /(?:\@shopify\/polaris|\.\.\/Card)/gi;
23+
const updatedSourcePathRegex = /(?:\@shopify\/polaris|\.\.\/AlphaCard)/gi;
24+
25+
if (!hasImportDeclaration(j, source, sourcePathRegex)) {
26+
return fileInfo.source;
27+
}
28+
29+
if (hasImportSpecifier(j, source, 'AlphaCard', updatedSourcePathRegex)) {
30+
// console.log('migration — first conditional');
31+
removeImportSpecifier(j, source, 'Card', sourcePathRegex);
32+
} else {
33+
// console.log('migration — second conditional');
34+
renameImportSpecifier(j, source, 'Card', 'AlphaCard', sourcePathRegex);
35+
// renameImportDeclaration(j, source, sourcePathRegex, '?');
36+
// renameImportSourcePath(j, source, 'Card', 'AlphaCard', sourcePathRegex);
37+
}
38+
39+
const localElementName =
40+
getImportSpecifierName(j, source, 'Card', updatedSourcePathRegex) || 'Card';
41+
42+
source.findJSXElements(localElementName).forEach((element) => {
43+
replaceJSXElement(j, element, 'AlphaCard');
44+
});
45+
46+
return source.toSource();
47+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import React from 'react';
2+
import {Card} from '../Card';
3+
4+
export function App() {
5+
return <Card>Card</Card>;
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import React from 'react';
2+
import {AlphaCard} from '../AlphaCard';
3+
4+
export function App() {
5+
return <AlphaCard>Card</AlphaCard>;
6+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {check} from '../../../utilities/testUtils';
2+
3+
const migration = 'replace-card-component';
4+
const fixtures = ['replace-card-component'];
5+
6+
for (const fixture of fixtures) {
7+
check(__dirname, {
8+
fixture,
9+
migration,
10+
});
11+
}

polaris-migrator/src/utilities/imports.ts

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ import core, {Collection} from 'jscodeshift';
33
export function hasImportDeclaration(
44
j: core.JSCodeshift,
55
source: Collection<any>,
6-
sourcePath: string,
6+
sourcePath: string | RegExp,
77
) {
88
return Boolean(
9-
source
10-
.find(j.ImportDeclaration)
11-
.filter((path) => path.node.source.value === sourcePath).length,
9+
source.find(j.ImportDeclaration).filter((path) => {
10+
const nodePath = path.node.source.value;
11+
return typeof sourcePath === 'string'
12+
? nodePath === sourcePath
13+
: sourcePath.test(nodePath?.toString() ?? '');
14+
}).length,
1215
);
1316
}
1417

@@ -85,11 +88,16 @@ export function getImportSpecifier(
8588
j: core.JSCodeshift,
8689
source: Collection<any>,
8790
specifier: string,
88-
sourcePath: string,
91+
sourcePath: string | RegExp,
8992
) {
9093
return source
9194
.find(j.ImportDeclaration)
92-
.filter((path) => path.node.source.value === sourcePath)
95+
.filter((path) => {
96+
const nodePath = path.node.source.value;
97+
return typeof sourcePath === 'string'
98+
? nodePath === sourcePath
99+
: sourcePath.test(nodePath?.toString() ?? '');
100+
})
93101
.find(j.ImportSpecifier)
94102
.filter((path) => path.value.imported.name === specifier);
95103
}
@@ -98,9 +106,10 @@ export function getImportSpecifierName(
98106
j: core.JSCodeshift,
99107
source: Collection<any>,
100108
specifier: string,
101-
sourcePath: string,
109+
sourcePath: string | RegExp,
102110
) {
103111
const specifiers = getImportSpecifier(j, source, specifier, sourcePath);
112+
console.log({specifiers});
104113

105114
return specifiers.length > 0 ? specifiers.nodes()[0]!.local!.name : null;
106115
}
@@ -109,7 +118,7 @@ export function hasImportSpecifier(
109118
j: core.JSCodeshift,
110119
source: Collection<any>,
111120
specifier: string,
112-
sourcePath: string,
121+
sourcePath: string | RegExp,
113122
) {
114123
return Boolean(getImportSpecifier(j, source, specifier, sourcePath)?.length);
115124
}
@@ -139,8 +148,16 @@ export function renameImportSpecifier(
139148
source: Collection<any>,
140149
specifier: string,
141150
newSpecifier: string,
142-
sourcePath: string,
151+
sourcePath: string | RegExp,
143152
) {
153+
console.log('inside rename');
154+
console.log(getImportSpecifier(j, source, specifier, sourcePath));
155+
// console.log(j.importSpecifier(j.identifier(newSpecifier)));
156+
// if (typeof sourcePath === 'string') {
157+
// console.log({sourcePath});
158+
// } else {
159+
// console.log('inside the else');
160+
// }
144161
getImportSpecifier(j, source, specifier, sourcePath).replaceWith(
145162
j.importSpecifier(j.identifier(newSpecifier)),
146163
);
@@ -150,7 +167,9 @@ export function removeImportSpecifier(
150167
j: core.JSCodeshift,
151168
source: Collection<any>,
152169
specifier: string,
153-
sourcePath: string,
170+
sourcePath: string | RegExp,
154171
) {
172+
console.log('inside remove');
173+
console.log(getImportSpecifier(j, source, specifier, sourcePath));
155174
getImportSpecifier(j, source, specifier, sourcePath).remove();
156175
}

0 commit comments

Comments
 (0)