Skip to content

fix(replace-reactdom-render): skip comments when reading call arguments - #22

Open
devagrawal09 wants to merge 1 commit into
mainfrom
fix/issue-21
Open

devagrawal09 wants to merge 1 commit into
mainfrom
fix/issue-21

Conversation

@devagrawal09

Copy link
Copy Markdown

Fixes #21.

A comment inside render(...) or unmountComponentAtNode(...) was read as an argument, so every argument shifted by one and the output didn't parse.

  • New callArguments() helper skips comment nodes; both call sites use it.
  • Comments before the element are kept inside the new root.render(...) call, which then spans several lines so a // comment can't swallow the closing ).
  • Fixtures: tests/comment-inside-render (the issue's input) and tests/comment-inside-unmount. Both fail without the fix.
  • Patch changeset added.

Not handled: a comment after the element (<App />, // note) is still dropped; the output stays valid.

…ts (#21)

Comment nodes are children of the call's arguments node, so a comment
inside render(...) was treated as the first argument and every argument
shifted: createRoot got the element, root.render got the comment (which
swallowed the closing paren), and the container became a stray call.

Filter comment nodes out of the argument list for both render and
unmountComponentAtNode, and keep comments that precede the element inside
the generated root.render(...) call.
@alexbit-codemod

Copy link
Copy Markdown
Contributor

/codex-review

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.

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.

@github-actions

Copy link
Copy Markdown
Contributor

Codex Review

All 16 fixtures and the transform type check pass. Additional checks reproduced two comment-directive edge cases that produce TypeScript errors.

Findings

  • [MEDIUM] codemods/replace-reactdom-render/scripts/codemod.ts:145 Preserve line breaks between leading comments and the element
  • [MEDIUM] codemods/replace-reactdom-render/scripts/codemod.ts:165 Preserve suppression directives before the unmount container

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants