Repository navigation
fix(replace-reactdom-render): skip comments when reading call arguments #22
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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(...)`. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -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; | ||
|
|
||
|
|
@@ -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"); | ||
| renderCall = `${rootName}.render(\n${body}\n${indent})`; | ||
| } else { | ||
| renderCall = `${rootName}.render(${elementText})`; | ||
| } | ||
|
|
@@ -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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
|
||
| if (argList.length < 1) return; | ||
|
|
||
|
|
||
| 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 | ||
| } | ||
| ] | ||
| } |
There was a problem hiding this comment.
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-errorimmediately 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.