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

feat: v19 codemods without workspaces, but with one test case removal and jscodeshift upgrade #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_STORE
dist
node_modules
npm-debug.log
13 changes: 13 additions & 0 deletions codemods-v2/remove-context-provider/.codemodrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": "1.0.0",
"name": "react/19/remove-context-provider",
"private": false,
"engine": "jscodeshift",
"meta": {
"tags": ["react"],
"useCaseCategory": "migration"
},
"applicability": {
"from": [["react", "<=", "^18.0.0"]]
}
}
35 changes: 35 additions & 0 deletions codemods-v2/remove-context-provider/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Change Context.Provider to Context

## Description

This codemod will remove the usage of `Provider` for contexts; e.g., Context.Provider to Context

## Example

### Before:

```tsx
function App() {
const [theme, setTheme] = useState('light');
// ...
return (
<ThemeContext.Provider value={theme}>
<Page />
</ThemeContext.Provider>
);
}
```

### After:

```tsx
function App() {
const [theme, setTheme] = useState('light');
// ...
return (
<ThemeContext value={theme}>
<Page />
</ThemeContext>
);
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function App() {
const [theme, setTheme] = useState('light');

return (
<ThemeContext.Provider value={theme}>
<Page />
</ThemeContext.Provider>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function App() {
const [theme, setTheme] = useState('light');

return (
<ThemeContext value={theme}>
<Page />
</ThemeContext>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function App() {
const [theme, setTheme] = useState('light');

return (
<Context.Provider value={theme}>
<Page />
</Context.Provider>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function App() {
const [theme, setTheme] = useState('light');

return (
<Context value={theme}>
<Page />
</Context>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function App() {
const [theme, setTheme] = useState('light');

return (
<Context value={theme}>
<Page />
</Context>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function App() {
const [theme, setTheme] = useState('light');

return (
<Context value={theme}>
<Page />
</Context>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function App({ url }: { url: string }) {
const [theme, setTheme] = useState<'light' | 'dark'>('light');

return (
<ThemeContext.Provider value={theme}>
<Page />
</ThemeContext.Provider>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function App({ url }: { url: string }) {
const [theme, setTheme] = useState<'light' | 'dark'>('light');

return (
<ThemeContext value={theme}>
<Page />
</ThemeContext>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function App({ url }: { url: string }) {
const [theme, setTheme] = useState<'light' | 'dark'>('light');

return (
<Context.Provider value={theme}>
<Page />
</Context.Provider>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function App({ url }: { url: string }) {
const [theme, setTheme] = useState<'light' | 'dark'>('light');

return (
<Context value={theme}>
<Page />
</Context>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function App({ url }: { url: string }) {
const [theme, setTheme] = useState<'light' | 'dark'>('light');

return (
<Context value={theme}>
<Page />
</Context>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function App({ url }: { url: string }) {
const [theme, setTheme] = useState<'light' | 'dark'>('light');

return (
<Context value={theme}>
<Page />
</Context>
);
}
3 changes: 3 additions & 0 deletions codemods-v2/remove-context-provider/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import type { API, FileInfo } from "jscodeshift";

export default function transform(file: FileInfo, api: API): string;
37 changes: 37 additions & 0 deletions codemods-v2/remove-context-provider/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { API, FileInfo } from 'jscodeshift';

export default function transform(
file: FileInfo,
api: API,
): string | undefined {
const j = api.jscodeshift;
const root = j(file.source);

root.findJSXElements().forEach((elementPath) => {
const { value } = elementPath;
const elements = [value.openingElement, value.closingElement];
elements.forEach((element) => {
if (!element) {
return;
}
if (
!j.JSXMemberExpression.check(element.name) ||
!j.JSXIdentifier.check(element.name.object)
) {
return;
}

const objectName = element.name.object.name;
const propertyName = element.name.property.name;

if (
objectName.toLocaleLowerCase().includes('context') &&
propertyName === 'Provider'
) {
element.name = element.name.object;
}
});
});

return root.toSource();
}
129 changes: 129 additions & 0 deletions codemods-v2/remove-context-provider/test/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import assert from 'node:assert/strict';
import { readFile } from 'node:fs/promises';
import { join } from 'node:path';
import jscodeshift, { API, FileInfo } from 'jscodeshift';
import { describe, it } from 'vitest';
import transform from '../src/index';

export const buildApi = (parser: string | undefined): API => ({
j: parser ? jscodeshift.withParser(parser) : jscodeshift,
jscodeshift: parser ? jscodeshift.withParser(parser) : jscodeshift,
stats: () => {
console.error(
'The stats function was called, which is not supported on purpose',
);
},
report: () => {
console.error(
'The report function was called, which is not supported on purpose',
);
},
});

describe('Context.Provider -> Context', () => {
describe('javascript code', () => {
it('should replace ThemeContext.Provider with ThemeContext', async () => {
const INPUT = await readFile(join(__dirname, '..', '__testfixtures__/fixture1.input.jsx'), 'utf-8');
const OUTPUT = await readFile(join(__dirname, '..', '__testfixtures__/fixture1.output.jsx'), 'utf-8');

const fileInfo: FileInfo = {
path: 'index.ts',
source: INPUT,
};

const actualOutput = transform(fileInfo, buildApi('js'));

assert.deepEqual(
actualOutput?.replace(/\W/gm, ''),
OUTPUT.replace(/\W/gm, ''),
);
});

it('should replace Context.Provider with Context', async () => {
const INPUT = await readFile(join(__dirname, '..', '__testfixtures__/fixture2.input.jsx'), 'utf-8');
const OUTPUT = await readFile(join(__dirname, '..', '__testfixtures__/fixture2.output.jsx'), 'utf-8');

const fileInfo: FileInfo = {
path: 'index.ts',
source: INPUT,
};

const actualOutput = transform(fileInfo, buildApi('js'));

assert.deepEqual(
actualOutput?.replace(/\W/gm, ''),
OUTPUT.replace(/\W/gm, '')
);
});

it('should do nothing if .Provider does not exist', async () => {
const INPUT = await readFile(join(__dirname, '..', '__testfixtures__/fixture3.input.jsx'), 'utf-8');
const OUTPUT = await readFile(join(__dirname, '..', '__testfixtures__/fixture3.output.jsx'), 'utf-8');

const fileInfo: FileInfo = {
path: 'index.ts',
source: INPUT,
};

const actualOutput = transform(fileInfo, buildApi('js'));

assert.deepEqual(
actualOutput?.replace(/\W/gm, ''),
OUTPUT.replace(/\W/gm, '')
);
});
});

describe('typescript code', () => {
it('should replace ThemeContext.Provider with ThemeContext', async () => {
const INPUT = await readFile(join(__dirname, '..', '__testfixtures__/fixture4.input.tsx'), 'utf-8');
const OUTPUT = await readFile(join(__dirname, '..', '__testfixtures__/fixture4.output.tsx'), 'utf-8');

const fileInfo: FileInfo = {
path: 'index.ts',
source: INPUT,
};

const actualOutput = transform(fileInfo, buildApi('tsx'));

assert.deepEqual(
actualOutput?.replace(/\W/gm, ''),
OUTPUT.replace(/\W/gm, '')
);
});

it('should replace Context.Provider with Context', async () => {
const INPUT = await readFile(join(__dirname, '..', '__testfixtures__/fixture5.input.tsx'), 'utf-8');
const OUTPUT = await readFile(join(__dirname, '..', '__testfixtures__/fixture5.output.tsx'), 'utf-8');

const fileInfo: FileInfo = {
path: 'index.ts',
source: INPUT,
};

const actualOutput = transform(fileInfo, buildApi('tsx'));

assert.deepEqual(
actualOutput?.replace(/\W/gm, ''),
OUTPUT.replace(/\W/gm, '')
);
});

it('should do nothing if .Provider does not exist', async () => {
const INPUT = await readFile(join(__dirname, '..', '__testfixtures__/fixture6.input.tsx'), 'utf-8');
const OUTPUT = await readFile(join(__dirname, '..', '__testfixtures__/fixture6.output.tsx'), 'utf-8');

const fileInfo: FileInfo = {
path: 'index.ts',
source: INPUT,
};

const actualOutput = transform(fileInfo, buildApi('tsx'));

assert.deepEqual(
actualOutput?.replace(/\W/gm, ''),
OUTPUT.replace(/\W/gm, '')
);
});
});
});
14 changes: 14 additions & 0 deletions codemods-v2/remove-forward-ref/.codemodrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"version": "1.0.0",
"private": false,
"name": "react/19/remove-forward-ref",
"description": "Codemod to remove React.forwardRef function that will be deprecated in next major React release",
"engine": "jscodeshift",
"meta": {
"tags": ["react"],
"useCaseCategory": "migration"
},
"applicability": {
"from": [["react", "<=", "^18.0.0"]]
}
}
Loading