Skip to content

JavaScript: upgrade mutative to 2.0.2 - #10173

Merged
timtebeek merged 1 commit into
mainfrom
tim/mutative-2.x-adopt
Dec 1, 2026
Merged

timtebeek merged 1 commit into
mainfrom
tim/mutative-2.x-adopt

Conversation

@timtebeek

Copy link
Copy Markdown
Member

Mutative 2.0 keeps the 1.x API. Its release notes report it as faster than 1.3.0 (4.3x on the geometric mean of its own benchmarks) and fix several cases where 1.x produced wrong states. 1.3.0 is the last 1.x release, and fixes are not being backported. The floor is ^2.0.2 because 2.0.2 (unadlib/mutative#218) fixes a bug that 1.3.0 also has: an inner create() that assigns a draft from an outer create() could finalize it early, so later outer reads could mutate the base state. We nest produce calls throughout the parser and formatter. I found no place where we actually hit this, so it reduces risk rather than fixes a known bug.

The only change that showed up in our code is a new development-build warning when create() is given a draft. In 1.x the draft was already handled the same way, silently. Four helpers did this from inside recipes:

  • maybeAddOptionalMarker in the parser now adds the marker to the draft directly.
  • BlankLinesVisitor now uses a new updateLastWhitespace, the in-place counterpart of replaceLastWhitespace. replaceLastWhitespace still copies, so callers outside this repository see no change.
  • WrappingAndBracesVisitor now passes the original Space to withNewlineSpace instead of the draft.

Since the Node package is consumed as CommonJS, the ESM, bundler and NODE_ENV changes in 2.0 don't apply. Its CommonJS entry is identical to 1.3.0's. We also don't use patches, apply(), auto-freeze, mark, strict mode or Map/Set drafts, which is where most of the migration guide's breaking changes are. Test timings didn't change, because our recipes assign fields and push onto arrays rather than using the array and Set operations that 2.0 speeds up.

Verification:

  • Local suite: npm run typecheck, npm run build and npx vitest run pass: 3730 passed, 0 failed. I ran the suite once with mutative's warn-once guard patched out, to find every call site rather than just the first, and the fixed run prints none of 2.0's warnings.
  • Static scan: a scan for helpers that copy a parameter with create() and are called with a draft finds all four helpers on main (11 call sites) and none on this branch. A second scan found no inner recipe that stores an outer recipe's draft.
  • Downstream: recipes-angular and recipes-react declare their own mutative ^1.x, so after this change they load 1.3.0 next to our 2.0.2. Both copies mark drafts with the same Symbol.for key, and nesting either version's create() inside the other's, plus current() and original() across versions, gave correct results. I ran both packages' test suites from fresh clones against this branch packed as a tarball, and against the same commit built with mutative 1.3.0. The results were identical: angular 1511 passed, react 2917 passed. The same tests failed on both versions: 31 in angular's java-rpc tests (no Java classpath locally) and 24 in react.

Mutative 2 warns when create() is given a draft. Four helpers did that
from inside recipes, so they now change the draft in place or are handed
the original value instead.
@timtebeek

Copy link
Copy Markdown
Member Author

@knutwannheden based on our earlier discussion figured bump mutative once here. the API remained the same; the changes you're seeing are to prevent warnings being printed where we were using their API incorrectly. The results should remain the same.

@github-project-automation github-project-automation Bot moved this from In Progress to Ready to Review in OpenRewrite Dec 1, 2026
@timtebeek
timtebeek merged commit c9f17af into main Dec 1, 2026
1 check passed
@timtebeek
timtebeek deleted the tim/mutative-2.x-adopt branch December 1, 2026 15:18
@github-project-automation github-project-automation Bot moved this from Ready to Review to Done in OpenRewrite Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants