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: react v19 codemods #1

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
3 changes: 3 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,9 @@ workflows:
- "-r=www-modern --env=development --variant=true"
- "-r=www-modern --env=production --variant=true"

# Codemods
- "--project=codemods -r=experimental"

# TODO: Test more persistent configurations?
- '-r=stable --env=development --persistent'
- '-r=experimental --env=development --persistent'
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.DS_STORE
dist
node_modules
scripts/flow/*/.flowconfig
.flowconfig
Expand Down
4 changes: 4 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
'use strict';

module.exports = {
presets: [
['@babel/preset-env', {targets: {node: 'current'}}],
'@babel/preset-typescript',
],
plugins: [
'@babel/plugin-syntax-jsx',
'@babel/plugin-transform-flow-strip-types',
Expand Down
13 changes: 13 additions & 0 deletions codemods/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"
r4zendev marked this conversation as resolved.
Show resolved Hide resolved
},
"applicability": {
"from": [["react", "<=", "^18.0.0"]]
r4zendev marked this conversation as resolved.
Show resolved Hide resolved
}
}
35 changes: 35 additions & 0 deletions codemods/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>
);
}
164 changes: 164 additions & 0 deletions codemods/remove-context-provider/__tests__/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
import assert from 'node:assert/strict';
import {readFile} from 'node:fs/promises';
import {join} from 'node:path';
import jscodeshift, {API, FileInfo} from 'jscodeshift';
import transform from '../src/index.ts';

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.js'),
'utf-8'
);
const OUTPUT = await readFile(
join(__dirname, '..', '__testfixtures__/fixture1.output.js'),
'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.js'),
'utf-8'
);
const OUTPUT = await readFile(
join(__dirname, '..', '__testfixtures__/fixture2.output.js'),
'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.js'),
'utf-8'
);
const OUTPUT = await readFile(
join(__dirname, '..', '__testfixtures__/fixture3.output.js'),
'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.ts'),
'utf-8'
);
const OUTPUT = await readFile(
join(__dirname, '..', '__testfixtures__/fixture4.output.ts'),
'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.ts'),
'utf-8'
);
const OUTPUT = await readFile(
join(__dirname, '..', '__testfixtures__/fixture5.output.ts'),
'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.ts'),
'utf-8'
);
const OUTPUT = await readFile(
join(__dirname, '..', '__testfixtures__/fixture6.output.ts'),
'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, '')
);
});
});
});
17 changes: 17 additions & 0 deletions codemods/remove-context-provider/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "remove-context-provider",
"version": "1.0.0",
"dependencies": {},
"devDependencies": {
"@types/node": "^20.12.3",
"@types/jscodeshift": "^0.11.10",
"jscodeshift": "^0.15.1",
"typescript": "^5.2.2"
},
"files": [
"./README.md",
"./.codemodrc.json",
"./dist/index.cjs"
],
"type": "module"
}
Loading