From 0ed7c11eab4dc64271b6f63e6577301c38b32d63 Mon Sep 17 00:00:00 2001 From: Jan Potoms <2109932+Janpot@users.noreply.github.com> Date: Thu, 1 Aug 2024 19:26:01 +0200 Subject: [PATCH] [codemod] Fix codemod crash on MuiDivider property (#43125) --- .../divider-props/divider-props.js | 48 ++++++++----------- .../divider-props/test-cases/theme.actual.js | 6 +++ .../test-cases/theme.expected.js | 14 ++++-- 3 files changed, 36 insertions(+), 32 deletions(-) diff --git a/packages/mui-codemod/src/deprecations/divider-props/divider-props.js b/packages/mui-codemod/src/deprecations/divider-props/divider-props.js index eeedbb934d3627..477579f140e4bd 100644 --- a/packages/mui-codemod/src/deprecations/divider-props/divider-props.js +++ b/packages/mui-codemod/src/deprecations/divider-props/divider-props.js @@ -1,6 +1,7 @@ import appendAttribute from '../../util/appendAttribute'; import assignObject from '../../util/assignObject'; import findComponentJSX from '../../util/findComponentJSX'; +import findComponentDefaultProps from '../../util/findComponentDefaultProps'; /** * @param {import('jscodeshift').FileInfo} file @@ -61,47 +62,36 @@ export default function transformer(file, api, options) { } }); - root.find(j.ObjectProperty, { key: { name: 'MuiDivider' } }).forEach((path) => { - const defaultPropsObject = path.value.value.properties.find( - (key) => key.key.name === 'defaultProps', - ); + const defaultPropsPathCollection = findComponentDefaultProps(j, { + root, + componentName: 'Divider', + }); - const lightProp = defaultPropsObject.value.properties.find((prop) => prop.key.name === 'light'); + defaultPropsPathCollection.find(j.ObjectProperty, { key: { name: 'light' } }).forEach((path) => { + const { properties: defaultPropsProperties } = path.parent.value; - if (!lightProp) { + if (path.value?.value.value === false) { + path.prune(); return; } - defaultPropsObject.value.properties = defaultPropsObject.value.properties.filter( - (prop) => !['light'].includes(prop?.key?.name), - ); - - const isLightPropTruthy = lightProp.value?.value !== false; - - if (!isLightPropTruthy) { - return; - } + const existingSx = defaultPropsProperties.find((prop) => prop.key.name === 'sx'); - const sxIndex = defaultPropsObject.value.properties.findIndex((prop) => prop.key.name === 'sx'); - - if (sxIndex === -1) { - defaultPropsObject.value.properties.push( - j.objectProperty( + if (!existingSx) { + defaultPropsProperties.push( + j.property( + 'init', j.identifier('sx'), j.objectExpression([j.objectProperty(j.identifier('opacity'), j.literal('0.6'))]), ), ); - } else { - const opacityIndex = defaultPropsObject.value.properties[sxIndex].value.properties.findIndex( - (key) => key.key.name === 'opacity', + } else if (!existingSx.value.properties.find((prop) => prop.key.name === 'opacity')) { + existingSx.value.properties.push( + j.property('init', j.identifier('opacity'), j.literal('0.6')), ); - - if (opacityIndex === -1) { - defaultPropsObject.value.properties[sxIndex].value.properties.push( - j.objectProperty(j.identifier('opacity'), j.literal('0.6')), - ); - } } + + path.prune(); }); return root.toSource(printOptions); diff --git a/packages/mui-codemod/src/deprecations/divider-props/test-cases/theme.actual.js b/packages/mui-codemod/src/deprecations/divider-props/test-cases/theme.actual.js index f2f889f5f3dd7e..8cf9f0b8cf985b 100644 --- a/packages/mui-codemod/src/deprecations/divider-props/test-cases/theme.actual.js +++ b/packages/mui-codemod/src/deprecations/divider-props/test-cases/theme.actual.js @@ -65,3 +65,9 @@ fn({ }, }, }); +fn({ + MuiDivider: {}, +}); +fn({ + MuiDivider: 123, +}); diff --git a/packages/mui-codemod/src/deprecations/divider-props/test-cases/theme.expected.js b/packages/mui-codemod/src/deprecations/divider-props/test-cases/theme.expected.js index 58f57de0e2fb5d..083c6141c11734 100644 --- a/packages/mui-codemod/src/deprecations/divider-props/test-cases/theme.expected.js +++ b/packages/mui-codemod/src/deprecations/divider-props/test-cases/theme.expected.js @@ -15,7 +15,7 @@ fn({ defaultProps: { sx: { opacity: "0.6" - }, + } }, }, }); @@ -23,9 +23,10 @@ fn({ MuiDivider: { defaultProps: { className: 'my-class', + sx: { opacity: "0.6" - }, + } }, }, }); @@ -33,9 +34,10 @@ fn({ MuiDivider: { defaultProps: { className: 'my-class', + sx: { opacity: "0.6" - }, + } }, }, }); @@ -69,3 +71,9 @@ fn({ }, }, }); +fn({ + MuiDivider: {}, +}); +fn({ + MuiDivider: 123, +});