From 321853cf829cdb3301c4e2681941c3960b99dccf Mon Sep 17 00:00:00 2001 From: Bilal Shafi Date: Fri, 9 Aug 2024 15:59:44 +0500 Subject: [PATCH 1/4] [codemod] Fix experimentalFeatures codemod for typescript parser --- .../src/util/removeObjectProperty.ts | 25 +++++++++----- ...ve-stabilized-experimentalFeatures.test.ts | 23 +++++++++++++ .../ts-actual.spec.tsx | 34 +++++++++++++++++++ .../ts-expected.spec.tsx | 17 ++++++++++ 4 files changed, 91 insertions(+), 8 deletions(-) create mode 100644 packages/x-codemod/src/v7.0.0/data-grid/remove-stabilized-experimentalFeatures/ts-actual.spec.tsx create mode 100644 packages/x-codemod/src/v7.0.0/data-grid/remove-stabilized-experimentalFeatures/ts-expected.spec.tsx diff --git a/packages/x-codemod/src/util/removeObjectProperty.ts b/packages/x-codemod/src/util/removeObjectProperty.ts index b93cbfb3682a3..80fe75236334d 100644 --- a/packages/x-codemod/src/util/removeObjectProperty.ts +++ b/packages/x-codemod/src/util/removeObjectProperty.ts @@ -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; @@ -48,11 +48,19 @@ 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) { + 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) @@ -62,8 +70,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(); } }); diff --git a/packages/x-codemod/src/v7.0.0/data-grid/remove-stabilized-experimentalFeatures/remove-stabilized-experimentalFeatures.test.ts b/packages/x-codemod/src/v7.0.0/data-grid/remove-stabilized-experimentalFeatures/remove-stabilized-experimentalFeatures.test.ts index d5610209aafd4..fd6ce1c889f2b 100644 --- a/packages/x-codemod/src/v7.0.0/data-grid/remove-stabilized-experimentalFeatures/remove-stabilized-experimentalFeatures.test.ts +++ b/packages/x-codemod/src/v7.0.0/data-grid/remove-stabilized-experimentalFeatures/remove-stabilized-experimentalFeatures.test.ts @@ -24,4 +24,27 @@ describe('v7.0.0/data-grid', () => { expect(actual).to.equal(expected, 'The transformed version should be correct'); }); }); + + describe('remove-stabilized-experimentalFeatures - with typescript', () => { + it('transforms props as needed', () => { + 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', () => { + 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'); + }); + }); }); diff --git a/packages/x-codemod/src/v7.0.0/data-grid/remove-stabilized-experimentalFeatures/ts-actual.spec.tsx b/packages/x-codemod/src/v7.0.0/data-grid/remove-stabilized-experimentalFeatures/ts-actual.spec.tsx new file mode 100644 index 0000000000000..10eb22d752ff7 --- /dev/null +++ b/packages/x-codemod/src/v7.0.0/data-grid/remove-stabilized-experimentalFeatures/ts-actual.spec.tsx @@ -0,0 +1,34 @@ +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'; + +function App() { + return ( + + + + + + + ); +} + +export default App; diff --git a/packages/x-codemod/src/v7.0.0/data-grid/remove-stabilized-experimentalFeatures/ts-expected.spec.tsx b/packages/x-codemod/src/v7.0.0/data-grid/remove-stabilized-experimentalFeatures/ts-expected.spec.tsx new file mode 100644 index 0000000000000..d1a1271fa5235 --- /dev/null +++ b/packages/x-codemod/src/v7.0.0/data-grid/remove-stabilized-experimentalFeatures/ts-expected.spec.tsx @@ -0,0 +1,17 @@ +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'; + +function App() { + return ( + ( + + + + + ) + ); +} + +export default App; From 40b4f0ddcca1c84865b4dae0d5307d31be3f4adf Mon Sep 17 00:00:00 2001 From: Bilal Shafi Date: Fri, 9 Aug 2024 16:50:05 +0500 Subject: [PATCH 2/4] Add ignore statements --- .../remove-stabilized-experimentalFeatures/ts-actual.spec.tsx | 2 ++ .../remove-stabilized-experimentalFeatures/ts-expected.spec.tsx | 2 ++ 2 files changed, 4 insertions(+) diff --git a/packages/x-codemod/src/v7.0.0/data-grid/remove-stabilized-experimentalFeatures/ts-actual.spec.tsx b/packages/x-codemod/src/v7.0.0/data-grid/remove-stabilized-experimentalFeatures/ts-actual.spec.tsx index 10eb22d752ff7..f1ca420a0da54 100644 --- a/packages/x-codemod/src/v7.0.0/data-grid/remove-stabilized-experimentalFeatures/ts-actual.spec.tsx +++ b/packages/x-codemod/src/v7.0.0/data-grid/remove-stabilized-experimentalFeatures/ts-actual.spec.tsx @@ -1,8 +1,10 @@ +// @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 ( diff --git a/packages/x-codemod/src/v7.0.0/data-grid/remove-stabilized-experimentalFeatures/ts-expected.spec.tsx b/packages/x-codemod/src/v7.0.0/data-grid/remove-stabilized-experimentalFeatures/ts-expected.spec.tsx index d1a1271fa5235..708f642b29735 100644 --- a/packages/x-codemod/src/v7.0.0/data-grid/remove-stabilized-experimentalFeatures/ts-expected.spec.tsx +++ b/packages/x-codemod/src/v7.0.0/data-grid/remove-stabilized-experimentalFeatures/ts-expected.spec.tsx @@ -1,8 +1,10 @@ +// @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 ( ( From 2befa5a1feea45fe80eb5cc24e6cf0cc8672df73 Mon Sep 17 00:00:00 2001 From: Bilal Shafi Date: Tue, 3 Sep 2024 16:52:45 +0500 Subject: [PATCH 3/4] Update condition --- .../x-codemod/src/util/removeObjectProperty.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/packages/x-codemod/src/util/removeObjectProperty.ts b/packages/x-codemod/src/util/removeObjectProperty.ts index 80fe75236334d..676846fec7f7d 100644 --- a/packages/x-codemod/src/util/removeObjectProperty.ts +++ b/packages/x-codemod/src/util/removeObjectProperty.ts @@ -52,15 +52,17 @@ export default function removeObjectProperty({ const objectProperties = j(targetAttribute).find(j.ObjectProperty); const propertiesToProcess = properties.length > 0 ? properties : objectProperties; - if (propertiesToProcess.length > 0) { - propertiesToProcess.forEach((path) => { - const keyName = (path.value.key as Identifier).name; - if (keyName) { - definedKeys.push(keyName); - } - }); + 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) From 3a95fa0e289402863053782072409774240f3466 Mon Sep 17 00:00:00 2001 From: Bilal Shafi Date: Tue, 3 Sep 2024 16:55:25 +0500 Subject: [PATCH 4/4] Merge the test --- .../remove-stabilized-experimentalFeatures.test.ts | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/packages/x-codemod/src/v7.0.0/data-grid/remove-stabilized-experimentalFeatures/remove-stabilized-experimentalFeatures.test.ts b/packages/x-codemod/src/v7.0.0/data-grid/remove-stabilized-experimentalFeatures/remove-stabilized-experimentalFeatures.test.ts index fd6ce1c889f2b..f0e8726f28e5c 100644 --- a/packages/x-codemod/src/v7.0.0/data-grid/remove-stabilized-experimentalFeatures/remove-stabilized-experimentalFeatures.test.ts +++ b/packages/x-codemod/src/v7.0.0/data-grid/remove-stabilized-experimentalFeatures/remove-stabilized-experimentalFeatures.test.ts @@ -10,23 +10,21 @@ 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'); }); - }); - describe('remove-stabilized-experimentalFeatures - with typescript', () => { - it('transforms props as needed', () => { + it('transforms props as needed - ts', () => { const actual = transform( { source: read('./ts-actual.spec.tsx') }, { jscodeshift: jscodeshift.withParser('tsx') }, @@ -36,7 +34,7 @@ describe('v7.0.0/data-grid', () => { expect(actual).to.equal(expected, 'The transformed version should be correct'); }); - it('should be idempotent', () => { + it('should be idempotent - ts', () => { const actual = transform( { source: read('./ts-expected.spec.tsx') }, { jscodeshift: jscodeshift.withParser('tsx') },