Skip to content

replace-reactdom-render: comment inside render(...) shifts arguments and produces invalid code #21

Open

Description

@enismustafaj

react-19-replace-reactdom-render@0.1.4 produces invalid code when a comment is inside the render(...) call. This pattern is common because of eslint directives on JSX in .js files.

Input

import { render } from "react-dom";
import App from "./App";

render(
  // eslint-disable-next-line react/jsx-filename-extension
  <App />,
  document.getElementById("root")
);

Output (codemod run react-19-replace-reactdom-render@0.1.4, CLI 1.18.6)

import { render } from "react-dom";
import App from "./App";
import { createRoot } from 'react-dom/client';

const root = createRoot(<App />);
root.render(// eslint-disable-next-line react/jsx-filename-extension);
(document.getElementById("root"))();

The comment is taken as the first argument, so every argument shifts by one:

  • createRoot receives the element and not the container.
  • root.render receives the comment, which also comments out its closing );, so the file no longer parses (tsc: error TS1005: ')' expected.).
  • The container is left as a stray (...)() call.

Expected

import { createRoot } from 'react-dom/client';
import App from "./App";

const root = createRoot(document.getElementById("root"));
root.render(
  // eslint-disable-next-line react/jsx-filename-extension
  <App />
);

(Keeping the comment and removing the now-unused render import would be ideal; dropping the comment is acceptable.)

Likely cause

In codemods/replace-reactdom-render/scripts/codemod.ts, applyRenderReplacement builds the argument list by filtering out only (, ) and ,:

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

comment nodes are also children of arguments, so argList[0] is the comment. Filtering child.kind() !== "comment" as well (here and in the similar filter around line 151) should fix the argument positions. None of the existing fixtures under tests/ has a comment inside the call.

Real-world occurrence

inaturalist/inaturalist at 96f627e: the codemod modifies 21 files, and 3 of them stop parsing:

  • app/webpack/observations/identify/webpack-entry.js
  • app/webpack/stats/year/webpack-entry.js
  • app/webpack/users/edit/webpack-entry.js

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions