-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplace-gutter-with-grid-gap.js
148 lines (130 loc) · 3.89 KB
/
replace-gutter-with-grid-gap.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import {
getPropertyFromDeclarationScope,
getSpreadObjectProperty,
isLiteral,
replaceKey,
replaceLiteralExpressionAssignments,
replaceValue,
} from "./utils";
const names = {
gutterProp: "gutter",
gridGapProp: "gridGap",
};
export function replaceGutterWithGridGap(j, rowNode) {
const spreadAttributeObjects = j(rowNode.node.openingElement).find(
j.JSXSpreadAttribute
);
// gutter as a simple JSX Attribute, e.g.: gutter="medium"
const gutterAttr = j(rowNode).findJSXAttribute(names.gutterProp);
if (gutterAttr.length) {
const gutterValue = gutterAttr.get("value");
gutterAttr.get("name").replace(names.gridGapProp);
if (isLiteral(gutterValue.value.type)) {
// replace literal value in JSX node
gutterValue.replace(
j.literal(convertGutterToGridGap(gutterValue.value.value))
);
} else {
// replace literal values in declaration scope
const expressionType = gutterAttr.get(
"value",
"expression",
"type"
).value;
const expressionName = gutterValue.value.expression.name;
if (isLiteral(expressionType)) {
const gutterExpressionLiteralValue = gutterValue.get(
"expression",
"value"
).value;
gutterValue.replace(
j.literal(convertGutterToGridGap(gutterExpressionLiteralValue))
);
} else if (expressionType === "Identifier") {
replaceLiteralExpressionAssignments(
j,
rowNode,
expressionName,
convertGutterToGridGap
);
}
}
}
if (spreadAttributeObjects.size()) {
// prop spread
spreadAttributeObjects.forEach((spreadObject) => {
convertSpreadObjectValues(j, spreadObject, rowNode);
});
}
}
function convertSpreadObjectValues(j, spreadObject, rowNode) {
const typeOfSpreadAttributeObject = spreadObject.get(
"argument",
"type"
).value;
if (typeOfSpreadAttributeObject === "Identifier") {
// props spread from an object in declaration scope e.g.: <Row {...props}>
const gutterInDeclarationScope = getPropertyFromDeclarationScope(
j,
rowNode,
spreadObject.value.argument.name,
names.gutterProp
);
if (gutterInDeclarationScope) {
const gutterValueType = gutterInDeclarationScope.get(
"value",
"type"
).value;
if (isLiteral(gutterValueType)) {
// replace literal value in declaration scope e.g.: gutter: "medium" -> gridGap: "15px"
replaceValue(
j,
gutterInDeclarationScope,
names.gutterProp,
convertGutterToGridGap
);
replaceKey(j, gutterInDeclarationScope, names.gridGapProp);
} else if (gutterValueType === "Identifier") {
replaceKey(j, gutterInDeclarationScope, names.gridGapProp);
}
}
} else if (typeOfSpreadAttributeObject === "ObjectExpression") {
// props spread in the opening element e.g.: <Row {...{gutter: "medium"}}>
const gutterProperty = getSpreadObjectProperty(j, spreadObject, names.gutterProp);
if (!gutterProperty) {
return;
}
const gutterPropName = gutterProperty.get("value", "name").value;
replaceValue(j, gutterProperty.get(), gutterPropName, convertGutterToGridGap);
replaceKey(j, gutterProperty.get(), names.gridGapProp);
if (gutterProperty.get("value", "type").value === "Identifier") {
replaceLiteralExpressionAssignments(
j,
rowNode,
gutterPropName,
convertGutterToGridGap
);
}
}
}
function convertGutterToGridGap(gutter) {
switch (gutter) {
case "extra-small":
return "2px";
case "small":
return "5px";
case "medium-small":
return "10px";
case "medium":
return "15px";
case "medium-large":
return "30px";
case "large":
return "60px";
case "extra-large":
return "90px";
case "none":
default:
return 0;
}
}