@mui/codemod 5.0.5 → 5.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -5
- package/codemod.js +7 -1
- package/node/util/propsToObject.js +31 -19
- package/node/v5.0.0/box-sx-prop.js +11 -0
- package/node/v5.0.0/box-sx-prop.test/alias-actual.js +15 -0
- package/node/v5.0.0/box-sx-prop.test/alias-expected.js +15 -0
- package/node/v5.0.0/jss-to-styled.js +130 -34
- package/node/v5.0.0/jss-to-styled.test/multipleWithStyles.actual.js +46 -0
- package/node/v5.0.0/jss-to-styled.test/multipleWithStyles.expected.js +68 -0
- package/node/v5.0.0/mui-replace.js +1 -1
- package/node/v5.0.0/mui-replace.test/expected.js +2 -2
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -26,6 +26,8 @@ Options:
|
|
|
26
26
|
--help Show help [boolean]
|
|
27
27
|
--dry dry run (no changes are made to files)
|
|
28
28
|
[boolean] [default: false]
|
|
29
|
+
--parser which parser for jscodeshift to use.
|
|
30
|
+
[string] [default: 'tsx']
|
|
29
31
|
--print print transformed files to stdout, useful for development
|
|
30
32
|
[boolean] [default: false]
|
|
31
33
|
--jscodeshift [string] [default: false]
|
|
@@ -34,6 +36,7 @@ Examples:
|
|
|
34
36
|
npx @mui/codemod v4.0.0/theme-spacing-api src
|
|
35
37
|
npx @mui/codemod v5.0.0/component-rename-prop src --
|
|
36
38
|
--component=Grid --from=prop --to=newProp
|
|
39
|
+
npx @mui/codemod v5.0.0/preset-safe src --parser=flow
|
|
37
40
|
```
|
|
38
41
|
|
|
39
42
|
#### jscodeshift options
|
|
@@ -756,10 +759,10 @@ You can find more details about this breaking change in [the migration guide](ht
|
|
|
756
759
|
#### `skeleton-variant`
|
|
757
760
|
|
|
758
761
|
```diff
|
|
759
|
-
-<Skeleton
|
|
760
|
-
-<Skeleton
|
|
761
|
-
+<Skeleton
|
|
762
|
-
+<Skeleton
|
|
762
|
+
-<Skeleton variant="circle" />
|
|
763
|
+
-<Skeleton variant="rect" />
|
|
764
|
+
+<Skeleton variant="circular" />
|
|
765
|
+
+<Skeleton variant="rectangular" />
|
|
763
766
|
```
|
|
764
767
|
|
|
765
768
|
```sh
|
|
@@ -1119,7 +1122,7 @@ Replace every occurrence of `material-ui` related package with the new package n
|
|
|
1119
1122
|
+"@mui/material": "next",
|
|
1120
1123
|
+"@mui/icons-material": "next",
|
|
1121
1124
|
+"@mui/lab": "next",
|
|
1122
|
-
+"@mui/
|
|
1125
|
+
+"@mui/base": "next",
|
|
1123
1126
|
+"@mui/styled-engine-sc": "next",
|
|
1124
1127
|
```
|
|
1125
1128
|
|
package/codemod.js
CHANGED
|
@@ -42,10 +42,11 @@ async function runTransform(transform, files, flags, codemodFlags) {
|
|
|
42
42
|
'--extensions',
|
|
43
43
|
'js,ts,jsx,tsx',
|
|
44
44
|
'--parser',
|
|
45
|
-
'tsx',
|
|
45
|
+
flags.parser || 'tsx',
|
|
46
46
|
'--ignore-pattern',
|
|
47
47
|
'**/node_modules/**',
|
|
48
48
|
];
|
|
49
|
+
|
|
49
50
|
if (flags.dry) {
|
|
50
51
|
args.push('--dry');
|
|
51
52
|
}
|
|
@@ -98,6 +99,11 @@ yargs
|
|
|
98
99
|
default: false,
|
|
99
100
|
type: 'boolean',
|
|
100
101
|
})
|
|
102
|
+
.option('parser', {
|
|
103
|
+
description: 'which parser for jscodeshift to use',
|
|
104
|
+
default: 'tsx',
|
|
105
|
+
type: 'string',
|
|
106
|
+
})
|
|
101
107
|
.option('print', {
|
|
102
108
|
description: 'print transformed files to stdout, useful for development',
|
|
103
109
|
default: false,
|
|
@@ -9,6 +9,7 @@ function propsToObject({
|
|
|
9
9
|
j,
|
|
10
10
|
root,
|
|
11
11
|
componentName,
|
|
12
|
+
aliasName,
|
|
12
13
|
propName,
|
|
13
14
|
props
|
|
14
15
|
}) {
|
|
@@ -20,30 +21,41 @@ function propsToObject({
|
|
|
20
21
|
return value;
|
|
21
22
|
}
|
|
22
23
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
propValue = buildObject(node, propValue);
|
|
30
|
-
delete attributes[index];
|
|
24
|
+
const result = aliasName ? root.find(j.JSXElement, {
|
|
25
|
+
openingElement: {
|
|
26
|
+
name: {
|
|
27
|
+
property: {
|
|
28
|
+
name: componentName
|
|
29
|
+
}
|
|
31
30
|
}
|
|
32
|
-
}
|
|
31
|
+
}
|
|
32
|
+
}) : root.findJSXElements(componentName);
|
|
33
|
+
return result.forEach(path => {
|
|
34
|
+
if (!aliasName || aliasName && path.node.openingElement.name.object.name === aliasName) {
|
|
35
|
+
let propValue = [];
|
|
36
|
+
const attributes = path.node.openingElement.attributes;
|
|
37
|
+
attributes.forEach((node, index) => {
|
|
38
|
+
// Only transform whitelisted props
|
|
39
|
+
if (node.type === 'JSXAttribute' && props.includes(node.name.name)) {
|
|
40
|
+
propValue = buildObject(node, propValue);
|
|
41
|
+
delete attributes[index];
|
|
42
|
+
}
|
|
43
|
+
});
|
|
33
44
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
45
|
+
if (propValue.length > 0) {
|
|
46
|
+
const propNameAttr = attributes.find(attr => {
|
|
47
|
+
var _attr$name;
|
|
37
48
|
|
|
38
|
-
|
|
39
|
-
|
|
49
|
+
return (attr == null ? void 0 : (_attr$name = attr.name) == null ? void 0 : _attr$name.name) === propName;
|
|
50
|
+
});
|
|
40
51
|
|
|
41
|
-
|
|
42
|
-
|
|
52
|
+
if (propNameAttr) {
|
|
53
|
+
var _propNameAttr$value$e;
|
|
43
54
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
55
|
+
(((_propNameAttr$value$e = propNameAttr.value.expression) == null ? void 0 : _propNameAttr$value$e.properties) || []).push(...j.objectExpression(propValue).properties);
|
|
56
|
+
} else {
|
|
57
|
+
attributes.push(j.jsxAttribute(j.jsxIdentifier(propName), j.jsxExpressionContainer(j.objectExpression(propValue))));
|
|
58
|
+
}
|
|
47
59
|
}
|
|
48
60
|
}
|
|
49
61
|
});
|
|
@@ -21,9 +21,20 @@ function transformer(file, api, options) {
|
|
|
21
21
|
const printOptions = options.printOptions || {
|
|
22
22
|
quote: 'single'
|
|
23
23
|
};
|
|
24
|
+
let aliasName;
|
|
25
|
+
root.find(j.ImportDeclaration).forEach(path => {
|
|
26
|
+
if (path.node.source.value.match(/^(@mui\/material|@material-ui\/core)$/)) {
|
|
27
|
+
var _path$node$specifiers;
|
|
28
|
+
|
|
29
|
+
if (((_path$node$specifiers = path.node.specifiers[0]) == null ? void 0 : _path$node$specifiers.type) === 'ImportNamespaceSpecifier') {
|
|
30
|
+
aliasName = path.node.specifiers[0].local.name;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
});
|
|
24
34
|
return (0, _propsToObject.default)({
|
|
25
35
|
j,
|
|
26
36
|
root,
|
|
37
|
+
aliasName,
|
|
27
38
|
componentName: 'Box',
|
|
28
39
|
propName: 'sx',
|
|
29
40
|
props
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var UI = _interopRequireWildcard(require("@material-ui/core"));
|
|
4
|
+
|
|
5
|
+
var _jsxRuntime = require("react/jsx-runtime");
|
|
6
|
+
|
|
7
|
+
var _UI$Box;
|
|
8
|
+
|
|
9
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
10
|
+
|
|
11
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
12
|
+
|
|
13
|
+
const Foo = () => _UI$Box || (_UI$Box = /*#__PURE__*/(0, _jsxRuntime.jsx)(UI.Box, {
|
|
14
|
+
px: 2
|
|
15
|
+
}));
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var UI = _interopRequireWildcard(require("@material-ui/core"));
|
|
4
|
+
|
|
5
|
+
var _jsxRuntime = require("react/jsx-runtime");
|
|
6
|
+
|
|
7
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
8
|
+
|
|
9
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
10
|
+
|
|
11
|
+
const Foo = () => /*#__PURE__*/(0, _jsxRuntime.jsx)(UI.Box, {
|
|
12
|
+
sx: {
|
|
13
|
+
px: 2
|
|
14
|
+
}
|
|
15
|
+
});
|
|
@@ -164,21 +164,30 @@ function transformer(file, api, options) {
|
|
|
164
164
|
|
|
165
165
|
return declaration;
|
|
166
166
|
}
|
|
167
|
+
|
|
168
|
+
const classesCount = {};
|
|
167
169
|
/**
|
|
168
170
|
*
|
|
169
171
|
* @param {import('jscodeshift').ObjectExpression} objExpression
|
|
172
|
+
* @param {import('jscodeshift').ObjectExpression} prevObj
|
|
170
173
|
*/
|
|
171
174
|
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
const classes = j.objectExpression([]);
|
|
175
|
+
function createClasses(objExpression, prevObj) {
|
|
176
|
+
const classes = prevObj || j.objectExpression([]);
|
|
175
177
|
objExpression.properties.forEach(prop => {
|
|
176
|
-
|
|
178
|
+
if (!classesCount[prop.key.name]) {
|
|
179
|
+
classesCount[prop.key.name] = 1;
|
|
180
|
+
} else {
|
|
181
|
+
classesCount[prop.key.name] += 1;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const resolvedKey = classesCount[prop.key.name] === 1 ? prop.key.name : `${prop.key.name}${classesCount[prop.key.name]}`;
|
|
185
|
+
classes.properties.push(j.objectProperty(j.identifier(resolvedKey), j.templateLiteral([j.templateElement({
|
|
177
186
|
raw: '',
|
|
178
187
|
cooked: ''
|
|
179
188
|
}, false), j.templateElement({
|
|
180
|
-
raw: `-${
|
|
181
|
-
cooked: `-${
|
|
189
|
+
raw: `-${resolvedKey}`,
|
|
190
|
+
cooked: `-${resolvedKey}`
|
|
182
191
|
}, true)], [j.identifier('PREFIX')])));
|
|
183
192
|
});
|
|
184
193
|
return classes;
|
|
@@ -225,46 +234,66 @@ function transformer(file, api, options) {
|
|
|
225
234
|
return null;
|
|
226
235
|
}
|
|
227
236
|
/**
|
|
228
|
-
*
|
|
229
|
-
* @param {import('jscodeshift').ArrowFunctionExpression | import('jscodeshift').FunctionDeclaration} functionExpression
|
|
237
|
+
* @param {import('jscodeshift').ObjectExpression | import('jscodeshift').ArrowFunctionExpression | import('jscodeshift').FunctionDeclaration} expression
|
|
230
238
|
*/
|
|
231
239
|
|
|
232
240
|
|
|
233
|
-
function
|
|
241
|
+
function getObjectExpression(expression) {
|
|
234
242
|
let objectExpression;
|
|
235
243
|
|
|
236
|
-
if (
|
|
237
|
-
objectExpression =
|
|
244
|
+
if (expression.type === 'ObjectExpression') {
|
|
245
|
+
objectExpression = expression;
|
|
238
246
|
}
|
|
239
247
|
|
|
240
|
-
if (
|
|
241
|
-
if (
|
|
242
|
-
const returnStatement =
|
|
248
|
+
if (expression.type === 'ArrowFunctionExpression') {
|
|
249
|
+
if (expression.body.type === 'BlockStatement') {
|
|
250
|
+
const returnStatement = expression.body.body.find(b => b.type === 'ReturnStatement');
|
|
243
251
|
objectExpression = returnStatement.argument;
|
|
244
252
|
}
|
|
245
253
|
|
|
246
|
-
if (
|
|
247
|
-
|
|
248
|
-
objectExpression =
|
|
254
|
+
if (expression.body.type === 'ObjectExpression') {
|
|
255
|
+
expression.body.extra.parenthesized = false;
|
|
256
|
+
objectExpression = expression.body;
|
|
249
257
|
}
|
|
250
258
|
}
|
|
251
259
|
|
|
252
|
-
if (
|
|
253
|
-
|
|
254
|
-
const returnStatement =
|
|
260
|
+
if (expression.type === 'FunctionDeclaration') {
|
|
261
|
+
expression.type = 'FunctionExpression';
|
|
262
|
+
const returnStatement = expression.body.body.find(b => b.type === 'ReturnStatement');
|
|
255
263
|
objectExpression = returnStatement.argument;
|
|
256
264
|
}
|
|
257
265
|
|
|
266
|
+
return objectExpression;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
const stylesCount = {};
|
|
270
|
+
/**
|
|
271
|
+
*
|
|
272
|
+
* @param {import('jscodeshift').ObjectExpression | import('jscodeshift').ArrowFunctionExpression | import('jscodeshift').FunctionDeclaration} functionExpression
|
|
273
|
+
* @param {string[]} rootKeys
|
|
274
|
+
* @param {import('jscodeshift').ObjectExpression | import('jscodeshift').ArrowFunctionExpression | import('jscodeshift').FunctionDeclaration} prevStyleArg
|
|
275
|
+
*/
|
|
276
|
+
|
|
277
|
+
function convertToStyledArg(functionExpression, rootKeys = [], prevStyleArg) {
|
|
278
|
+
const objectExpression = getObjectExpression(functionExpression);
|
|
279
|
+
|
|
258
280
|
if (objectExpression) {
|
|
259
281
|
objectExpression.properties.forEach(prop => {
|
|
260
|
-
|
|
282
|
+
if (!stylesCount[prop.key.name]) {
|
|
283
|
+
stylesCount[prop.key.name] = 1;
|
|
284
|
+
} else {
|
|
285
|
+
stylesCount[prop.key.name] += 1;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
const resolvedKey = stylesCount[prop.key.name] === 1 ? prop.key.name : `${prop.key.name}${stylesCount[prop.key.name]}`;
|
|
289
|
+
const selector = rootKeys.includes(resolvedKey) ? '&.' : '& .';
|
|
261
290
|
prop.key = j.templateLiteral([j.templateElement({
|
|
262
291
|
raw: selector,
|
|
263
292
|
cooked: selector
|
|
264
293
|
}, false), j.templateElement({
|
|
265
294
|
raw: '',
|
|
266
295
|
cooked: ''
|
|
267
|
-
}, true)], [j.identifier(`classes.${
|
|
296
|
+
}, true)], [j.identifier(`classes.${resolvedKey}`)]);
|
|
268
297
|
prop.computed = true;
|
|
269
298
|
return prop;
|
|
270
299
|
});
|
|
@@ -282,6 +311,22 @@ function transformer(file, api, options) {
|
|
|
282
311
|
});
|
|
283
312
|
}
|
|
284
313
|
|
|
314
|
+
if (prevStyleArg) {
|
|
315
|
+
const prevObjectExpression = getObjectExpression(prevStyleArg);
|
|
316
|
+
|
|
317
|
+
if (objectExpression) {
|
|
318
|
+
// merge object
|
|
319
|
+
prevObjectExpression.properties = [...prevObjectExpression.properties, ...objectExpression.properties];
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
if (functionExpression.params && prevStyleArg.type === 'ObjectExpression') {
|
|
323
|
+
// turn prevStyleArg to ArrowFunction
|
|
324
|
+
prevStyleArg = j.arrowFunctionExpression(functionExpression.params, prevStyleArg);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
return prevStyleArg;
|
|
328
|
+
}
|
|
329
|
+
|
|
285
330
|
return functionExpression;
|
|
286
331
|
}
|
|
287
332
|
|
|
@@ -326,6 +371,8 @@ function transformer(file, api, options) {
|
|
|
326
371
|
const prefix = getPrefix(withStylesCall || makeStylesCall);
|
|
327
372
|
const rootClassKeys = getRootClassKeys();
|
|
328
373
|
const result = {};
|
|
374
|
+
const componentClassesCount = {};
|
|
375
|
+
const withStylesComponents = [];
|
|
329
376
|
|
|
330
377
|
if (withStylesCall) {
|
|
331
378
|
let stylesFnName;
|
|
@@ -333,7 +380,7 @@ function transformer(file, api, options) {
|
|
|
333
380
|
callee: {
|
|
334
381
|
name: 'withStyles'
|
|
335
382
|
}
|
|
336
|
-
}).
|
|
383
|
+
}).forEach(path => {
|
|
337
384
|
const arg = path.node.arguments[0];
|
|
338
385
|
|
|
339
386
|
if (arg.type === 'Identifier') {
|
|
@@ -343,15 +390,33 @@ function transformer(file, api, options) {
|
|
|
343
390
|
const objectExpression = getReturnStatement(arg);
|
|
344
391
|
|
|
345
392
|
if (objectExpression) {
|
|
346
|
-
|
|
347
|
-
|
|
393
|
+
// do this first, because objectExpression will be mutated in `createClasses` below.
|
|
394
|
+
if (path.parent.parent && path.parent.parent.node.id) {
|
|
395
|
+
// save withStylesComponent name, to add classes on JSX
|
|
396
|
+
withStylesComponents.push({
|
|
397
|
+
variableName: path.parent.parent.node.id.name,
|
|
398
|
+
classes: j.objectExpression(objectExpression.properties.map(prop => {
|
|
399
|
+
if (!componentClassesCount[prop.key.name]) {
|
|
400
|
+
componentClassesCount[prop.key.name] = 1;
|
|
401
|
+
} else {
|
|
402
|
+
componentClassesCount[prop.key.name] += 1;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
const resolvedKey = componentClassesCount[prop.key.name] === 1 ? prop.key.name : `${prop.key.name}${componentClassesCount[prop.key.name]}`;
|
|
406
|
+
return j.property('init', j.identifier(prop.key.name), j.memberExpression(j.identifier('classes'), j.identifier(resolvedKey)));
|
|
407
|
+
}))
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
result.classes = createClasses(objectExpression, result.classes);
|
|
412
|
+
result.styledArg = convertToStyledArg(arg, rootClassKeys, result.styledArg);
|
|
348
413
|
}
|
|
349
414
|
});
|
|
350
415
|
root.find(j.VariableDeclarator, {
|
|
351
416
|
id: {
|
|
352
417
|
name: stylesFnName
|
|
353
418
|
}
|
|
354
|
-
}).
|
|
419
|
+
}).forEach(path => {
|
|
355
420
|
let fnArg = path.node.init;
|
|
356
421
|
const objectExpression = getReturnStatement(fnArg);
|
|
357
422
|
|
|
@@ -370,7 +435,7 @@ function transformer(file, api, options) {
|
|
|
370
435
|
}
|
|
371
436
|
|
|
372
437
|
if (objectExpression) {
|
|
373
|
-
result.classes = createClasses(objectExpression,
|
|
438
|
+
result.classes = createClasses(objectExpression, result.classes);
|
|
374
439
|
result.styledArg = convertToStyledArg(fnArg, rootClassKeys);
|
|
375
440
|
}
|
|
376
441
|
}).remove();
|
|
@@ -378,9 +443,9 @@ function transformer(file, api, options) {
|
|
|
378
443
|
id: {
|
|
379
444
|
name: stylesFnName
|
|
380
445
|
}
|
|
381
|
-
}).
|
|
446
|
+
}).forEach(path => {
|
|
382
447
|
const returnStatement = path.node.body.body.find(b => b.type === 'ReturnStatement');
|
|
383
|
-
result.classes = createClasses(returnStatement.argument,
|
|
448
|
+
result.classes = createClasses(returnStatement.argument, result.classes);
|
|
384
449
|
result.styledArg = convertToStyledArg(path.node, rootClassKeys);
|
|
385
450
|
}).remove();
|
|
386
451
|
}
|
|
@@ -415,7 +480,7 @@ function transformer(file, api, options) {
|
|
|
415
480
|
}
|
|
416
481
|
|
|
417
482
|
if (objectExpression) {
|
|
418
|
-
result.classes = createClasses(objectExpression,
|
|
483
|
+
result.classes = createClasses(objectExpression, result.classes);
|
|
419
484
|
result.styledArg = convertToStyledArg(arg, rootClassKeys);
|
|
420
485
|
}
|
|
421
486
|
});
|
|
@@ -427,7 +492,7 @@ function transformer(file, api, options) {
|
|
|
427
492
|
const objectExpression = getReturnStatement(path.node.init);
|
|
428
493
|
|
|
429
494
|
if (objectExpression) {
|
|
430
|
-
result.classes = createClasses(objectExpression,
|
|
495
|
+
result.classes = createClasses(objectExpression, result.classes);
|
|
431
496
|
result.styledArg = convertToStyledArg(path.node.init, rootClassKeys);
|
|
432
497
|
}
|
|
433
498
|
}).remove();
|
|
@@ -437,7 +502,7 @@ function transformer(file, api, options) {
|
|
|
437
502
|
}
|
|
438
503
|
}).at(0).forEach(path => {
|
|
439
504
|
const returnStatement = path.node.body.body.find(b => b.type === 'ReturnStatement');
|
|
440
|
-
result.classes = createClasses(returnStatement.argument,
|
|
505
|
+
result.classes = createClasses(returnStatement.argument, result.classes);
|
|
441
506
|
result.styledArg = convertToStyledArg(path.node, rootClassKeys);
|
|
442
507
|
}).remove();
|
|
443
508
|
root.find(j.VariableDeclaration).filter(path => path.node.declarations.some(d => d.id.name === 'useStyles')).remove();
|
|
@@ -500,10 +565,28 @@ function transformer(file, api, options) {
|
|
|
500
565
|
root.findJSXElements(rootJsxName).at(0).forEach(transformJsxRootToStyledComponent);
|
|
501
566
|
}
|
|
502
567
|
/**
|
|
503
|
-
*
|
|
568
|
+
* Attach classes to components created by withStyles
|
|
569
|
+
* ex. const Button1 = withStyles(...)(Button)
|
|
504
570
|
*/
|
|
505
571
|
|
|
506
572
|
|
|
573
|
+
withStylesComponents.forEach(data => {
|
|
574
|
+
root.find(j.JSXOpeningElement, {
|
|
575
|
+
name: {
|
|
576
|
+
name: data.variableName
|
|
577
|
+
}
|
|
578
|
+
}).forEach(path => {
|
|
579
|
+
if (!path.node.attributes) {
|
|
580
|
+
path.node.attributes = [];
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
path.node.attributes.push(j.jsxAttribute(j.jsxIdentifier('classes'), j.jsxExpressionContainer(data.classes)));
|
|
584
|
+
});
|
|
585
|
+
});
|
|
586
|
+
/**
|
|
587
|
+
* import styled if not exist
|
|
588
|
+
*/
|
|
589
|
+
|
|
507
590
|
const imports = root.find(j.ImportDeclaration).filter(({
|
|
508
591
|
node
|
|
509
592
|
}) => node.source.value.match(/^@material-ui\/core\/styles$/)).forEach(({
|
|
@@ -524,8 +607,21 @@ function transformer(file, api, options) {
|
|
|
524
607
|
*/
|
|
525
608
|
|
|
526
609
|
|
|
527
|
-
root.find(j.ImportDeclaration).filter(path => path.node.source.value.match(
|
|
610
|
+
root.find(j.ImportDeclaration).filter(path => path.node.source.value.match(/^(@material-ui|@mui)\/styles\/?(withStyles|makeStyles|createStyles)?$/)).forEach(path => {
|
|
528
611
|
path.node.specifiers = path.node.specifiers.filter(s => s.local.name !== 'withStyles' && s.local.name !== 'makeStyles' && s.local.name !== 'createStyles');
|
|
529
612
|
}).filter(path => !path.node.specifiers.length).remove();
|
|
613
|
+
/**
|
|
614
|
+
* remove withStyles calls that create new component
|
|
615
|
+
*/
|
|
616
|
+
|
|
617
|
+
root.find(j.CallExpression, {
|
|
618
|
+
callee: {
|
|
619
|
+
name: 'withStyles'
|
|
620
|
+
}
|
|
621
|
+
}).forEach(path => {
|
|
622
|
+
if (path.parent.parent.parent.node.type === 'VariableDeclaration' && path.parent.parent.parent.parent.node.type !== 'ExportNamedDeclaration' && path.parent.node.arguments[0].type === 'Identifier') {
|
|
623
|
+
path.parent.parent.node.init = j.identifier(path.parent.node.arguments[0].name);
|
|
624
|
+
}
|
|
625
|
+
});
|
|
530
626
|
return root.toSource(printOptions).replace(/withStyles\([^)]*\),?/gm, '').replace(/([^=]{.*)classes[^.],?(.*})/gm, '$1$2').replace(/^.*useStyles(.*);?/gm, '');
|
|
531
627
|
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.Test = void 0;
|
|
9
|
+
|
|
10
|
+
var _react = _interopRequireDefault(require("react"));
|
|
11
|
+
|
|
12
|
+
var _Button = _interopRequireDefault(require("@mui/material/Button"));
|
|
13
|
+
|
|
14
|
+
var _withStyles = _interopRequireDefault(require("@mui/styles/withStyles"));
|
|
15
|
+
|
|
16
|
+
var _jsxRuntime = require("react/jsx-runtime");
|
|
17
|
+
|
|
18
|
+
var _React$Fragment;
|
|
19
|
+
|
|
20
|
+
const Button1 = (0, _withStyles.default)({
|
|
21
|
+
root: {
|
|
22
|
+
backgroundColor: 'red'
|
|
23
|
+
}
|
|
24
|
+
})(_Button.default);
|
|
25
|
+
const Button2 = (0, _withStyles.default)(theme => ({
|
|
26
|
+
root: {
|
|
27
|
+
backgroundColor: theme.palette.primary.main
|
|
28
|
+
},
|
|
29
|
+
actions: {
|
|
30
|
+
padding: theme.spacing(1)
|
|
31
|
+
}
|
|
32
|
+
}))(_Button.default);
|
|
33
|
+
const Button3 = (0, _withStyles.default)({
|
|
34
|
+
root: {
|
|
35
|
+
backgroundColor: 'blue'
|
|
36
|
+
},
|
|
37
|
+
actions: {
|
|
38
|
+
padding: '0px'
|
|
39
|
+
}
|
|
40
|
+
})(_Button.default);
|
|
41
|
+
|
|
42
|
+
const Test = () => _React$Fragment || (_React$Fragment = /*#__PURE__*/(0, _jsxRuntime.jsxs)(_react.default.Fragment, {
|
|
43
|
+
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(Button1, {}), /*#__PURE__*/(0, _jsxRuntime.jsx)(Button2, {}), /*#__PURE__*/(0, _jsxRuntime.jsx)(Button3, {})]
|
|
44
|
+
}));
|
|
45
|
+
|
|
46
|
+
exports.Test = Test;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.Test = void 0;
|
|
9
|
+
|
|
10
|
+
var _react = _interopRequireDefault(require("react"));
|
|
11
|
+
|
|
12
|
+
var _styles = require("@mui/material/styles");
|
|
13
|
+
|
|
14
|
+
var _Button = _interopRequireDefault(require("@mui/material/Button"));
|
|
15
|
+
|
|
16
|
+
var _jsxRuntime = require("react/jsx-runtime");
|
|
17
|
+
|
|
18
|
+
const PREFIX = 'Test';
|
|
19
|
+
const classes = {
|
|
20
|
+
root: `${PREFIX}-root`,
|
|
21
|
+
root2: `${PREFIX}-root2`,
|
|
22
|
+
actions: `${PREFIX}-actions`,
|
|
23
|
+
root3: `${PREFIX}-root3`,
|
|
24
|
+
actions2: `${PREFIX}-actions2`
|
|
25
|
+
}; // TODO jss-to-styled codemod: The Fragment root was replaced by div. Change the tag if needed.
|
|
26
|
+
|
|
27
|
+
const Root = (0, _styles.styled)('div')(({
|
|
28
|
+
theme
|
|
29
|
+
}) => ({
|
|
30
|
+
[`& .${classes.root}`]: {
|
|
31
|
+
backgroundColor: 'red'
|
|
32
|
+
},
|
|
33
|
+
[`& .${classes.root2}`]: {
|
|
34
|
+
backgroundColor: theme.palette.primary.main
|
|
35
|
+
},
|
|
36
|
+
[`& .${classes.actions}`]: {
|
|
37
|
+
padding: theme.spacing(1)
|
|
38
|
+
},
|
|
39
|
+
[`& .${classes.root3}`]: {
|
|
40
|
+
backgroundColor: 'blue'
|
|
41
|
+
},
|
|
42
|
+
[`& .${classes.actions2}`]: {
|
|
43
|
+
padding: '0px'
|
|
44
|
+
}
|
|
45
|
+
}));
|
|
46
|
+
const Button1 = _Button.default;
|
|
47
|
+
const Button2 = _Button.default;
|
|
48
|
+
const Button3 = _Button.default;
|
|
49
|
+
|
|
50
|
+
const Test = () => /*#__PURE__*/(0, _jsxRuntime.jsxs)(Root, {
|
|
51
|
+
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(Button1, {
|
|
52
|
+
classes: {
|
|
53
|
+
root: classes.root
|
|
54
|
+
}
|
|
55
|
+
}), /*#__PURE__*/(0, _jsxRuntime.jsx)(Button2, {
|
|
56
|
+
classes: {
|
|
57
|
+
root: classes.root2,
|
|
58
|
+
actions: classes.actions
|
|
59
|
+
}
|
|
60
|
+
}), /*#__PURE__*/(0, _jsxRuntime.jsx)(Button3, {
|
|
61
|
+
classes: {
|
|
62
|
+
root: classes.root3,
|
|
63
|
+
actions: classes.actions2
|
|
64
|
+
}
|
|
65
|
+
})]
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
exports.Test = Test;
|
|
@@ -9,5 +9,5 @@ exports.default = transformer;
|
|
|
9
9
|
* @param {import('jscodeshift').FileInfo} file
|
|
10
10
|
*/
|
|
11
11
|
function transformer(file) {
|
|
12
|
-
return file.source.replace(/@material-ui\/unstyled/gm, '@mui/
|
|
12
|
+
return file.source.replace(/@material-ui\/unstyled/gm, '@mui/base').replace(/@material-ui\/core/gm, '@mui/material').replace(/@material-ui\/icons/gm, '@mui/icons-material').replace(/@material-ui\/(?!(pickers|data-grid|x-grid))/gm, '@mui/');
|
|
13
13
|
}
|
|
@@ -14,9 +14,9 @@ var _styles2 = require("@mui/styles");
|
|
|
14
14
|
|
|
15
15
|
var _createStyles = _interopRequireDefault(require("@mui/styles/createStyles"));
|
|
16
16
|
|
|
17
|
-
var
|
|
17
|
+
var _base = require("@mui/base");
|
|
18
18
|
|
|
19
|
-
var _SwitchUnstyled = _interopRequireDefault(require("@mui/
|
|
19
|
+
var _SwitchUnstyled = _interopRequireDefault(require("@mui/base/SwitchUnstyled"));
|
|
20
20
|
|
|
21
21
|
var _system = require("@mui/system");
|
|
22
22
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mui/codemod",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.2.1",
|
|
4
4
|
"bin": "./codemod.js",
|
|
5
5
|
"private": false,
|
|
6
6
|
"author": "MUI Team",
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"keywords": [
|
|
9
9
|
"react",
|
|
10
10
|
"react-component",
|
|
11
|
-
"
|
|
11
|
+
"mui",
|
|
12
12
|
"codemod",
|
|
13
13
|
"jscodeshift"
|
|
14
14
|
],
|
|
@@ -26,15 +26,15 @@
|
|
|
26
26
|
"license": "MIT",
|
|
27
27
|
"homepage": "https://github.com/mui-org/material-ui/tree/master/packages/mui-codemod",
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@babel/core": "^7.
|
|
30
|
-
"@babel/runtime": "^7.
|
|
31
|
-
"@babel/traverse": "^7.
|
|
29
|
+
"@babel/core": "^7.16.0",
|
|
30
|
+
"@babel/runtime": "^7.16.3",
|
|
31
|
+
"@babel/traverse": "^7.16.3",
|
|
32
32
|
"jscodeshift": "^0.13.0",
|
|
33
33
|
"jscodeshift-add-imports": "^1.0.10",
|
|
34
34
|
"yargs": "^17.2.1"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
|
-
"@types/jscodeshift": "0.11.
|
|
37
|
+
"@types/jscodeshift": "0.11.3"
|
|
38
38
|
},
|
|
39
39
|
"sideEffects": false,
|
|
40
40
|
"publishConfig": {
|