forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add replace-act codemod and update configs
- Loading branch information
Showing
22 changed files
with
460 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"version": "1.0.0", | ||
"name": "react/19/replace-act-import", | ||
"private": false, | ||
"engine": "jscodeshift", | ||
"meta": { | ||
"tags": ["react", "migration"] | ||
}, | ||
"applicability": { | ||
"from": [["react", "<=", "18"]] | ||
} | ||
} |
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,42 @@ | ||
# Replace react dom test utils with react | ||
|
||
## Description | ||
|
||
This codemod will replace the usages of `TestUtils.act()` to use `React.act()`, introduced in React v19. | ||
|
||
## Examples | ||
|
||
### Before | ||
|
||
```ts | ||
import { act } from 'react-dom/test-utils'; | ||
|
||
act(); | ||
``` | ||
|
||
### After | ||
|
||
```ts | ||
import { act } from "react"; | ||
|
||
act(); | ||
``` | ||
|
||
|
||
|
||
### Before | ||
|
||
```ts | ||
import * as ReactDOMTestUtils from 'react-dom/test-utils'; | ||
|
||
ReactDOMTestUtils.act(); | ||
``` | ||
|
||
### After | ||
|
||
```ts | ||
import * as React from "react"; | ||
|
||
React.act(); | ||
``` | ||
|
2 changes: 2 additions & 0 deletions
2
codemods/replace-act-import/__testfixtures__/fixture1.input.js
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,2 @@ | ||
import {act} from 'react-dom/test-utils'; | ||
act(); |
2 changes: 2 additions & 0 deletions
2
codemods/replace-act-import/__testfixtures__/fixture1.output.js
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,2 @@ | ||
import {act} from 'react'; | ||
act(); |
2 changes: 2 additions & 0 deletions
2
codemods/replace-act-import/__testfixtures__/fixture2.input.js
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,2 @@ | ||
import ReactTestUtils from 'react-dom/test-utils'; | ||
ReactTestUtils.act(); |
2 changes: 2 additions & 0 deletions
2
codemods/replace-act-import/__testfixtures__/fixture2.output.js
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,2 @@ | ||
import React from 'react'; | ||
React.act(); |
2 changes: 2 additions & 0 deletions
2
codemods/replace-act-import/__testfixtures__/fixture3.input.js
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,2 @@ | ||
import * as ReactTestUtils from 'react-dom/test-utils'; | ||
ReactTestUtils.act(); |
2 changes: 2 additions & 0 deletions
2
codemods/replace-act-import/__testfixtures__/fixture3.output.js
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,2 @@ | ||
import * as React from 'react'; | ||
React.act(); |
2 changes: 2 additions & 0 deletions
2
codemods/replace-act-import/__testfixtures__/fixture4.input.js
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,2 @@ | ||
import {other} from 'react-dom/test-utils'; | ||
other.thing(); |
2 changes: 2 additions & 0 deletions
2
codemods/replace-act-import/__testfixtures__/fixture4.output.js
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,2 @@ | ||
import {other} from 'react-dom/test-utils'; | ||
other.thing(); |
4 changes: 4 additions & 0 deletions
4
codemods/replace-act-import/__testfixtures__/fixture5.input.js
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,4 @@ | ||
import * as React from 'react'; | ||
import * as ReactTestUtils from 'react-dom/test-utils'; | ||
|
||
ReactTestUtils.act(); |
3 changes: 3 additions & 0 deletions
3
codemods/replace-act-import/__testfixtures__/fixture5.output.js
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,3 @@ | ||
import * as React from 'react'; | ||
|
||
React.act(); |
3 changes: 3 additions & 0 deletions
3
codemods/replace-act-import/__testfixtures__/fixture6.input.js
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,3 @@ | ||
import {FC} from 'react'; | ||
import {act} from 'react-dom/test-utils'; | ||
act(); |
2 changes: 2 additions & 0 deletions
2
codemods/replace-act-import/__testfixtures__/fixture6.output.js
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,2 @@ | ||
import {FC, act} from 'react'; | ||
act(); |
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,174 @@ | ||
import assert from 'node:assert/strict'; | ||
import {readFile} from 'node:fs/promises'; | ||
import {join} from 'node:path'; | ||
import jscodeshift, {type API, type 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('react/19/replace-act-import: TestUtils.act -> React.act', () => { | ||
describe('javascript code', () => { | ||
it('should replace direct import with import from react', 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'), { | ||
quote: 'single', | ||
}); | ||
|
||
assert.deepEqual( | ||
actualOutput?.replace(/\W/gm, ''), | ||
OUTPUT.replace(/\W/gm, '') | ||
); | ||
}); | ||
|
||
it('should replace TestUtils.act with React.act', 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'), { | ||
quote: 'single', | ||
}); | ||
|
||
assert.deepEqual( | ||
actualOutput?.replace(/\W/gm, ''), | ||
OUTPUT.replace(/\W/gm, '') | ||
); | ||
}); | ||
|
||
it('should properly replace star import', 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'), { | ||
quote: 'single', | ||
}); | ||
|
||
assert.deepEqual( | ||
actualOutput?.replace(/\W/gm, ''), | ||
OUTPUT.replace(/\W/gm, '') | ||
); | ||
}); | ||
|
||
it('should not replace other imports from test utils', async () => { | ||
const INPUT = await readFile( | ||
join(__dirname, '..', '__testfixtures__/fixture4.input.js'), | ||
'utf-8' | ||
); | ||
const OUTPUT = await readFile( | ||
join(__dirname, '..', '__testfixtures__/fixture4.output.js'), | ||
'utf-8' | ||
); | ||
|
||
const fileInfo: FileInfo = { | ||
path: 'index.ts', | ||
source: INPUT, | ||
}; | ||
|
||
const actualOutput = transform(fileInfo, buildApi('js'), { | ||
quote: 'single', | ||
}); | ||
|
||
assert.deepEqual( | ||
actualOutput?.replace(/\W/gm, ''), | ||
OUTPUT.replace(/\W/gm, '') | ||
); | ||
}); | ||
|
||
it('should not add react import if one is already present', async () => { | ||
const INPUT = await readFile( | ||
join(__dirname, '..', '__testfixtures__/fixture5.input.js'), | ||
'utf-8' | ||
); | ||
const OUTPUT = await readFile( | ||
join(__dirname, '..', '__testfixtures__/fixture5.output.js'), | ||
'utf-8' | ||
); | ||
|
||
const fileInfo: FileInfo = { | ||
path: 'index.ts', | ||
source: INPUT, | ||
}; | ||
|
||
const actualOutput = transform(fileInfo, buildApi('js'), { | ||
quote: 'single', | ||
}); | ||
|
||
assert.deepEqual( | ||
actualOutput?.replace(/\W/gm, ''), | ||
OUTPUT.replace(/\W/gm, '') | ||
); | ||
}); | ||
|
||
it('should add import specifier to existing import if it exists', async () => { | ||
const INPUT = await readFile( | ||
join(__dirname, '..', '__testfixtures__/fixture6.input.js'), | ||
'utf-8' | ||
); | ||
const OUTPUT = await readFile( | ||
join(__dirname, '..', '__testfixtures__/fixture6.output.js'), | ||
'utf-8' | ||
); | ||
|
||
const fileInfo: FileInfo = { | ||
path: 'index.ts', | ||
source: INPUT, | ||
}; | ||
|
||
const actualOutput = transform(fileInfo, buildApi('js'), { | ||
quote: 'single', | ||
}); | ||
|
||
assert.deepEqual( | ||
actualOutput?.replace(/\W/gm, ''), | ||
OUTPUT.replace(/\W/gm, '') | ||
); | ||
}); | ||
}); | ||
}); |
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,17 @@ | ||
{ | ||
"name": "replace-act-import", | ||
"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" | ||
} |
Oops, something went wrong.