Skip to content

Commit

Permalink
[codemod] Fix handling of computed paragraph props (mui#44195)
Browse files Browse the repository at this point in the history
Co-authored-by: ZeeshanTamboli <[email protected]>
  • Loading branch information
joshkel and ZeeshanTamboli authored Nov 26, 2024
1 parent 72823b8 commit 5d1c6ab
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
7 changes: 7 additions & 0 deletions packages/mui-codemod/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1669,6 +1669,13 @@ npx @mui/codemod@latest deprecations/table-sort-label-classes <path>
/>
```

```diff
<Typography
- paragraph={isTypographyParagraph}
+ sx={isTypographyParagraph ? { marginBottom: '16px' } : undefined}
/>
```

```diff
MuiTypography: {
defaultProps: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { Typography as MyTypography } from '@mui/material';
<MyTypography paragraph className="my-class" />;
<Typography paragraph={false} className="my-class" />;
<MyTypography paragraph={false} className="my-class" />;
<Typography paragraph={true} className="my-class" />;
<MyTypography paragraph={true} className="my-class" />;
<Typography paragraph={paragraph} className="my-class" />;
<MyTypography paragraph={paragraph} className="my-class" />;
<Typography paragraph sx={{ marginBottom: "32px" }} />;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ import { Typography as MyTypography } from '@mui/material';
<MyTypography className="my-class" sx={{
marginBottom: "16px"
}} />;
<Typography className="my-class" sx={paragraph ? {
marginBottom: "16px"
} : undefined} />;
<MyTypography className="my-class" sx={paragraph ? {
marginBottom: "16px"
} : undefined} />;
<Typography sx={{ marginBottom: "32px" }} />;
<MyTypography sx={{ marginBottom: "32px" }} />;
<Typography sx={{ mb: "32px" }} />;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,33 @@ export default function transformer(file, api, options) {
},
);

const isParagraphPropTruthy = paragraphProp.value?.expression.value !== false;
const isParagraphPropPresent = paragraphProp.value?.expression.value !== false;

if (!isParagraphPropTruthy) {
if (!isParagraphPropPresent) {
return;
}

const isParagraphPropTrue =
paragraphProp.value == null || paragraphProp.value.expression.value === true;
const paragraphExpression = (expression) =>
isParagraphPropTrue
? expression
: j.conditionalExpression(
paragraphProp.value.expression,
expression,
j.identifier('undefined'),
);

const sxIndex = elementPath.node.openingElement.attributes.findIndex(
(attr) => attr.type === 'JSXAttribute' && attr.name.name === 'sx',
);
if (sxIndex === -1) {
appendAttribute(j, {
target: elementPath.node,
attributeName: 'sx',
expression: j.objectExpression([
j.objectProperty(j.identifier('marginBottom'), j.literal('16px')),
]),
expression: paragraphExpression(
j.objectExpression([j.objectProperty(j.identifier('marginBottom'), j.literal('16px'))]),
),
});
} else {
const hasMarginBottom = elementPath.node.openingElement.attributes[
Expand All @@ -57,7 +68,7 @@ export default function transformer(file, api, options) {
assignObject(j, {
target: elementPath.node.openingElement.attributes[sxIndex],
key: 'marginBottom',
expression: j.literal('16px'),
expression: paragraphExpression(j.literal('16px')),
});
}
}
Expand Down

0 comments on commit 5d1c6ab

Please sign in to comment.