Skip to content

Commit

Permalink
[codemod] Fix codemod crash on MuiDivider property (#43125)
Browse files Browse the repository at this point in the history
  • Loading branch information
Janpot authored Aug 1, 2024
1 parent edc6d61 commit 0ed7c11
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,9 @@ fn({
},
},
});
fn({
MuiDivider: {},
});
fn({
MuiDivider: 123,
});
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,29 @@ fn({
defaultProps: {
sx: {
opacity: "0.6"
},
}
},
},
});
fn({
MuiDivider: {
defaultProps: {
className: 'my-class',

sx: {
opacity: "0.6"
},
}
},
},
});
fn({
MuiDivider: {
defaultProps: {
className: 'my-class',

sx: {
opacity: "0.6"
},
}
},
},
});
Expand Down Expand Up @@ -69,3 +71,9 @@ fn({
},
},
});
fn({
MuiDivider: {},
});
fn({
MuiDivider: 123,
});

0 comments on commit 0ed7c11

Please sign in to comment.