@mui/codemod 5.8.4 → 5.9.3

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
@@ -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
 
@@ -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 = [];
@@ -246,6 +260,8 @@ function transformer(file, api, options) {
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
  }
@@ -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;
@@ -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: {
@@ -5,13 +5,19 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = transformer;
7
7
 
8
+ /* eslint-disable no-template-curly-in-string */
9
+
8
10
  /**
9
11
  * @param {import('jscodeshift').FileInfo} file
10
12
  */
11
13
  function transformer(file) {
12
- return file.source // `${theme.spacing(2)}px` -> theme.spacing(2)
13
- .replace(/`(-?)\${(-?)(theme\.spacing|spacing)\(([^{}]*)\)}px`/gm, '$3($1$2$4)') // `${theme.spacing(2)}px ${theme.spacing(4)}px` -> `${theme.spacing(2)} ${theme.spacing(4)}`
14
- .replace(/(?<={(theme\.spacing|spacing)\([^]*.*[^]*\)})px/gm, '').replace(/((theme\.spacing|spacing)\(.*\))\s*\+\s*'px'/gm, '$1').replace(/\${(theme.spacing|spacing)(\(.*\))\s*([+-])\s*([\d.]+)\s*}px/gm, // eslint-disable-next-line no-template-curly-in-string
15
- 'calc(${$1$2} $3 $4px)').replace(/\${(theme.spacing|spacing)(\(.*\))\s*([*/])\s*([\d.]+)\s*}px/gm, // eslint-disable-next-line no-template-curly-in-string
16
- 'calc(${$1$2} $3 $4)');
14
+ return file.source.replace( // handle cases like: `-${theme.spacing(1)}px`
15
+ /`(-?)\${(-?)(theme\.spacing|spacing)\(([^{}]*)\)}px`/gm, '$3($1$2$4)').replace( // handle cases like: theme.spacing(gap) + 'px'
16
+ /((theme\.spacing|spacing)\(.*\))\s*\+\s*'px'/gm, '$1').replace( // handle cases like: theme.spacing(gap) + "px"
17
+ /((theme\.spacing|spacing)\(.*\))\s*\+\s*"px"/gm, '$1').replace( // handle cases like: `calc(${theme.spacing(2)} - 1px) 0`
18
+ /\${(theme\.spacing|spacing)(\([^)]+\))\s*([+-])\s*([\d.]+)\s*}px/gm, 'calc(${$1$2} $3 $4px)').replace( // handle cases like: calc(${theme.spacing(itemHorzPadding)} * 0.3)
19
+ /\${(theme\.spacing|spacing)(\([^)]+\))\s*([*/])\s*([\d.]+)\s*}px/gm, 'calc(${$1$2} $3 $4)').replace( // handle common cases like:
20
+ // `${theme.spacing(2)}px`
21
+ // `${theme.spacing(2)}px ${theme.spacing(1)}px ${theme.spacing(2)}px ${theme.spacing(2)}px`
22
+ /(spacing\([^)]+\)\})px(.)/gm, '$1$2');
17
23
  }
@@ -2,6 +2,8 @@
2
2
 
3
3
  `${theme.spacing(2)}px``${spacing(2)}px``${theme.spacing(1 / 2)}px``${theme.spacing(0.5)}px``${theme.spacing(1 / 2)}px ${theme.spacing(4)}px`;
4
4
  theme.spacing(gap) + 'px';
5
- spacing(gap) + 'px'`calc(100% - ${spacing(itemHorzPadding * 2)}px)``calc(100% - ${theme.spacing(itemHorzPadding * 2)}px)`;
5
+ spacing(gap) + 'px';
6
+ theme.spacing(gap) + "px";
7
+ spacing(gap) + "px"`calc(100% - ${spacing(itemHorzPadding * 2)}px)``calc(100% - ${theme.spacing(itemHorzPadding * 2)}px)`;
6
8
 
7
9
  padding: `${theme.spacing(2) - 1}px 0``calc(100% - ${theme.spacing(itemHorzPadding) * 0.3}px)``${-theme.spacing(1)}px``-${theme.spacing(1)}px``${theme.spacing(2)}px ${theme.spacing(1)}px ${theme.spacing(2)}px ${theme.spacing(2)}px`;
@@ -5,6 +5,8 @@ spacing(2);
5
5
  theme.spacing(1 / 2);
6
6
  theme.spacing(0.5)`${theme.spacing(1 / 2)} ${theme.spacing(4)}`;
7
7
  theme.spacing(gap);
8
+ spacing(gap);
9
+ theme.spacing(gap);
8
10
  spacing(gap)`calc(100% - ${spacing(itemHorzPadding * 2)})``calc(100% - ${theme.spacing(itemHorzPadding * 2)})`;
9
11
 
10
12
  padding: `calc(${theme.spacing(2)} - 1px) 0``calc(100% - calc(${theme.spacing(itemHorzPadding)} * 0.3))`;