Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fixup! Convert ReactDOMTextarea to createRoot
  • Loading branch information
Sebastian Silbermann committed Jan 27, 2024
commit ce278d4972b76e46dfc312b757190e660cb7c31f
2 changes: 0 additions & 2 deletions packages/react-dom/src/__tests__/ReactDOMTextarea-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,9 @@ describe('ReactDOMTextarea', () => {
await act(() => {
root.render(<textarea defaultValue="foo" />);
});

await act(() => {
root.render(<textarea defaultValue="bar" />);
});

await act(() => {
root.render(<textarea defaultValue="noise" />);
});
Expand Down
201 changes: 201 additions & 0 deletions packages/react/src/__tests__/codemod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
// jscodeshift can take a parser, like "babel", "babylon", "flow", "ts", or "tsx"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test failures seem to be just from trying to run this as a test

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops 😄

// Read more: https://github.com/facebook/jscodeshift#parser
export const parser = 'flow';

// Press ctrl+space for code completion
export default function transformer(file, api) {
const j = api.jscodeshift;

return j(file.source)
.find(j.ExpressionStatement)
.forEach(path => {
const {
node: {
expression: {callee},
},
} = path;

if (
callee !== undefined &&
callee.type === 'MemberExpression' &&
callee.object.type === 'Identifier' &&
callee.object.name === 'ReactDOM' &&
callee.property.type === 'Identifier' &&
callee.property.name === 'render'
) {
// replace ReactDOM.render with
// const root = ReactDOMClient.createRoot(container);
// await act(() => { root.render() })
const [element, container] = path.node.expression.arguments;
j(path).insertBefore([
j.variableDeclaration('const', [
j.variableDeclarator(
j.identifier('root'),
j.callExpression(
j.memberExpression(
j.identifier('ReactDOMClient'),
j.identifier('createRoot'),
),
[container],
),
),
]),
]);
j(path).replaceWith(
j.expressionStatement(
j.awaitExpression(
j.callExpression(j.identifier('act'), [
j.arrowFunctionExpression(
[],
j.blockStatement([
j.expressionStatement(
j.callExpression(
j.memberExpression(
j.identifier('root'),
j.identifier('render'),
),
[element],
),
),
]),
),
]),
),
),
);

path.parent.parent.value.async = true;
} else if (
callee !== undefined &&
callee.type === 'MemberExpression' &&
callee.object.type === 'Identifier' &&
callee.object.name === 'ReactTestUtils' &&
callee.property.type === 'Identifier' &&
callee.property.name === 'renderIntoDocument'
) {
// replace ReactTestUtils.renderIntoDocument with
// const container = document.createElement('div')
// const root = ReactDOMClient.createRoot(container);
// await act(() => { root.render() })
const [element] = path.node.expression.arguments;
j(path).insertBefore([
j.variableDeclaration('const', [
j.variableDeclarator(
j.identifier('container'),
j.callExpression(j.identifier('document.createElement'), [
j.literal('div'),
]),
),
]),
j.variableDeclaration('const', [
j.variableDeclarator(
j.identifier('root'),
j.callExpression(
j.memberExpression(
j.identifier('ReactDOMClient'),
j.identifier('createRoot'),
),
[j.identifier('container')],
),
),
]),
]);
j(path).replaceWith(
j.expressionStatement(
j.awaitExpression(
j.callExpression(j.identifier('act'), [
j.arrowFunctionExpression(
[],
j.blockStatement([
j.expressionStatement(
j.callExpression(
j.memberExpression(
j.identifier('root'),
j.identifier('render'),
),
[element],
),
),
]),
),
]),
),
),
);

path.parent.parent.value.async = true;
}
})
.find(j.VariableDeclaration)
.forEach(path => {
const {
node: {declarations},
} = path;
if (
declarations.length === 1 &&
declarations[0].init !== null &&
declarations[0].init.type === 'CallExpression'
) {
const {arguments: args, callee} = declarations[0].init;
if (
callee !== undefined &&
callee.type === 'MemberExpression' &&
callee.object.type === 'Identifier' &&
callee.object.name === 'ReactDOM' &&
callee.property.type === 'Identifier' &&
callee.property.name === 'render'
) {
// replace const node = ReactDOM.render(element, container) with
// const root = ReactDOMClient.createRoot(container);
// await act(() => { root.render() })
// const node = container.firstChild;
const [element, container] = args;
j(path).insertBefore([
j.variableDeclaration('const', [
j.variableDeclarator(
j.identifier('root'),
j.callExpression(
j.memberExpression(
j.identifier('ReactDOMClient'),
j.identifier('createRoot'),
),
[container],
),
),
]),
j.expressionStatement(
j.awaitExpression(
j.callExpression(j.identifier('act'), [
j.arrowFunctionExpression(
[],
j.blockStatement([
j.expressionStatement(
j.callExpression(
j.memberExpression(
j.identifier('root'),
j.identifier('render'),
),
[element],
),
),
]),
),
]),
),
),
]);
j(path).replaceWith(
j.variableDeclaration('const', [
j.variableDeclarator(
j.identifier(declarations[0].id.name),
j.identifier('container.firstChild'),
),
]),
);

path.parent.parent.value.async = true;
}
}
})
.toSource();
}