diff --git a/packages/mui-codemod/src/deprecations/typography-props/test-cases/actual.js b/packages/mui-codemod/src/deprecations/typography-props/test-cases/actual.js
index a5263bdb240e8e..5848d49bd63308 100644
--- a/packages/mui-codemod/src/deprecations/typography-props/test-cases/actual.js
+++ b/packages/mui-codemod/src/deprecations/typography-props/test-cases/actual.js
@@ -11,6 +11,8 @@ import { Typography as MyTypography } from '@mui/material';
;
;
;
+;
+;
;
;
;
diff --git a/packages/mui-codemod/src/deprecations/typography-props/test-cases/expected.js b/packages/mui-codemod/src/deprecations/typography-props/test-cases/expected.js
index b078bb27c92dad..539d508318ba93 100644
--- a/packages/mui-codemod/src/deprecations/typography-props/test-cases/expected.js
+++ b/packages/mui-codemod/src/deprecations/typography-props/test-cases/expected.js
@@ -25,6 +25,12 @@ import { Typography as MyTypography } from '@mui/material';
;
+;
+;
;
;
;
diff --git a/packages/mui-codemod/src/deprecations/typography-props/typography-props.js b/packages/mui-codemod/src/deprecations/typography-props/typography-props.js
index 9968cc366038b4..170f0a8d8a22ce 100644
--- a/packages/mui-codemod/src/deprecations/typography-props/typography-props.js
+++ b/packages/mui-codemod/src/deprecations/typography-props/typography-props.js
@@ -29,12 +29,23 @@ 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',
);
@@ -42,9 +53,9 @@ export default function transformer(file, api, options) {
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[
@@ -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')),
});
}
}