-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: alex <[email protected]>
- Loading branch information
1 parent
50933fe
commit e71e831
Showing
11 changed files
with
211 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
packages/x-codemod/src/v8.0.0/charts/rename-sparkline-colors-to-color/actual.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// @ts-nocheck | ||
/* eslint-disable no-restricted-imports */ | ||
import * as React from 'react'; | ||
import { SparkLineChart } from '@mui/x-charts'; | ||
|
||
const data = [1, 2]; | ||
|
||
function Chart() { | ||
const fn = (mode) => (mode === 'light' ? ['black'] : ['white']); | ||
|
||
// prettier-ignore | ||
return ( | ||
<React.Fragment> | ||
<SparkLineChart data={data} colors={['red']} /> | ||
<SparkLineChart data={data} colors={fn} /> | ||
<SparkLineChart data={data} colors={(mode) => (mode === 'light' ? ['black'] : ['white'])} /> | ||
<SparkLineChart data={data} colors="red" /> | ||
</React.Fragment> | ||
); | ||
} |
26 changes: 26 additions & 0 deletions
26
packages/x-codemod/src/v8.0.0/charts/rename-sparkline-colors-to-color/expected.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// @ts-nocheck | ||
/* eslint-disable no-restricted-imports */ | ||
import * as React from 'react'; | ||
import { SparkLineChart } from '@mui/x-charts'; | ||
|
||
const data = [1, 2]; | ||
|
||
function Chart() { | ||
const fn = (mode) => (mode === 'light' ? ['black'] : ['white']); | ||
|
||
// prettier-ignore | ||
return ( | ||
(<React.Fragment> | ||
<SparkLineChart data={data} color={'red'} /> | ||
<SparkLineChart data={data} color={typeof fn === "function" ? mode => fn(mode)?.[0] : fn} /> | ||
<SparkLineChart | ||
data={data} | ||
/* mui-x-codemod: We renamed the `colors` prop to `color`, but didn't change the value. Please ensure sure this prop receives a string or a function that returns a string. */ | ||
color={(mode) => (mode === 'light' ? ['black'] : ['white'])} /> | ||
<SparkLineChart | ||
data={data} | ||
/* mui-x-codemod: We renamed the `colors` prop to `color`, but didn't change the value. Please ensure sure this prop receives a string or a function that returns a string. */ | ||
color="red" /> | ||
</React.Fragment>) | ||
); | ||
} |
80 changes: 80 additions & 0 deletions
80
packages/x-codemod/src/v8.0.0/charts/rename-sparkline-colors-to-color/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { JsCodeShiftAPI, JsCodeShiftFileInfo } from '../../../types'; | ||
/** | ||
* @param {import('jscodeshift').FileInfo} file | ||
* @param {import('jscodeshift').API} api | ||
*/ | ||
export default function transformer(file: JsCodeShiftFileInfo, api: JsCodeShiftAPI, options: any) { | ||
const j = api.jscodeshift; | ||
|
||
const printOptions = options.printOptions; | ||
|
||
const root = j(file.source); | ||
const componentNames = ['SparkLineChart']; | ||
const props = { colors: 'color' }; | ||
|
||
const colorAttributes = root | ||
.find(j.JSXElement) | ||
.filter((path) => { | ||
return componentNames.includes((path.value.openingElement.name as any).name); | ||
}) | ||
.find(j.JSXAttribute) | ||
.filter((attribute) => Object.keys(props).includes(attribute.node.name.name as string)); | ||
|
||
return colorAttributes | ||
.forEach((attribute) => { | ||
const colorsAttributeExpression = | ||
attribute.node.value?.type === 'JSXExpressionContainer' | ||
? attribute.node.value.expression | ||
: null; | ||
|
||
let colorAttributeExpression; | ||
|
||
if (colorsAttributeExpression?.type === 'ArrayExpression') { | ||
colorAttributeExpression = colorsAttributeExpression.elements[0]; | ||
} else if (colorsAttributeExpression?.type === 'Identifier') { | ||
colorAttributeExpression = j.conditionalExpression( | ||
j.binaryExpression( | ||
'===', | ||
j.unaryExpression('typeof', colorsAttributeExpression), | ||
j.literal('function'), | ||
), | ||
j.arrowFunctionExpression( | ||
[j.identifier('mode')], | ||
j.chainExpression( | ||
j.optionalMemberExpression( | ||
j.callExpression(colorsAttributeExpression, [j.identifier('mode')]), | ||
j.literal(0), | ||
), | ||
), | ||
), | ||
colorsAttributeExpression, | ||
); | ||
} else { | ||
// Don't know how to handle this case | ||
} | ||
|
||
// Only transform the value if we know how to handle it, otherwise rename the prop and add a comment | ||
if (colorAttributeExpression) { | ||
j(attribute).replaceWith( | ||
j.jsxAttribute( | ||
j.jsxIdentifier(props[attribute.node.name.name as string]), | ||
j.jsxExpressionContainer(colorAttributeExpression), | ||
), | ||
); | ||
} else { | ||
j(attribute) | ||
.replaceWith( | ||
j.jsxAttribute( | ||
j.jsxIdentifier(props[attribute.node.name.name as string]), | ||
attribute.node.value, | ||
), | ||
) | ||
.insertBefore( | ||
j.commentBlock( | ||
" mui-x-codemod: We renamed the `colors` prop to `color`, but didn't change the value. Please ensure sure this prop receives a string or a function that returns a string. ", | ||
), | ||
); | ||
} | ||
}) | ||
.toSource(printOptions); | ||
} |
27 changes: 27 additions & 0 deletions
27
...c/v8.0.0/charts/rename-sparkline-colors-to-color/rename-sparkline-colors-to-color.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import path from 'path'; | ||
import { expect } from 'chai'; | ||
import jscodeshift from 'jscodeshift'; | ||
import transform from '.'; | ||
import readFile from '../../../util/readFile'; | ||
|
||
function read(fileName) { | ||
return readFile(path.join(__dirname, fileName)); | ||
} | ||
|
||
describe('v8.0.0/charts', () => { | ||
describe('rename-sparkline-colors-to-color', () => { | ||
it('transforms code as needed', () => { | ||
const actual = transform( | ||
{ | ||
source: read('./actual.spec.tsx'), | ||
path: require.resolve('./actual.spec.tsx'), | ||
}, | ||
{ jscodeshift: jscodeshift.withParser('tsx') }, | ||
{}, | ||
); | ||
|
||
const expected = read('./expected.spec.tsx'); | ||
expect(actual).to.equal(expected, 'The transformed version should be correct'); | ||
}); | ||
}); | ||
}); |