Closed rsslldnphy closed 2 years ago
I've never used jscodeshift
but have managed to hack something together which meets my needs - with the module alias material ui is imported as hardcoded to UI
:
diff --git a/packages/material-ui-codemod/src/util/propsToObject.js b/packages/material-ui-codemod/src/util/propsToObject.js
index 9309a5edff..641c4d5131 100644
--- a/packages/material-ui-codemod/src/util/propsToObject.js
+++ b/packages/material-ui-codemod/src/util/propsToObject.js
@@ -11,8 +11,19 @@ export default function propsToObject({ j, root, componentName, propName, props
return value;
}
- return root.findJSXElements(componentName).forEach((path) => {
+ const isJSXIdentifier = (node, name) => j.JSXIdentifier.check(node) && node.name === name;
+ return root.findJSXElements().forEach((path) => {
+ const { openingElement } = path.value;
+ const isUnscopedElement = isJSXIdentifier(openingElement.name, componentName);
+ const isScopedElement =
+ j.JSXMemberExpression.check(openingElement.name) &&
+ isJSXIdentifier(openingElement.name.object, 'UI') &&
+ isJSXIdentifier(openingElement.name.property, componentName);
+
+ if (!isUnscopedElement && !isScopedElement) return;
+
let propValue = [];
+
const attributes = path.node.openingElement.attributes;
attributes.forEach((node, index) => {
// Only transform whitelisted props
Posting it here in case it's useful for anyone else facing the same problem (you can just change the hardcoded 'UI'
to whatever alias you use) or in case it's helpful for someone who knows jscodeshift
and can make this code work for any alias the material-ui module is imported as.
The codemod to convert
Box
style props to thesx
prop syntax doesn't work if material UI is imported into a module alias, i.e.import * as UI from "@material-ui/core";
Current Behavior 😯
Running the codemod on files like this:
leaves them unmodified:
It works as expected on files like this:
Expected Behavior 🤔
The codemod should work for files using the module alias.
Steps to Reproduce 🕹
Steps:
foo.tsx
containing the following:const Foo = () =>
System: OS: macOS 11.2 Binaries: Node: 15.12.0 - /usr/local/bin/node Yarn: 1.22.10 - ~/Code/gg/goodgym-web/node_modules/.bin/yarn npm: 7.6.3 - /usr/local/bin/npm Browsers: Chrome: 90.0.4430.212 Edge: Not Found Firefox: 88.0.1 Safari: 14.0.3 npmPackages: @emotion/styled: 10.0.27 @material-ui/codemod: ^5.0.0-alpha.33 => 5.0.0-alpha.33 @material-ui/core: ^5.0.0-alpha.34 => 5.0.0-alpha.34 @material-ui/icons: ^5.0.0-alpha.34 => 5.0.0-alpha.34 @material-ui/lab: ^5.0.0-alpha.34 => 5.0.0-alpha.34 @material-ui/private-theming: 5.0.0-alpha.33 @material-ui/styled-engine: 5.0.0-alpha.34 @material-ui/styles: 5.0.0-alpha.33 @material-ui/system: 5.0.0-alpha.34 @material-ui/types: 6.0.0 @material-ui/unstyled: 5.0.0-alpha.34 @material-ui/utils: 5.0.0-alpha.33 @types/react: ^16.9.53 => 16.14.5 react: ^17.0.1 => 17.0.2 react-dom: ^17.0.1 => 17.0.2 typescript: ^4.0.3 => 4.2.4