Fix: Prevent map transformation when inside derive and exclude untransformed map parameters #1960
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes two related issues with map transformations in JSX expressions:
Prevents
.mapWithPattern()errors when map is inside derive: Stops transforming.map()to.mapWithPattern()when the map will be wrapped in aderive(), since the array gets unwrapped inside the derive and plain.map()is needed.Fixes incorrect parameter capture in outer derives: Prevents untransformed map callback parameters from being incorrectly captured in outer derive calls.
Problem
When a map is nested in an expression with opaque refs (e.g.,
list.length > 0 && list.map(...)), the OpaqueRefJSXTransformer wraps the entire expression inderive(list, list => ...). This causes two issues:.map()to.mapWithPattern(), this causes runtime errors because unwrapped arrays don't havemapWithPattern()item.namefrom inside the map callback)Solution
Fix 1: Skip map transformation when inside derive
shouldTransformMap()function inclosures/transformer.tsthat uses dataflow analysisanalysis.requiresRewrite && !skip-call-rewritehint to distinguish derive wrapping from other transformations.map()which works on unwrapped arraysFix 2: Exclude untransformed map parameters from outer scope
getOpaqueCallKindForParameter()intransformers/opaque-ref/helpers.tscontext.isMapCallback()to determine if a callback was actually transformedmapWithPattern.map()inside derives) have regular parameters that shouldn't be capturedExample
Input:
Before (broken):
After (fixed):
Test Plan
map-array-length-conditionalfor the specific use case🤖 Generated with Claude Code
Summary by cubic
Prevents transforming .map() to .mapWithPattern() when the map is inside a derive-wrapped JSX expression, avoiding runtime errors on plain arrays. Also stops untransformed map callback parameters from being captured by outer derive dependencies.