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] Fix experimentalFeatures codemod for typescript parser #14150

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
25 changes: 18 additions & 7 deletions packages/x-codemod/src/util/removeObjectProperty.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Collection, JSCodeshift, JSXAttribute } from 'jscodeshift';
import type { Collection, JSCodeshift, JSXAttribute, Identifier } from 'jscodeshift';

const getAttributeName = (attribute: JSXAttribute): string =>
attribute.name.type === 'JSXIdentifier' ? attribute.name.name : attribute.name.name.name;
Expand Down Expand Up @@ -48,11 +48,21 @@ export default function removeObjectProperty({
return;
}
const definedKeys: any[] = [];
const objectProperties = j(targetAttribute).find(j.Property);
objectProperties.forEach((path) => {
const objectKey = (path.value.key as any).name as any;
definedKeys.push(objectKey);
const properties = j(targetAttribute).find(j.Property);
const objectProperties = j(targetAttribute).find(j.ObjectProperty);

const propertiesToProcess = properties.length > 0 ? properties : objectProperties;
if (propertiesToProcess.length === 0) {
return;
}

propertiesToProcess.forEach((path) => {
const keyName = (path.value.key as Identifier).name;
if (keyName) {
definedKeys.push(keyName);
}
});

if (definedKeys.length === 1 && definedKeys[0] === propKey) {
// only that property is defined, remove the whole prop
j(element)
Expand All @@ -62,8 +72,9 @@ export default function removeObjectProperty({
j(path).remove();
});
} else {
objectProperties.forEach((path) => {
if ((path.value.key as any).name === propKey) {
propertiesToProcess.forEach((path) => {
const name = (path.value.key as Identifier).name;
if (name === propKey) {
j(path).remove();
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,39 @@ function read(fileName) {

describe('v7.0.0/data-grid', () => {
describe('remove-stabilized-experimentalFeatures', () => {
it('transforms props as needed', () => {
it('transforms props as needed - js', () => {
const actual = transform({ source: read('./actual.spec.js') }, { jscodeshift }, {});

const expected = read('./expected.spec.js');
expect(actual).to.equal(expected, 'The transformed version should be correct');
});

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

const expected = read('./expected.spec.js');
expect(actual).to.equal(expected, 'The transformed version should be correct');
});

it('transforms props as needed - ts', () => {
const actual = transform(
{ source: read('./ts-actual.spec.tsx') },
{ jscodeshift: jscodeshift.withParser('tsx') },
{},
);
const expected = read('./ts-expected.spec.tsx');
expect(actual).to.equal(expected, 'The transformed version should be correct');
});

it('should be idempotent - ts', () => {
const actual = transform(
{ source: read('./ts-expected.spec.tsx') },
{ jscodeshift: jscodeshift.withParser('tsx') },
{},
);

const expected = read('./ts-expected.spec.tsx');
expect(actual).to.equal(expected, 'The transformed version should be correct');
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// @ts-nocheck
import * as React from 'react';
import { DataGrid } from '@mui/x-data-grid';
import { DataGridPro } from '@mui/x-data-grid-pro';
import { DataGridPremium } from '@mui/x-data-grid-premium';

// prettier-ignore
function App() {
return (
<React.Fragment>
<DataGrid
experimentalFeatures={{
columnGrouping: true,
clipboardPaste: true,
}}
/>
<DataGridPro
experimentalFeatures={{
lazyLoading: true,
ariaV7: true,
}}
/>
<DataGridPremium
experimentalFeatures={{
columnGrouping: true,
clipboardPaste: true,
lazyLoading: true,
ariaV7: true,
}}
/>
<DataGridPro {...props} />
</React.Fragment>
);
}

export default App;
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// @ts-nocheck
import * as React from 'react';
import { DataGrid } from '@mui/x-data-grid';
import { DataGridPro } from '@mui/x-data-grid-pro';
import { DataGridPremium } from '@mui/x-data-grid-premium';

// prettier-ignore
function App() {
return (
(<React.Fragment>
<DataGrid />
<DataGridPro />
<DataGridPremium />
<DataGridPro {...props} />
</React.Fragment>)
);
}

export default App;