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] Skip sx spread transformation #43291

Merged
merged 2 commits into from
Aug 14, 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
4 changes: 4 additions & 0 deletions packages/mui-codemod/src/v6.0.0/sx-prop/sx-v6.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,10 @@ export default function sxV6(file, api, options) {
}
if (data.node.argument.type === 'ConditionalExpression') {
const isSxSpread =
(data.node.argument.test.type === 'CallExpression' &&
data.node.argument.test.callee.type === 'MemberExpression' &&
data.node.argument.test.callee.object.name === 'Array' &&
data.node.argument.test.callee.property.name === 'isArray') ||
Comment on lines +343 to +346
Copy link
Member

Choose a reason for hiding this comment

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

Are you sure that we don't need the sx prop name check in this rule as well? 🤔
I suspect that given the existing rule, it would skip the following prop as well, even though it probably shouldn't. 🤔

xyz={[
  ...(Array.isArray(xyz) ? xs : [xyz]),
  ...(Array.isArray(slotProps?.layout?.xyz) ? slotProps.layout.xyz : [slotProps.layout.sxxyz),

Copy link
Member Author

Choose a reason for hiding this comment

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

This codemod only targets sx prop, so it will not look into this xyz prop.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, I saw sx checks in the || else rules and assumed these are the only checks. 🙈
LGTM then, sorry for the distraction. 👍

(data.node.argument.consequent.type === 'Identifier' &&
data.node.argument.consequent.name === 'sx') ||
(data.node.argument.alternate.type === 'Identifier' &&
Expand Down
24 changes: 24 additions & 0 deletions packages/mui-codemod/src/v6.0.0/sx-prop/sx-v6.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,5 +177,29 @@ describe('@mui/codemod', () => {
expect(actual).to.equal(expected, 'The transformed version should be correct');
});
});

describe('inheritance sx-v6', () => {
it('should do nothing', () => {
const actual = transform(
{ source: read('./test-cases/sx-inheritance.actual.js') },
{ jscodeshift },
{},
);

const expected = read('./test-cases/sx-inheritance.expected.js');
expect(actual).to.equal(expected, 'The transformed version should be correct');
});

it('should be idempotent', () => {
const actual = transform(
{ source: read('./test-cases/sx-inheritance.expected.js') },
{ jscodeshift },
{},
);

const expected = read('./test-cases/sx-inheritance.expected.js');
expect(actual).to.equal(expected, 'The transformed version should be correct');
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Layout
{...layoutProps}
{...slotProps.layout}
slots={slots}
slotProps={slotProps}
sx={[
...(Array.isArray(sx) ? sx : [sx]),
...(Array.isArray(slotProps?.layout?.sx) ? slotProps.layout.sx : [slotProps.layout.sx]),
]}
className={clsx(className, slotProps.layout.className)}
ref={ref}
/>;

<FormControl
disabled={disabled}
id={id}
sx={[...(Array.isArray(formControlSx) ? formControlSx : [formControlSx])]}
/>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Layout
{...layoutProps}
{...slotProps.layout}
slots={slots}
slotProps={slotProps}
sx={[
...(Array.isArray(sx) ? sx : [sx]),
...(Array.isArray(slotProps?.layout?.sx) ? slotProps.layout.sx : [slotProps.layout.sx]),
]}
className={clsx(className, slotProps.layout.className)}
ref={ref}
/>;

<FormControl
disabled={disabled}
id={id}
sx={[...(Array.isArray(formControlSx) ? formControlSx : [formControlSx])]}
/>;
Loading