-
Notifications
You must be signed in to change notification settings - Fork 481
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
Extra parens inserted around JSX after attributes are added #534
Comments
This is something that is happening at the recast level. I temporarily downgraded jscodeshift's version of recast to 0.20.4 and the extra parentheses went away. Do the extra parentheses actually introduce bugs/regressions into migrated mui projects, or is it more of an aesthetics thing? |
Here's what I believe is the issue.
This might be one of those tricky recast bugs that crosses the border into philosophical debate on what the "correct" behavior might be and how exceptions to rules to exceptions to rules might have to be calculated. You might be better off adding a prettier rule that strips the extra parentheses and running code through prettier after the migration. |
It's impossible to say right now. We haven't released our codemods with updated jscodeshift, so we don't know what our users may run into. The way our tests fail makes me believe it's only a matter of aesthetics.
Thanks a lot for investigating it! I don't think we can use any formatter as a part of the codemod, as we don't know the code style settings of codebases the codemods will run on.
Does jscodeshift 0.14.0 work with recast 0.20.4 without any code changes? Perhaps pinning the recast version using yarn resolutions could be the way to go for us. |
FWIW this is generally the approach I take these days, across a few languages. Rather than ensuring all codemods output properly formatted code, I instead use a tool like Prettier to auto-format the resulting code. At Meta, we have auto-formatters for the most commonly-used languages, and for example our Hacklang codemods don't even try to properly format the resulting code as they just rely on hackfmt to format the code. The issue with that is that not every codebase uses auto-formatting... |
This impacts us as well, in the same way. Unit tests doing input/output comparisons (similar to what MUI's codemod test suite does) are breaking after the update, and it yields files now that don't conform to prettier rules. |
Phiphuket99 |
There appears to be a PR in |
Aye, there is. And it may get merged into recast's main branch in the next year or two. |
I remove it by regexp: const REG_RETURN = /return\s+\((\s+)\(<([^\s>]+)/g;
function removeCircleBracket(string) {
const matches = string.matchAll(REG_RETURN);
if (matches) {
[...matches].forEach((match) => {
const [_, spaces, componentName] = match;
const sl = spaces.length;
const REG_DOUBLE_TAG = `(return\\s+\\(\\s{${sl}})\\((<${componentName}.+?\\s{${sl}}<\\/${componentName}>)\\)(\\s+\\))`;
const REG_SELF_CLOSING = `(return\\s+\\(\\${sl})\\((<${componentName}.+?\\${sl}\\/>)\\)(\\s+\\))`;
const REG_FULL = REG_DOUBLE_TAG + '|' + REG_SELF_CLOSING;
const regexp = new RegExp(REG_FULL, 'gs');
string = string.replace(regexp, (match, g1, g2, g3, g4, g5, g6) => {
const isDoubleCondition = g1 && g2 && g3;
const isSelfCondition = g4 && g5 && g6;
if (isDoubleCondition) {
return g1 + g2 + g3;
}
if (isSelfCondition) {
return g4 + g5 + g6;
}
return match;
});
});
}
return string;
} removeCircleBracket(root.toSource(toSourceOptions)); Doesn't handle all cases, but most + it's necessary for the code to be written correctly. |
We're encountering an issue that prevents us from upgrading from 0.13.1 to 0.14.0 (mui/material-ui#34680).
It seems that when a codemod adds a JSX attribute, the whole JSX block in a return statement gets wrapped in extra parens (even if it had parens before).
I managed to create a minimal repro:
index.js
source.js
expected output
actual output
The text was updated successfully, but these errors were encountered: