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

[mui-codemod] Fix merging of slotProps and componentProps #41323

Merged
merged 4 commits into from
Mar 7, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('@mui/codemod', () => {
const actual = transform(
{ source: read('./test-cases/theme.actual.js') },
{ jscodeshift },
{ printOptions: { trailingComma: true } },
{ printOptions: { trailingComma: false } },
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

codemod was transforming without trailing comma, so changed to false here. Hope it's not an issue

);

const expected = read('./test-cases/theme.expected.js');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,9 @@ import Alert from '@mui/material/Alert';
slotProps={{ closeIcon: slotsIconProps, closeButton: slotsButtonProps }}
componentsProps={{ closeButton: componentsButtonProps }}
/>;
<Alert
slots={{ closeIcon: SlotsIcon, closeButton: SlotsButton }}
components={{ CloseButton: ComponentsButton }}
slotProps={{ closeIcon: slotsIconProps, closeButton: slotsButtonProps }}
componentsProps={{ closeButton: componentsButtonProps, closeIcon: componentsIconProps }}
/>;
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,16 @@ import Alert from '@mui/material/Alert';
}} />;
<Alert
slots={{ closeIcon: SlotsIcon, closeButton: SlotsButton }}
slotProps={{ closeIcon: slotsIconProps, closeButton: slotsButtonProps }} />;
slotProps={{ closeIcon: slotsIconProps, closeButton: {
...componentsButtonProps,
...slotsButtonProps
} }} />;
<Alert
slots={{ closeIcon: SlotsIcon, closeButton: SlotsButton }}
slotProps={{ closeButton: {
...componentsButtonProps,
...slotsButtonProps
}, closeIcon: {
...componentsIconProps,
...slotsIconProps
} }} />;
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,14 @@ fn({
},
},
});

fn({
MuiAlert: {
defaultProps: {
components: { CloseButton: ComponentsButton },
slots: { closeIcon: SlotsIcon, closeButton: SlotsButton },
componentsProps: { closeButton: componentsButtonProps, closeIcon: componentsIconProps },
slotProps: { closeIcon: slotsIconProps, closeButton: slotsButtonProps },
},
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ fn({
MuiAlert: {
defaultProps: {
slots: {
closeButton: ComponentsButton,
closeButton: ComponentsButton
},

slotProps: {
closeButton: componentsButtonProps,
},
closeButton: componentsButtonProps
}
},
},
});
Expand All @@ -17,13 +17,13 @@ fn({
defaultProps: {
slots: {
closeButton: ComponentsButton,
closeIcon: SlotsIcon,
closeIcon: SlotsIcon
},

slotProps: {
closeButton: componentsButtonProps,
closeIcon: slotsIconProps,
},
closeIcon: slotsIconProps
}
},
},
});
Expand All @@ -33,13 +33,40 @@ fn({
defaultProps: {
slots: {
closeButton: SlotsButton,
closeIcon: SlotsIcon,
closeIcon: SlotsIcon
},

slotProps: {
closeButton: slotsButtonProps,
closeIcon: slotsIconProps,
closeButton: {
...componentsButtonProps,
...slotsButtonProps
},

closeIcon: slotsIconProps
}
},
},
});

fn({
MuiAlert: {
defaultProps: {
slots: {
closeButton: SlotsButton,
closeIcon: SlotsIcon
},

slotProps: {
closeButton: {
...componentsButtonProps,
...slotsButtonProps
},

closeIcon: {
...componentsIconProps,
...slotsIconProps
}
}
},
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('@mui/codemod', () => {
const actual = transform(
{ source: read('./test-cases/theme.actual.js') },
{ jscodeshift },
{ printOptions: { trailingComma: true } },
{ printOptions: { trailingComma: false } },
);

const expected = read('./test-cases/theme.expected.js');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,9 @@ import Slider from '@mui/material/Slider';
slotProps={{ rail: slotsRailProps, track: slotsTrackProps }}
componentsProps={{ track: componentsTrackProps }}
/>;
<Slider
slots={{ rail: SlotsRail, track: SlotsTrack }}
components={{ Track: ComponentsTrack }}
slotProps={{ rail: slotsRailProps, track: slotsTrackProps }}
componentsProps={{ track: componentsTrackProps, rail: componentsRailProps}}
/>;
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,16 @@ import Slider from '@mui/material/Slider';
}} />;
<Slider
slots={{ rail: SlotsRail, track: SlotsTrack }}
slotProps={{ rail: slotsRailProps, track: slotsTrackProps }} />;
slotProps={{ rail: slotsRailProps, track: {
...componentsTrackProps,
...slotsTrackProps
} }} />;
<Slider
slots={{ rail: SlotsRail, track: SlotsTrack }}
slotProps={{ track: {
...componentsTrackProps,
...slotsTrackProps
}, rail: {
...componentsRailProps,
...slotsRailProps
} }} />;
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,14 @@ fn({
},
},
});

fn({
MuiSlider: {
defaultProps: {
components: { Track: ComponentsTrack },
slots: { rail: SlotsRail, track: SlotsTrack },
componentsProps: { track: componentsTrackProps, rail: componentsRailProps },
slotProps: { rail: slotsRailProps, track: slotsTrackProps },
},
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ fn({
MuiSlider: {
defaultProps: {
slots: {
track: ComponentsTrack,
track: ComponentsTrack
},

slotProps: {
track: componentsTrackProps,
},
track: componentsTrackProps
}
},
},
});
Expand All @@ -17,13 +17,13 @@ fn({
defaultProps: {
slots: {
track: ComponentsTrack,
rail: SlotsRail,
rail: SlotsRail
},

slotProps: {
track: componentsTrackProps,
rail: slotsRailProps,
},
rail: slotsRailProps
}
},
},
});
Expand All @@ -33,13 +33,40 @@ fn({
defaultProps: {
slots: {
track: SlotsTrack,
rail: SlotsRail,
rail: SlotsRail
},

slotProps: {
track: slotsTrackProps,
rail: slotsRailProps,
track: {
...componentsTrackProps,
...slotsTrackProps
},

rail: slotsRailProps
}
},
},
});

fn({
MuiSlider: {
defaultProps: {
slots: {
track: SlotsTrack,
rail: SlotsRail
},

slotProps: {
track: {
...componentsTrackProps,
...slotsTrackProps
},

rail: {
...componentsRailProps,
...slotsRailProps
}
}
},
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ function replaceJsxComponentsPropsProp(j, element) {
key: prop.key.name,
expression: prop.value,
});
} else {
attr.value.expression.properties = attr.value.expression.properties.filter(
(p) => p?.key?.name !== prop.key.name,
);

assignObject(j, {
target: attr,
key: prop.key.name,
expression: j.objectExpression([
j.spreadElement(prop.value),
j.spreadElement(slotProps[prop.key.name]),
]),
});
}
});
}
Expand Down Expand Up @@ -131,7 +144,15 @@ function replaceDefaultPropsComponentsPropsProp(j, defaultPropsPathCollection) {

const slots = existingSlots
? existingSlots.value.properties.reduce((acc, prop) => {
return { ...acc, [prop.key.name]: prop.value };
return {
...acc,
[prop.key.name]: components[prop.key.name]
? j.objectExpression([
j.spreadElement(components[prop.key.name]),
j.spreadElement(prop.value),
])
: prop.value,
};
}, {})
: {};

Expand Down
Loading