Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mui-codemod] Fix findComponentJSX util #40855

Merged
merged 2 commits into from
Jan 31, 2024
Merged

Conversation

sai6855
Copy link
Contributor

@sai6855 sai6855 commented Jan 30, 2024

How to test the fix

  1. go to https://github.com/mui/material-ui/blob/master/packages/mui-codemod/src/deprecations/accordion-props/accordion-props.js in master branch
  2. add console.log(elementPath.node.openingElement.name.name); below this line
    findComponentJSX(j, { root, componentName: 'Accordion' }, (elementPath) => {
    and run node packages/mui-codemod/codemod deprecations/accordion-props packages/mui-codemod/src/deprecations/accordion-props/test-cases/actual.js
  3. Notice it logs Accordion 2 times, everything is good until now
  4. Now change componentName: "Accordion" to componentName: "sank" (or any random value) on this line and run node packages/mui-codemod/codemod deprecations/accordion-props packages/mui-codemod/src/deprecations/accordion-props/test-cases/actual.js
  5. Notice it logs Accordion again 2 times, as per my understanding findComponentJSX function should filter out components which matches with componentName but i don't think that's the case now. This PR fixes it

Note: I'm really new to codemods and Regex, apologies in advance if my understanding or logic is wrong

@sai6855 sai6855 added the package: codemod Specific to @mui/codemod label Jan 30, 2024
@sai6855 sai6855 marked this pull request as draft January 30, 2024 11:47
@mui-bot
Copy link

mui-bot commented Jan 30, 2024

Netlify deploy preview

https://deploy-preview-40855--material-ui.netlify.app/

Bundle size report

No bundle size changes (Toolpad)
No bundle size changes

Generated by 🚫 dangerJS against 8b714f8

@sai6855 sai6855 marked this pull request as ready for review January 30, 2024 11:59
@sai6855 sai6855 added the bug 🐛 Something doesn't work label Jan 30, 2024
Copy link
Member

@DiegoAndai DiegoAndai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching this @sai6855

Comment on lines 20 to 23
.filter(
(path) =>
path.node.source.value.match(new RegExp(`^@mui/material/(${componentName})`)) ||
path.node.source.value === '@mui/material',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can make it simpler:

Suggested change
.filter(
(path) =>
path.node.source.value.match(new RegExp(`^@mui/material/(${componentName})`)) ||
path.node.source.value === '@mui/material',
.filter((path) =>
path.node.source.value.match(new RegExp(`^@mui/material(/${componentName})?$`)),

It's the same as your fix, as $ indicates to match the end of the line, so it's either

  • string start (^) -> @mui/material -> string end ($)
  • string start (^) -> @mui/material/componentName -> string end ($)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for clear explaination. fixed in this commit 8b714f8

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Did you test if it's fixed on your side after the refactor?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, ran codemod script. Now util is filtering out data correctly

@DiegoAndai DiegoAndai merged commit 69b7a29 into mui:master Jan 31, 2024
19 checks passed
@sai6855 sai6855 deleted the fix-codemod branch January 31, 2024 20:18
mostafa-rio pushed a commit to mostafa-rio/material-ui that referenced this pull request Feb 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work package: codemod Specific to @mui/codemod
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants