Skip to content

fix(codemod): handle static z.extend(Schema, {...}) form#1530

Open
uttam12331 wants to merge 1 commit into
open-circle:mainfrom
uttam12331:fix/codemod-z-extend-static-form
Open

fix(codemod): handle static z.extend(Schema, {...}) form#1530
uttam12331 wants to merge 1 commit into
open-circle:mainfrom
uttam12331:fix/codemod-z-extend-static-form

Conversation

@uttam12331

@uttam12331 uttam12331 commented Jun 30, 2026

Copy link
Copy Markdown

The zod-to-valibot codemod only handled the method form Foo.extend({...}) but not the static namespace form z.extend(Foo, {...}).

When the transformer encountered z.extend(Foo, { bar: z.string() }) it treated the namespace identifier itself as the base schema and the first argument as the extension, producing garbled output instead of v.object({ ...Foo.entries, bar: v.string() }).

Root cause: The import rewrite transforms z.extend(Foo, {...}) into v.extend(Foo, {...}). The chain walker then hits v.extend as a method call with transformedExp === null, so it falls through to toValibotMethodExp with schemaExp = j.identifier('v') (the namespace) and args = [Foo, {...}] — both wrong.

Fix: In the isZodMethodName branch of the chain walker, detect when propertyName === 'extend', transformedExp === null (nothing built yet, so we are on the namespace), and exactly 2 arguments are present. In that case extract args[0] as the base schema expression and args[1] as the extension, matching the shape expected by transformExtend.

Adds a test fixture (object-extend-static) covering both a schema-reference base and an inline-object base.

Closes #1502

Summary by CodeRabbit

  • New Features
    • Improved conversion support for static schema extension patterns, including cases that reuse an existing schema or start from an inline object.
  • Bug Fixes
    • Fixed handling of static extend conversions so the generated output now uses the correct base schema and extension values.
  • Tests
    • Added new test coverage for these extension scenarios to help prevent regressions.

@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. fix A smaller enhancement or bug fix labels Jun 30, 2026
@coderabbitai

coderabbitai Bot commented Jun 30, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 5cfbeb68-b876-4f44-8bcd-d6b0105b8afe

📥 Commits

Reviewing files that changed from the base of the PR and between 0dc26ea and a7d8559.

📒 Files selected for processing (4)
  • codemod/zod-to-valibot/__testfixtures__/object-extend-static/input.ts
  • codemod/zod-to-valibot/__testfixtures__/object-extend-static/output.ts
  • codemod/zod-to-valibot/src/test-setup.test.ts
  • codemod/zod-to-valibot/src/transform/schemas-and-links/schemas-and-links.ts

Walkthrough

The PR adds support for the static namespace form z.extend(Base, { ... }) in the zod-to-valibot codemod. The transformer's isZodMethodName handling is updated to detect this form (when transformedExp is null and there are exactly two arguments), swap the schema expression to the first argument, and pass only the second argument to toValibotMethodExp. Two new test fixtures (input.ts, output.ts) and a test registration entry are added to cover both z.extend(Foo, {...}) and z.extend(z.object({...}), {...}) patterns.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly states the main fix: supporting static z.extend(Schema, {...}) in the codemod.
Linked Issues check ✅ Passed The transformer now handles static z.extend with schema references and inline objects, matching the issue's expected conversions.
Out of Scope Changes check ✅ Passed The changes stay focused on z.extend handling and matching fixtures, with no unrelated functionality added.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

No issues found across 4 files

Re-trigger cubic

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

Labels

fix A smaller enhancement or bug fix size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Codemod cannot convert z.extend

1 participant