-
Notifications
You must be signed in to change notification settings - Fork 337
/
upsell-primary-secondary-action-replace-primary-secondary-link.js
63 lines (53 loc) · 1.67 KB
/
upsell-primary-secondary-action-replace-primary-secondary-link.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
* Converts
* <Upsell primaryLink={...} secondaryLink={...} /> to <Upsell primaryAction={...} secondaryAction={...} />
*/
export default function transformer(file, api) {
const j = api.jscodeshift;
const src = j(file.source);
let localIdentifierName;
let fileHasModifications = false;
src.find(j.ImportDeclaration).forEach((path) => {
const decl = path.node;
if (decl.source.value !== 'gestalt') {
return null;
}
localIdentifierName = decl.specifiers
.filter((node) => node.imported.name === 'Upsell')
.map((node) => node.local.name);
return null;
});
if (!localIdentifierName) {
return null;
}
const transform = src
.find(j.JSXElement)
.forEach((jsxElement) => {
const { node } = jsxElement;
if (!localIdentifierName.includes(node.openingElement.name.name)) {
return null;
}
const attrs = node.openingElement.attributes;
const newAppendAttr = [];
const newAttrs = attrs
.map((attr) => {
if (attr?.name?.name && attr.name.name === 'primaryLink') {
const renamedAttr = { ...attr };
renamedAttr.name.name = 'primaryAction';
return renamedAttr;
}
if (attr?.name?.name && attr.name.name === 'secondaryLink') {
const renamedAttr = { ...attr };
renamedAttr.name.name = 'secondaryAction';
return renamedAttr;
}
return attr;
})
.filter(Boolean);
fileHasModifications = true;
node.openingElement.attributes = [...newAppendAttr, ...newAttrs];
return null;
})
.toSource();
return fileHasModifications ? transform : null;
}