Skip to content
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

[codemod][Divider] Only apply codemod if light prop is present #42098

Merged
merged 2 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
103 changes: 58 additions & 45 deletions packages/mui-codemod/src/deprecations/divider-props/divider-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,43 @@ export default function transformer(file, api, options) {
const printOptions = options.printOptions;

findComponentJSX(j, { root, componentName: 'Divider' }, (elementPath) => {
elementPath.node.openingElement.attributes = elementPath.node.openingElement.attributes.filter(
(attr) => {
if (attr.type === 'JSXAttribute' && attr.name.name === 'light') {
return false;
}
return true;
},
);
const hasLightProp =
elementPath.node.openingElement.attributes.findIndex(
(attr) => attr.type === 'JSXAttribute' && attr.name.name === 'light',
) !== -1;

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('opacity'), j.literal('0.6')),
]),
});
} else {
const opacityIndex = elementPath.node.openingElement.attributes[
sxIndex
].value.expression.properties.findIndex((key) => key.key.name === 'opacity');
if (hasLightProp) {
elementPath.node.openingElement.attributes =
elementPath.node.openingElement.attributes.filter((attr) => {
if (attr.type === 'JSXAttribute' && attr.name.name === 'light') {
return false;
}
return true;
});

if (opacityIndex === -1) {
assignObject(j, {
target: elementPath.node.openingElement.attributes[sxIndex],
key: 'opacity',
expression: j.literal('0.6'),
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('opacity'), j.literal('0.6')),
]),
});
} else {
const opacityIndex = elementPath.node.openingElement.attributes[
sxIndex
].value.expression.properties.findIndex((key) => key.key.name === 'opacity');

if (opacityIndex === -1) {
assignObject(j, {
target: elementPath.node.openingElement.attributes[sxIndex],
key: 'opacity',
expression: j.literal('0.6'),
});
}
}
}
});
Expand All @@ -52,28 +58,35 @@ export default function transformer(file, api, options) {
(key) => key.key.name === 'defaultProps',
);

defaultPropsObject.value.properties = defaultPropsObject.value.properties.filter(
(prop) => !['light'].includes(prop?.key?.name),
);

const sxIndex = defaultPropsObject.value.properties.findIndex((prop) => prop.key.name === 'sx');
const hasLightProp =
defaultPropsObject.value.properties.findIndex((prop) => prop.key.name === 'light') !== -1;

if (sxIndex === -1) {
defaultPropsObject.value.properties.push(
j.objectProperty(
j.identifier('sx'),
j.objectExpression([j.objectProperty(j.identifier('opacity'), j.literal('0.6'))]),
),
if (hasLightProp) {
defaultPropsObject.value.properties = defaultPropsObject.value.properties.filter(
(prop) => !['light'].includes(prop?.key?.name),
);
} else {
const opacityIndex = defaultPropsObject.value.properties[sxIndex].value.properties.findIndex(
(key) => key.key.name === 'opacity',

const sxIndex = defaultPropsObject.value.properties.findIndex(
(prop) => prop.key.name === 'sx',
);

if (opacityIndex === -1) {
defaultPropsObject.value.properties[sxIndex].value.properties.push(
j.objectProperty(j.identifier('opacity'), j.literal('0.6')),
if (sxIndex === -1) {
defaultPropsObject.value.properties.push(
j.objectProperty(
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');

if (opacityIndex === -1) {
defaultPropsObject.value.properties[sxIndex].value.properties.push(
j.objectProperty(j.identifier('opacity'), j.literal('0.6')),
);
}
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ import { Divider as MyDivider } from '@mui/material';
<MyDivider light={light} className="test" />;
<Divider light sx={{ opacity: '0.7' }} />;
<MyDivider light sx={{ bgcolor: 'black' }} />;
<Divider className="test" />;
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ import { Divider as MyDivider } from '@mui/material';
bgcolor: 'black',
opacity: "0.6"
}} />;
<Divider className="test" />;
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,11 @@ fn({
},
},
});

fn({
MuiDivider: {
defaultProps: {
className: 'my-class',
},
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,11 @@ fn({
},
},
});

fn({
MuiDivider: {
defaultProps: {
className: 'my-class',
},
},
});