@mui/codemod 7.2.0 → 7.3.0

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 (26) hide show
  1. package/CHANGELOG.md +794 -0
  2. package/README.md +21 -0
  3. package/package.json +30 -16
  4. package/v5.0.0/top-level-imports.js +1 -1
  5. package/v5.0.0/top-level-imports.test/actual.js +2 -2
  6. package/v5.0.0/top-level-imports.test/expected.js +2 -0
  7. package/v7.0.0/theme-color-functions/index.js +13 -0
  8. package/v7.0.0/theme-color-functions/test-cases/actual.js +21 -0
  9. package/v7.0.0/theme-color-functions/test-cases/darken-basic.actual.js +20 -0
  10. package/v7.0.0/theme-color-functions/test-cases/darken-basic.expected.js +19 -0
  11. package/v7.0.0/theme-color-functions/test-cases/expected.js +20 -0
  12. package/v7.0.0/theme-color-functions/test-cases/lighten-basic.actual.js +13 -0
  13. package/v7.0.0/theme-color-functions/test-cases/lighten-basic.expected.js +12 -0
  14. package/v7.0.0/theme-color-functions/test-cases/mixed-functions.actual.js +17 -0
  15. package/v7.0.0/theme-color-functions/test-cases/mixed-functions.expected.js +16 -0
  16. package/v7.0.0/theme-color-functions/test-cases/mixed-imports.actual.js +19 -0
  17. package/v7.0.0/theme-color-functions/test-cases/mixed-imports.expected.js +18 -0
  18. package/v7.0.0/theme-color-functions/test-cases/mui-material-styles.actual.js +18 -0
  19. package/v7.0.0/theme-color-functions/test-cases/mui-material-styles.expected.js +17 -0
  20. package/v7.0.0/theme-color-functions/test-cases/no-import.actual.js +11 -0
  21. package/v7.0.0/theme-color-functions/test-cases/no-import.expected.js +11 -0
  22. package/v7.0.0/theme-color-functions/test-cases/opacity-calc.actual.js +57 -0
  23. package/v7.0.0/theme-color-functions/test-cases/opacity-calc.expected.js +56 -0
  24. package/v7.0.0/theme-color-functions/test-cases/opacity-var.actual.js +37 -0
  25. package/v7.0.0/theme-color-functions/test-cases/opacity-var.expected.js +36 -0
  26. package/v7.0.0/theme-color-functions/theme-color-functions.js +142 -0
@@ -0,0 +1,142 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = transformer;
7
+ /**
8
+ * @param {import('jscodeshift').FileInfo} file
9
+ * @param {import('jscodeshift').API} api
10
+ */
11
+ function transformer(file, api, options) {
12
+ if (file.path?.endsWith('.d.ts')) {
13
+ return file.source;
14
+ }
15
+ const j = api.jscodeshift;
16
+ const root = j(file.source);
17
+ const printOptions = options.printOptions;
18
+
19
+ // Check if the file imports alpha, lighten, or darken from @mui/system/colorManipulator or @mui/material/styles
20
+ let hasAlphaImport = false;
21
+ let hasLightenImport = false;
22
+ let hasDarkenImport = false;
23
+ const importSources = ['@mui/system/colorManipulator', '@mui/material/styles', '@mui/material'];
24
+ importSources.forEach(source => {
25
+ root.find(j.ImportDeclaration, {
26
+ source: {
27
+ value: source
28
+ }
29
+ }).forEach(path => {
30
+ path.node.specifiers.forEach(spec => {
31
+ if (spec.type === 'ImportSpecifier') {
32
+ if (spec.imported.name === 'alpha') {
33
+ hasAlphaImport = true;
34
+ }
35
+ if (spec.imported.name === 'lighten') {
36
+ hasLightenImport = true;
37
+ }
38
+ if (spec.imported.name === 'darken') {
39
+ hasDarkenImport = true;
40
+ }
41
+ }
42
+ });
43
+ });
44
+ });
45
+
46
+ // Skip transformation if none of the functions are imported
47
+ if (!hasAlphaImport && !hasLightenImport && !hasDarkenImport) {
48
+ return file.source;
49
+ }
50
+ function replaceThemeWithVars(node, objectName) {
51
+ if (node.type === 'MemberExpression') {
52
+ let deepnode = node;
53
+ while (deepnode.object && deepnode.object.type === 'MemberExpression') {
54
+ deepnode = deepnode.object;
55
+ }
56
+ deepnode.object = j.logicalExpression('||', j.memberExpression(j.identifier(objectName), j.identifier('vars')), j.identifier(objectName));
57
+ }
58
+ if (node.type === 'BinaryExpression') {
59
+ return j.templateLiteral([j.templateElement({
60
+ raw: '',
61
+ cooked: ''
62
+ }, false), j.templateElement({
63
+ raw: ' + ',
64
+ cooked: ' + '
65
+ }, false), j.templateElement({
66
+ raw: '',
67
+ cooked: ''
68
+ }, true)], [replaceThemeWithVars(node.left, objectName), replaceThemeWithVars(node.right, objectName)]);
69
+ }
70
+ return node;
71
+ }
72
+
73
+ // Transform alpha function
74
+ if (hasAlphaImport) {
75
+ root.find(j.ConditionalExpression).forEach(path => {
76
+ if (path.node.test.type === 'MemberExpression') {
77
+ if (path.node.test.property.name === 'vars') {
78
+ if (path.node.alternate.type === 'CallExpression' && path.node.alternate.callee.name === 'alpha' && path.node.consequent.type === 'TemplateLiteral') {
79
+ path.replace(j.callExpression(j.memberExpression(j.identifier(path.node.test.object.name), j.identifier('alpha')), [replaceThemeWithVars(path.node.alternate.arguments[0], path.node.test.object.name), replaceThemeWithVars(path.node.alternate.arguments[1], path.node.test.object.name)]));
80
+ }
81
+ }
82
+ }
83
+ });
84
+ root.find(j.CallExpression, {
85
+ callee: {
86
+ name: 'alpha'
87
+ }
88
+ }).forEach(path => {
89
+ path.replace(j.callExpression(j.memberExpression(j.identifier('theme'), j.identifier('alpha')), path.node.arguments));
90
+ });
91
+ }
92
+
93
+ // Transform lighten function
94
+ if (hasLightenImport) {
95
+ root.find(j.CallExpression, {
96
+ callee: {
97
+ name: 'lighten'
98
+ }
99
+ }).forEach(path => {
100
+ path.replace(j.callExpression(j.memberExpression(j.identifier('theme'), j.identifier('lighten')), path.node.arguments));
101
+ });
102
+ }
103
+
104
+ // Transform darken function
105
+ if (hasDarkenImport) {
106
+ root.find(j.CallExpression, {
107
+ callee: {
108
+ name: 'darken'
109
+ }
110
+ }).forEach(path => {
111
+ path.replace(j.callExpression(j.memberExpression(j.identifier('theme'), j.identifier('darken')), path.node.arguments));
112
+ });
113
+ }
114
+
115
+ // Remove transformed functions from import statements
116
+ importSources.forEach(source => {
117
+ root.find(j.ImportDeclaration, {
118
+ source: {
119
+ value: source
120
+ }
121
+ }).forEach(path => {
122
+ const specifiers = path.node.specifiers.filter(spec => {
123
+ if (spec.type === 'ImportSpecifier') {
124
+ const name = spec.imported.name;
125
+ // Remove if it was transformed
126
+ if (name === 'alpha' && hasAlphaImport || name === 'lighten' && hasLightenImport || name === 'darken' && hasDarkenImport) {
127
+ return false;
128
+ }
129
+ }
130
+ return true;
131
+ });
132
+ if (specifiers.length === 0) {
133
+ // Remove the import entirely if no specifiers left
134
+ j(path).remove();
135
+ } else {
136
+ // Update the import with remaining specifiers
137
+ path.node.specifiers = specifiers;
138
+ }
139
+ });
140
+ });
141
+ return root.toSource(printOptions);
142
+ }