Skip to content

Conversation

@mathpirate
Copy link
Contributor

@mathpirate mathpirate commented Oct 28, 2025

Make it work when there's a name collision in map captures. This should be superseded soon by a larger refactor to avoid rewriting callback bodies.


Summary by cubic

Fixes map callback capture name collisions and correctly handles destructured aliases (including computed, string, and numeric keys). Prevents wrong property access and shadowing in transformed map closures.

  • Bug Fixes
    • Generate unique capture names when collisions occur; skip duplicates for equivalent expressions and avoid reserved names.
    • Map destructured aliases to the right element access for identifiers, string/numeric literals, and computed property names.
    • Added regression fixtures for alias cases to prevent future regressions.

Copy link
Contributor

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

Choose a reason for hiding this comment

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

No issues found across 9 files

const elemName = elemParam?.name;

// Collect destructured property names if the param is an object destructuring pattern
const destructuredProps = new Set<string>();
Copy link
Collaborator

Choose a reason for hiding this comment

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

I wonder if NodeFactory#createTempVariable or NodeFactory#createUniqueName could be helpful in synthesizing unused var names

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks for pointing those out, i'm glad to know about them. i think they might be a little overly complex for this exact usage, in that they return Identifiers and we'd need to get the text for either synthetic or non-synthetic nodes; they're also not fully deterministic so it might be a bit tricky to make fixtures for them although maybe they're deterministic-enough for a fixed input, i'm not sure

either way though i intend to deprecate this mechanism entirely in my next larger follow-up change to avoid rewriting closure bodies, so i'm going to leave this for now and come back to it later if that turns out not to be true

Copy link
Contributor

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

Choose a reason for hiding this comment

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

1 issue found across 11 files

Prompt for AI agents (all 1 issues)

Understand the root cause of the following 1 issues and fix them.


<file name="packages/ts-transformers/src/closures/transformer.ts">

<violation number="1" location="packages/ts-transformers/src/closures/transformer.ts:933">
Transforming destructured aliases with computed property names into direct element accesses re-evaluates the property expression (`counter++`, function calls, etc.) on every use, so aliases no longer behave like bound values. This breaks callbacks that rely on the original single evaluation.</violation>
</file>

React with 👍 or 👎 to teach cubic. Mention @cubic-dev-ai to give feedback, ask questions, or re-run the review.

}

if (ts.isComputedPropertyName(propertyName)) {
return factory.createElementAccessExpression(
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot Oct 28, 2025

Choose a reason for hiding this comment

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

Transforming destructured aliases with computed property names into direct element accesses re-evaluates the property expression (counter++, function calls, etc.) on every use, so aliases no longer behave like bound values. This breaks callbacks that rely on the original single evaluation.

Prompt for AI agents
Address the following comment on packages/ts-transformers/src/closures/transformer.ts at line 933:

<comment>Transforming destructured aliases with computed property names into direct element accesses re-evaluates the property expression (`counter++`, function calls, etc.) on every use, so aliases no longer behave like bound values. This breaks callbacks that rely on the original single evaluation.</comment>

<file context>
@@ -898,11 +898,49 @@ function transformDestructuredProperties(
+          }
+
+          if (ts.isComputedPropertyName(propertyName)) {
+            return factory.createElementAccessExpression(
+              target,
+              propertyName.expression,
</file context>

✅ Addressed in 839a59e

Copy link
Contributor

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

Choose a reason for hiding this comment

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

No issues found across 6 files

@mathpirate mathpirate merged commit b62e7c1 into main Oct 29, 2025
9 checks passed
@mathpirate mathpirate deleted the fix-map-capture-collisions branch October 29, 2025 21:01
jkomoros pushed a commit that referenced this pull request Oct 30, 2025
* Fix map closure destructured alias handling and add regression fixtures

* Fix map closure capture name collisions and add regression fixture

* Avoid recomputing computed destructured aliases and add fixture
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.

3 participants