@mui/codemod 5.8.3 → 5.8.7

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 CHANGED
@@ -437,7 +437,7 @@ You can find more details about this breaking change in [the migration guide](ht
437
437
 
438
438
  #### `emotion-prepend-cache`
439
439
 
440
- Adds `prepend: true` to emotion `createCache`
440
+ Adds `prepend: true` to Emotion `createCache`
441
441
 
442
442
  ```diff
443
443
  const cache = emotionCreateCache({
@@ -685,7 +685,7 @@ The following scenarios are not currently handled by this codemod and will be ma
685
685
  - If an arrow function at the rule level contains a code block (i.e. contains an explicit `return`
686
686
  statement) rather than just an object expression, it will not be converted.
687
687
 
688
- You can find more details about migrating from JSS to tss-react in [the migration guide](https://mui.com/migration/migrating-from-jss/#2-use-tss-react).
688
+ You can find more details about migrating from JSS to tss-react in [the migration guide](https://mui.com/material-ui/migration/migrating-from-jss/#2-use-tss-react).
689
689
 
690
690
  #### `link-underline-hover`
691
691
 
package/codemod.js CHANGED
File without changes
@@ -172,6 +172,18 @@ function transformStylesExpression(j, comments, stylesExpression, nestedKeys, se
172
172
  }
173
173
  }
174
174
 
175
+ function addCommentsToNode(node, commentsToAdd, addToBeginning = false) {
176
+ if (!node.comments) {
177
+ node.comments = [];
178
+ }
179
+
180
+ if (addToBeginning) {
181
+ node.comments.unshift(...commentsToAdd);
182
+ } else {
183
+ node.comments.push(...commentsToAdd);
184
+ }
185
+ }
186
+
175
187
  function addCommentsToDeclaration(declaration, commentsToAdd) {
176
188
  let commentsPath = declaration;
177
189
 
@@ -179,18 +191,18 @@ function addCommentsToDeclaration(declaration, commentsToAdd) {
179
191
  commentsPath = declaration.parentPath;
180
192
  }
181
193
 
182
- if (!commentsPath.node.comments) {
183
- commentsPath.node.comments = [];
184
- }
185
-
186
- commentsPath.node.comments.push(...commentsToAdd);
194
+ addCommentsToNode(commentsPath.node, commentsToAdd);
187
195
  }
188
196
 
189
- function addComments(j, path, commentsToAdd) {
197
+ function addCommentsToClosestDeclaration(j, path, commentsToAdd) {
190
198
  j(path).closest(j.VariableDeclaration).forEach(declaration => {
191
199
  addCommentsToDeclaration(declaration, commentsToAdd);
192
200
  });
193
201
  }
202
+
203
+ function getFirstNode(j, root) {
204
+ return root.find(j.Program).get('body', 0).node;
205
+ }
194
206
  /**
195
207
  * @param {import('jscodeshift').FileInfo} file
196
208
  * @param {import('jscodeshift').API} api
@@ -203,6 +215,7 @@ function transformer(file, api, options) {
203
215
  const printOptions = options.printOptions || {
204
216
  quote: 'single'
205
217
  };
218
+ const originalFirstNode = getFirstNode(j, root);
206
219
  let importsChanged = false;
207
220
  let foundCreateStyles = false;
208
221
  let foundMakeStyles = false;
@@ -213,6 +226,7 @@ function transformer(file, api, options) {
213
226
 
214
227
  root.find(j.ImportDeclaration).forEach(path => {
215
228
  const importSource = path.node.source.value;
229
+ const originalComments = path.node.comments;
216
230
 
217
231
  if (importSource === '@material-ui/core/styles' || importSource === '@material-ui/core' || importSource === '@mui/styles') {
218
232
  const specifiersToMove = [];
@@ -237,15 +251,17 @@ function transformer(file, api, options) {
237
251
  path.replace(j.importDeclaration(specifiersToMove, j.stringLiteral('tss-react/mui')), specifiersToStay.length > 0 ? j.importDeclaration(specifiersToStay, j.stringLiteral(importSource)) : undefined);
238
252
  importsChanged = true;
239
253
  }
240
- } else if (importSource === '@material-ui/styles/makeStyles') {
254
+ } else if (importSource === '@material-ui/styles/makeStyles' || importSource === '@mui/styles/makeStyles') {
241
255
  foundMakeStyles = true;
242
256
  path.replace(j.importDeclaration([j.importSpecifier(j.identifier('makeStyles'))], j.stringLiteral('tss-react/mui')));
243
257
  importsChanged = true;
244
- } else if (importSource === '@material-ui/styles/withStyles') {
258
+ } else if (importSource === '@material-ui/styles/withStyles' || importSource === '@mui/styles/withStyles') {
245
259
  foundWithStyles = true;
246
260
  path.replace(j.importDeclaration([j.importSpecifier(j.identifier('withStyles'))], j.stringLiteral('tss-react/mui')));
247
261
  importsChanged = true;
248
262
  }
263
+
264
+ path.node.comments = originalComments;
249
265
  });
250
266
 
251
267
  if (!importsChanged) {
@@ -265,7 +281,19 @@ function transformer(file, api, options) {
265
281
  clsxOrClassnamesName = specifier.local.name;
266
282
  }
267
283
  });
284
+ let commentsToPreserve = null;
285
+
286
+ if (originalFirstNode === path.node) {
287
+ // For a removed import, only preserve the comments if it is the first node in the file,
288
+ // otherwise the comments are probably about the removed import and no longer relevant.
289
+ commentsToPreserve = path.node.comments;
290
+ }
291
+
268
292
  j(path).remove();
293
+
294
+ if (commentsToPreserve) {
295
+ addCommentsToNode(getFirstNode(j, root), commentsToPreserve, true);
296
+ }
269
297
  }
270
298
  });
271
299
  /**
@@ -306,7 +334,7 @@ function transformer(file, api, options) {
306
334
  transformStylesExpression(j, commentsToAdd, path.node.arguments[0], nestedKeys, newStylesExpression => {
307
335
  stylesExpression = newStylesExpression;
308
336
  });
309
- addComments(j, path, commentsToAdd);
337
+ addCommentsToClosestDeclaration(j, path, commentsToAdd);
310
338
  let makeStylesIdentifier = 'makeStyles';
311
339
 
312
340
  if (isTypeScript && (nestedKeys.length > 0 || paramsTypes !== null)) {
@@ -331,7 +359,7 @@ function transformer(file, api, options) {
331
359
  styleHooks.push(path.node.id.name);
332
360
  j(path).closest(j.ExportNamedDeclaration).forEach(() => {
333
361
  const comments = [j.commentLine(` TODO jss-to-tss-react codemod: usages of this hook outside of this file will not be converted.`, true)];
334
- addComments(j, path, comments);
362
+ addCommentsToClosestDeclaration(j, path, comments);
335
363
  });
336
364
  });
337
365
  /**
@@ -433,7 +461,7 @@ function transformer(file, api, options) {
433
461
  transformStylesExpression(j, commentsToAdd, styles, nestedKeys, newStylesExpression => {
434
462
  path.node.callee.arguments[0] = newStylesExpression;
435
463
  });
436
- addComments(j, path, commentsToAdd);
464
+ addCommentsToClosestDeclaration(j, path, commentsToAdd);
437
465
  }
438
466
 
439
467
  const component = path.node.arguments[0];
@@ -451,7 +479,7 @@ function transformer(file, api, options) {
451
479
  transformStylesExpression(j, commentsToAdd, path.node.init, nestedKeys, newStylesExpression => {
452
480
  path.node.init = newStylesExpression;
453
481
  });
454
- addComments(j, path, commentsToAdd);
482
+ addCommentsToClosestDeclaration(j, path, commentsToAdd);
455
483
  });
456
484
  });
457
485
  }
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.default = ComponentUsingStyles;
9
+
10
+ var _react = _interopRequireDefault(require("react"));
11
+
12
+ var _makeStyles = _interopRequireDefault(require("@mui/styles/makeStyles"));
13
+
14
+ var _jsxRuntime = require("react/jsx-runtime");
15
+
16
+ var _InnerComponent;
17
+
18
+ const useStyles = (0, _makeStyles.default)({
19
+ test: {
20
+ backgroundColor: "purple",
21
+ color: "white"
22
+ }
23
+ });
24
+ const useStyles2 = (0, _makeStyles.default)({
25
+ test: {
26
+ backgroundColor: "purple",
27
+ color: "white",
28
+ "& $test2": {
29
+ backgroundColor: "lime",
30
+ color: "blue"
31
+ }
32
+ },
33
+ test2: {
34
+ backgroundColor: "blue",
35
+ color: "lime"
36
+ }
37
+ });
38
+
39
+ function InnerComponent() {
40
+ const classes = useStyles2();
41
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
42
+ className: classes.test2,
43
+ children: "Inner Test"
44
+ });
45
+ }
46
+
47
+ function ComponentUsingStyles(props) {
48
+ const classes = useStyles();
49
+ return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
50
+ className: classes.test,
51
+ children: ["Test", _InnerComponent || (_InnerComponent = /*#__PURE__*/(0, _jsxRuntime.jsx)(InnerComponent, {}))]
52
+ });
53
+ }
@@ -9,14 +9,15 @@ exports.default = ComponentUsingStyles;
9
9
 
10
10
  var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
11
11
 
12
+ var _clsx = _interopRequireDefault(require("clsx"));
13
+
12
14
  var _react = _interopRequireDefault(require("react"));
13
15
 
14
16
  var _styles = require("@material-ui/core/styles");
15
17
 
16
- var _clsx = _interopRequireDefault(require("clsx"));
17
-
18
18
  var _jsxRuntime = require("react/jsx-runtime");
19
19
 
20
+ // Comment on first node where the first node will be removed and we should preserve this comment.
20
21
  function mixins() {
21
22
  return {
22
23
  test: {
@@ -7,12 +7,12 @@ Object.defineProperty(exports, "__esModule", {
7
7
  });
8
8
  exports.useExportedStyles = exports.default = void 0;
9
9
 
10
+ var _clsx = _interopRequireDefault(require("clsx"));
11
+
10
12
  var _react = _interopRequireDefault(require("react"));
11
13
 
12
14
  var _core = require("@material-ui/core");
13
15
 
14
- var _clsx = _interopRequireDefault(require("clsx"));
15
-
16
16
  var _jsxRuntime = require("react/jsx-runtime");
17
17
 
18
18
  var _InnerComponent;
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.default = ComponentUsingStyles;
9
+
10
+ var _react = _interopRequireDefault(require("react"));
11
+
12
+ var _mui = require("tss-react/mui");
13
+
14
+ var _jsxRuntime = require("react/jsx-runtime");
15
+
16
+ var _InnerComponent;
17
+
18
+ const useStyles = (0, _mui.makeStyles)()({
19
+ test: {
20
+ backgroundColor: "purple",
21
+ color: "white"
22
+ }
23
+ });
24
+ const useStyles2 = (0, _mui.makeStyles)()((_theme, _params, classes) => ({
25
+ test: {
26
+ backgroundColor: "purple",
27
+ color: "white",
28
+ [`& .${classes.test2}`]: {
29
+ backgroundColor: "lime",
30
+ color: "blue"
31
+ }
32
+ },
33
+ test2: {
34
+ backgroundColor: "blue",
35
+ color: "lime"
36
+ }
37
+ }));
38
+
39
+ function InnerComponent() {
40
+ const {
41
+ classes
42
+ } = useStyles2();
43
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
44
+ className: classes.test2,
45
+ children: "Inner Test"
46
+ });
47
+ }
48
+
49
+ function ComponentUsingStyles(props) {
50
+ const {
51
+ classes
52
+ } = useStyles();
53
+ return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
54
+ className: classes.test,
55
+ children: ["Test", _InnerComponent || (_InnerComponent = /*#__PURE__*/(0, _jsxRuntime.jsx)(InnerComponent, {}))]
56
+ });
57
+ }
@@ -15,6 +15,7 @@ var _mui = require("tss-react/mui");
15
15
 
16
16
  var _jsxRuntime = require("react/jsx-runtime");
17
17
 
18
+ // Comment on first node where the first node will be removed and we should preserve this comment.
18
19
  function mixins() {
19
20
  return {
20
21
  test: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mui/codemod",
3
- "version": "5.8.3",
3
+ "version": "5.8.7",
4
4
  "bin": "./codemod.js",
5
5
  "private": false,
6
6
  "author": "MUI Team",
@@ -35,7 +35,7 @@
35
35
  "@babel/traverse": "^7.17.3",
36
36
  "jscodeshift": "^0.13.1",
37
37
  "jscodeshift-add-imports": "^1.0.10",
38
- "yargs": "^17.4.1"
38
+ "yargs": "^17.5.1"
39
39
  },
40
40
  "devDependencies": {
41
41
  "@types/jscodeshift": "0.11.5"