@mui/codemod 5.15.9 → 5.15.11

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.
Files changed (31) hide show
  1. package/README.md +155 -3
  2. package/codemod.js +80 -7
  3. package/node/deprecations/accordion-summary-classes/accordion-summary-classes.js +55 -0
  4. package/node/deprecations/accordion-summary-classes/index.js +13 -0
  5. package/node/deprecations/accordion-summary-classes/postcss-plugin.js +28 -0
  6. package/node/deprecations/accordion-summary-classes/postcss.config.js +8 -0
  7. package/node/deprecations/accordion-summary-classes/test-cases/actual.js +54 -0
  8. package/node/deprecations/accordion-summary-classes/test-cases/expected.js +54 -0
  9. package/node/deprecations/alert-props/alert-props.js +22 -0
  10. package/node/deprecations/alert-props/index.js +13 -0
  11. package/node/deprecations/alert-props/test-cases/actual.js +43 -0
  12. package/node/deprecations/alert-props/test-cases/expected.js +33 -0
  13. package/node/deprecations/alert-props/test-cases/theme.actual.js +52 -0
  14. package/node/deprecations/alert-props/test-cases/theme.expected.js +42 -0
  15. package/node/deprecations/all/deprecations-all.js +9 -1
  16. package/node/deprecations/all/postcss.config.js +11 -0
  17. package/node/deprecations/avatar-props/avatar-props.js +58 -0
  18. package/node/deprecations/avatar-props/index.js +13 -0
  19. package/node/deprecations/avatar-props/test-cases/actual.js +26 -0
  20. package/node/deprecations/avatar-props/test-cases/expected.js +30 -0
  21. package/node/deprecations/avatar-props/test-cases/theme.actual.js +12 -0
  22. package/node/deprecations/avatar-props/test-cases/theme.expected.js +14 -0
  23. package/node/deprecations/pagination-item-classes/index.js +13 -0
  24. package/node/deprecations/pagination-item-classes/pagination-item-classes.js +58 -0
  25. package/node/deprecations/pagination-item-classes/postcss-plugin.js +39 -0
  26. package/node/deprecations/pagination-item-classes/postcss.config.js +8 -0
  27. package/node/deprecations/pagination-item-classes/test-cases/actual.js +108 -0
  28. package/node/deprecations/pagination-item-classes/test-cases/expected.js +108 -0
  29. package/node/deprecations/utils/replaceComponentsWithSlots.js +184 -0
  30. package/node/util/findComponentDefaultProps.js +30 -0
  31. package/package.json +3 -1
@@ -0,0 +1,184 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.default = replaceComponentsWithSlots;
8
+ var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
9
+ var _findComponentJSX = _interopRequireDefault(require("../../util/findComponentJSX"));
10
+ var _findComponentDefaultProps = _interopRequireDefault(require("../../util/findComponentDefaultProps"));
11
+ var _assignObject = _interopRequireDefault(require("../../util/assignObject"));
12
+ var _appendAttribute = _interopRequireDefault(require("../../util/appendAttribute"));
13
+ function componentsKeyToSlotsKey(str) {
14
+ return str[0].toLowerCase() + str.slice(1);
15
+ }
16
+ function replaceJsxComponentsProp(j, elementPath) {
17
+ const element = elementPath.node;
18
+ const index = element.openingElement.attributes.findIndex(attr => attr.type === 'JSXAttribute' && attr.name.name === 'components');
19
+ if (index !== -1) {
20
+ const removed = element.openingElement.attributes.splice(index, 1);
21
+ const camelCaseComponents = removed[0].value.expression.properties.reduce((acc, prop) => {
22
+ return (0, _extends2.default)({}, acc, {
23
+ [componentsKeyToSlotsKey(prop.key.name)]: prop.value
24
+ });
25
+ }, {});
26
+ let hasNode = false;
27
+ element.openingElement.attributes.forEach(attr => {
28
+ var _attr$name;
29
+ if (((_attr$name = attr.name) == null ? void 0 : _attr$name.name) === 'slots') {
30
+ hasNode = true;
31
+ const slots = attr.value.expression.properties.reduce((acc, prop) => {
32
+ return (0, _extends2.default)({}, acc, {
33
+ [prop.key.name]: prop.value
34
+ });
35
+ }, {});
36
+ Object.entries(camelCaseComponents).forEach(([slot, value]) => {
37
+ if (!slots[slot]) {
38
+ (0, _assignObject.default)(j, {
39
+ target: attr,
40
+ key: slot,
41
+ expression: value
42
+ });
43
+ }
44
+ });
45
+ }
46
+ });
47
+ if (!hasNode) {
48
+ (0, _appendAttribute.default)(j, {
49
+ target: element,
50
+ attributeName: 'slots',
51
+ expression: j.objectExpression(Object.entries(camelCaseComponents).map(([slot, value]) => {
52
+ return j.objectProperty(j.identifier(slot), value);
53
+ }))
54
+ });
55
+ }
56
+ }
57
+ }
58
+ function replaceJsxComponentsPropsProp(j, element) {
59
+ const index = element.openingElement.attributes.findIndex(attr => attr.type === 'JSXAttribute' && attr.name.name === 'componentsProps');
60
+ if (index !== -1) {
61
+ const removed = element.openingElement.attributes.splice(index, 1);
62
+ let hasNode = false;
63
+ element.openingElement.attributes.forEach(attr => {
64
+ var _attr$name2;
65
+ if (((_attr$name2 = attr.name) == null ? void 0 : _attr$name2.name) === 'slotProps') {
66
+ hasNode = true;
67
+ const slotProps = attr.value.expression.properties.reduce((acc, prop) => {
68
+ return (0, _extends2.default)({}, acc, {
69
+ [prop.key.name]: prop.value
70
+ });
71
+ }, {});
72
+ removed[0].value.expression.properties.forEach(prop => {
73
+ if (!slotProps[prop.key.name]) {
74
+ (0, _assignObject.default)(j, {
75
+ target: attr,
76
+ key: prop.key.name,
77
+ expression: prop.value
78
+ });
79
+ }
80
+ });
81
+ }
82
+ });
83
+ if (!hasNode) {
84
+ (0, _appendAttribute.default)(j, {
85
+ target: element,
86
+ attributeName: 'slotProps',
87
+ expression: removed[0].value.expression
88
+ });
89
+ }
90
+ }
91
+ }
92
+ function replaceDefaultPropsComponentsProp(j, defaultPropsPathCollection) {
93
+ defaultPropsPathCollection.find(j.ObjectProperty, {
94
+ key: {
95
+ name: 'components'
96
+ }
97
+ }).forEach(path => {
98
+ const {
99
+ properties: defaultPropsProperties
100
+ } = path.parent.value;
101
+ const components = path.value.value.properties.reduce((acc, prop) => {
102
+ return (0, _extends2.default)({}, acc, {
103
+ [componentsKeyToSlotsKey(prop.key.name)]: prop.value
104
+ });
105
+ }, {});
106
+ const existingSlots = defaultPropsProperties.find(prop => prop.key.name === 'slots');
107
+ const slots = existingSlots ? existingSlots.value.properties.reduce((acc, prop) => {
108
+ return (0, _extends2.default)({}, acc, {
109
+ [prop.key.name]: prop.value
110
+ });
111
+ }, {}) : {};
112
+ const updatedSlots = j.objectExpression(Object.entries((0, _extends2.default)({}, components, slots)).map(([slot, value]) => {
113
+ return j.objectProperty(j.identifier(slot), value);
114
+ }));
115
+ if (existingSlots) {
116
+ existingSlots.value = updatedSlots;
117
+ } else {
118
+ defaultPropsProperties.push(j.property('init', j.identifier('slots'), updatedSlots));
119
+ }
120
+ path.prune();
121
+ });
122
+ }
123
+ function replaceDefaultPropsComponentsPropsProp(j, defaultPropsPathCollection) {
124
+ defaultPropsPathCollection.find(j.ObjectProperty, {
125
+ key: {
126
+ name: 'componentsProps'
127
+ }
128
+ }).forEach(path => {
129
+ const {
130
+ properties: defaultPropsProperties
131
+ } = path.parent.value;
132
+ const components = path.value.value.properties.reduce((acc, prop) => {
133
+ return (0, _extends2.default)({}, acc, {
134
+ [prop.key.name]: prop.value
135
+ });
136
+ }, {});
137
+ const existingSlots = defaultPropsProperties.find(prop => prop.key.name === 'slotProps');
138
+ const slots = existingSlots ? existingSlots.value.properties.reduce((acc, prop) => {
139
+ return (0, _extends2.default)({}, acc, {
140
+ [prop.key.name]: prop.value
141
+ });
142
+ }, {}) : {};
143
+ const updatedSlots = j.objectExpression(Object.entries((0, _extends2.default)({}, components, slots)).map(([slot, value]) => {
144
+ return j.objectProperty(j.identifier(slot), value);
145
+ }));
146
+ if (existingSlots) {
147
+ existingSlots.value = updatedSlots;
148
+ } else {
149
+ defaultPropsProperties.push(j.property('init', j.identifier('slotProps'), updatedSlots));
150
+ }
151
+ path.prune();
152
+ });
153
+ }
154
+
155
+ /**
156
+ * Replaces components and componentsProps props with slots and slotProps.
157
+ * Handles local object and variable declaration.
158
+ * If the slots prop exists, it will add the components to the slots.
159
+ * If there are duplicated values, the slots values will be used.
160
+ *
161
+ * @param {import('jscodeshift')} j
162
+ * @param {{ element: import('jscodeshift').JSXElement }} options
163
+ *
164
+ * @example <Component componentsProps={{ root: { 'testid': 'root-id'} }} /> => <Component slotProps={{ root: { 'testid': 'root-id'} }} />
165
+ */
166
+ function replaceComponentsWithSlots(j, options) {
167
+ const {
168
+ root,
169
+ componentName
170
+ } = options;
171
+ (0, _findComponentJSX.default)(j, {
172
+ root,
173
+ componentName
174
+ }, elementPath => {
175
+ replaceJsxComponentsProp(j, elementPath);
176
+ replaceJsxComponentsPropsProp(j, elementPath.node);
177
+ });
178
+ const defaultPropsPathCollection = (0, _findComponentDefaultProps.default)(j, {
179
+ root,
180
+ componentName
181
+ });
182
+ replaceDefaultPropsComponentsProp(j, defaultPropsPathCollection);
183
+ replaceDefaultPropsComponentsPropsProp(j, defaultPropsPathCollection);
184
+ }
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = findComponentDefaultProps;
7
+ /**
8
+ * Find all the default props path of a given component name.
9
+ *
10
+ * @param {import('jscodeshift')} j
11
+ * @param {{ root: import('jscodeshift').Collection; componentName: string }} options
12
+ * @returns {import('jscodeshift').Collection}
13
+ *
14
+ */
15
+ function findComponentDefaultProps(j, options) {
16
+ const {
17
+ root,
18
+ componentName
19
+ } = options;
20
+ const defaultPropsPathCollection = root.find(j.ObjectProperty, {
21
+ key: {
22
+ name: `Mui${componentName}`
23
+ }
24
+ }).find(j.ObjectProperty, {
25
+ key: {
26
+ name: 'defaultProps'
27
+ }
28
+ });
29
+ return defaultPropsPathCollection;
30
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mui/codemod",
3
- "version": "5.15.9",
3
+ "version": "5.15.11",
4
4
  "bin": "./codemod.js",
5
5
  "private": false,
6
6
  "author": "MUI Team",
@@ -29,6 +29,8 @@
29
29
  "@babel/traverse": "^7.23.9",
30
30
  "jscodeshift": "^0.13.1",
31
31
  "jscodeshift-add-imports": "^1.0.10",
32
+ "postcss": "^8.4.33",
33
+ "postcss-cli": "^8.0.0",
32
34
  "yargs": "^17.7.2"
33
35
  },
34
36
  "devDependencies": {