Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/fix-render-comment-args.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-19-replace-reactdom-render": patch
---

Ignore comments inside `render(...)` / `unmountComponentAtNode(...)` when reading call arguments. A comment before the element (e.g. an eslint directive) was taken as the first argument, shifting every argument and producing code that no longer parsed. Comments before the element are now kept inside `root.render(...)`.
34 changes: 23 additions & 11 deletions codemods/replace-reactdom-render/scripts/codemod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ function findReactDomMemberImportNames(rootNode: SgNode<TSX, "program">): Set<st
return names;
}

// Comments inside the call parens are children of `arguments` too; skip them so
// they are not mistaken for positional arguments.
function callArguments(args: SgNode<TSX>): SgNode<TSX>[] {
return args.children().filter((child) => {
const kind = child.kind();
return kind !== "(" && kind !== ")" && kind !== "," && kind !== "comment";
});
}

function directExpressionStatement(node: SgNode<TSX>): SgNode<TSX> | null {
const parent = node.parent();
return parent?.kind() === "expression_statement" ? parent : null;
Expand Down Expand Up @@ -107,9 +116,7 @@ const transform: Transform<TSX> = async (root) => {
const args = call.field("arguments");
if (!args) return;

const argList = args.children().filter((child) =>
child.kind() !== "(" && child.kind() !== ")" && child.kind() !== ","
);
const argList = callArguments(args);

if (argList.length < 2) return;

Expand All @@ -122,14 +129,21 @@ const transform: Transform<TSX> = async (root) => {
const rootName = nextRootName();
const indent = getIndent(statement);
const elementText = element.text();
// Keep comments written before the element (e.g. eslint directives on the JSX line).
const leadingComments = args.children().filter((child) =>
child.kind() === "comment" && child.range().end.index <= element.range().start.index
);

let renderCall: string;
if (elementText.includes("\n")) {
const elementStartLine = sourceLines[element.range().start.line] ?? "";
const originalCol = elementStartLine.length - elementStartLine.trimStart().length;
if (elementText.includes("\n") || leadingComments.length > 0) {
const desiredCol = indent.length + 2;
const reindented = reindentText(elementText, originalCol, desiredCol);
renderCall = `${rootName}.render(\n${" ".repeat(desiredCol)}${reindented}\n${indent})`;
const reindentNode = (node: SgNode<TSX>): string => {
const startLine = sourceLines[node.range().start.line] ?? "";
const originalCol = startLine.length - startLine.trimStart().length;
return " ".repeat(desiredCol) + reindentText(node.text(), originalCol, desiredCol);
};
const body = [...leadingComments, element].map(reindentNode).join("\n");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[MEDIUM] Preserve line breaks between leading comments and the element

Joining every comment and the element with a newline changes directive targets. For example, a // @ts-expect-error immediately above /* rationale */ build("bad") correctly suppresses an argument type error before transformation. This code moves the block comment onto its own line, separating the directive from the expression; TypeScript then reports both an unused directive and the original type error. Preserve the original line relationships and add a mixed line/block comment fixture.

renderCall = `${rootName}.render(\n${body}\n${indent})`;
} else {
renderCall = `${rootName}.render(${elementText})`;
}
Expand All @@ -148,9 +162,7 @@ const transform: Transform<TSX> = async (root) => {
const args = call.field("arguments");
if (!args) return;

const argList = args.children().filter((child) =>
child.kind() !== "(" && child.kind() !== ")" && child.kind() !== ","
);
const argList = callArguments(args);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[MEDIUM] Preserve suppression directives before the unmount container

The unmount path now skips leading comments when selecting the container but discards them when generating createRoot(...). A call with // @ts-expect-error before a container typed Element | undefined type-checks before transformation, while the generated createRoot(container) fails because the suppression is gone. Preserve these directives next to the container expression in the new call and add a directive fixture; the current ordinary-comment fixture does not catch this failure.


if (argList.length < 1) return;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { render } from "react-dom";
import App from "./App";
import { createRoot } from 'react-dom/client';

const root = createRoot(document.getElementById("root"));
root.render(
// eslint-disable-next-line react/jsx-filename-extension
<App />
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { render } from "react-dom";
import App from "./App";

render(
// eslint-disable-next-line react/jsx-filename-extension
<App />,
document.getElementById("root")
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"reactdom-render-replacements": [
{
"cardinality": {
"file": "tests/comment-inside-render/input.tsx",
"pattern": "render"
},
"count": 1
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { unmountComponentAtNode } from "react-dom";
import { createRoot } from 'react-dom/client';

const root = createRoot(document.getElementById("root"));
root.unmount();
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { unmountComponentAtNode } from "react-dom";

unmountComponentAtNode(
// tear down the app root
document.getElementById("root")
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"reactdom-render-replacements": [
{
"cardinality": {
"file": "tests/comment-inside-unmount/input.tsx",
"pattern": "unmountComponentAtNode"
},
"count": 1
}
]
}
Loading