-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplace-columns-with-grid-template-columns.js
194 lines (171 loc) · 5.53 KB
/
replace-columns-with-grid-template-columns.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import {
getPropertyFromDeclarationScope,
getSpreadObjectProperty,
isLiteral,
replaceKey,
replaceLiteralExpressionAssignments,
replaceValue,
} from "./utils";
const names = {
columnsProp: "columns",
gridTemplateColumnsProp: "gridTemplateColumns",
};
export function replaceColumnsWithGridTemplateColumns(j, rowNode) {
const spreadAttributeObjects = j(rowNode.node.openingElement).find(
j.JSXSpreadAttribute
);
if (spreadAttributeObjects.size()) {
// prop spread
spreadAttributeObjects.forEach((spreadObject) => {
const typeOfSpreadAttributeObject = spreadObject.get(
"argument",
"type"
).value;
if (typeOfSpreadAttributeObject === "Identifier") {
// props spread from an object in declaration scope e.g.: <Component {...props}>
const columnsInDeclarationScope = getPropertyFromDeclarationScope(
j,
rowNode,
spreadObject.value.argument.name,
names.columnsProp
);
if (columnsInDeclarationScope) {
const columnsInDeclarationScopeValueType =
columnsInDeclarationScope.get("value", "type").value;
if (isLiteral(columnsInDeclarationScopeValueType)) {
// replace literal value in declaration scope e.g.: columns: 7 -> gridTemplateColumns: "repeat(7, 1fr)"
replaceValue(
j,
columnsInDeclarationScope,
names.columnsProp,
getGridTemplateColumns
);
replaceKey(
j,
columnsInDeclarationScope,
names.gridTemplateColumnsProp
);
} else if (columnsInDeclarationScopeValueType === "Identifier") {
columnsInDeclarationScope
.get("value")
.replace(
getGridTemplateQuasis(
j,
columnsInDeclarationScope.get("value").value
)
);
replaceKey(
j,
columnsInDeclarationScope,
names.gridTemplateColumnsProp
);
}
}
} else if (typeOfSpreadAttributeObject === "ObjectExpression") {
// props spread in the opening element e.g.: <Row {...{columns: 7}}>
const columnsProperty = getSpreadObjectProperty(j, spreadObject, names.columnsProp);
if (!columnsProperty) {
return;
}
const coumnsPropertyValueType = columnsProperty.get(
"value",
"type"
).value;
replaceKey(j, columnsProperty.get(), names.gridTemplateColumnsProp);
if (isLiteral(coumnsPropertyValueType)) {
columnsProperty
.get("value")
.replace(
j.literal(
getGridTemplateColumns(
columnsProperty.get("value", "value").value
)
)
);
} else if (coumnsPropertyValueType === "Identifier") {
if (columnsProperty.get("shorthand").value) {
replaceShorthandProp(j, columnsProperty, names.gridTemplateColumnsProp);
} else {
columnsProperty
.get("value")
.replace(
getGridTemplateQuasis(j, columnsProperty.get("value").value)
);
}
}
}
});
}
const columnsAttr = j(rowNode).findJSXAttribute(names.columnsProp);
if (columnsAttr.length) {
const columnsValue = columnsAttr.get("value");
columnsAttr.get("name").replace(names.gridTemplateColumnsProp);
if (isLiteral(columnsValue.value.type)) {
// replace literal value in a JSX node e.g.: <Row columns="7">
columnsValue.replace(
j.literal(getGridTemplateColumns(columnsValue.value.value))
);
} else if (columnsValue.value.type === "JSXExpressionContainer") {
const expressionType = columnsAttr.get(
"value",
"expression",
"type"
).value;
if (isLiteral(expressionType)) {
columnsValue.replace(
j.literal(getGridTemplateColumns(columnsValue.value.expression.value))
);
} else {
const expression = columnsAttr.get("value", "expression");
const templateLiteral = getGridTemplateQuasis(j, expression.value);
expression.replace(templateLiteral);
}
} else {
// replace literal values in declaration scope
const expressionName = columnsValue.value.expression.name;
replaceLiteralExpressionAssignments(
j,
rowNode,
expressionName,
getGridTemplateColumns
);
}
}
}
function getGridTemplateColumns(columns) {
return `repeat(${columns}, 1fr)`;
}
function getGridTemplateQuasis(j, propValue) {
const gridTemplateQuasis = j.templateLiteral(
[
j.templateElement({ cooked: "repeat(", raw: "repeat(" }, false),
j.templateElement({ cooked: ", 1fr)", raw: ", 1fr)" }, true),
],
[propValue]
);
return gridTemplateQuasis;
}
function replaceShorthandProp(j, shorthandProp, newValue) {
const shorthandPropType = shorthandProp.get("type").value;
if (shorthandPropType === "Property") {
shorthandProp
.get()
.replace(
j.property(
"init",
j.identifier(newValue),
getGridTemplateQuasis(j, shorthandProp.get("value").value)
)
);
}
if (shorthandPropType === "ObjectProperty") {
shorthandProp
.get()
.replace(
j.objectProperty(
j.identifier(newValue),
getGridTemplateQuasis(j, shorthandProp.get("value").value)
)
);
}
}