fix(iast): fix detached parenthesis in IAST rewrite of optional chains inside conditionals - #203
Draft
CarlesDD wants to merge 4 commits into
Draft
Conversation
IlyasShabi
previously approved these changes
Aug 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What does this PR do?
Fixes if conditions with optional-call chains ending in a CSI method (e.g.
if (value?.toLowerCase()?.includes('needle'))): the rewriter closed the parenthesis before the final call, detaching it from its receiver and making it throw at runtime. Fixed by visitingif_stmt.testviavisit_mut_withinstead ofvisit_mut_children_with, so the top-level optional-chain transform applies as it already does for while/return/ternaries.Also removes the now-redundant
visit_mut_if_stmtoverride entirely, relying on swc's defaultIfStmttraversal. This fixes a second bug where a bracelesselsebranch was never instrumented at all.Motivation
Reported in DataDog/dd-trace-js#9836. With IAST enabled, this rewrites valid code into code that throws on every execution, silently, since it only shows up at runtime.
Additional Notes
The custom visitor override for if statements predated this fix and only handled
test/cons, neveralt, causing the second bug above. It has been removed in favor of the default traversal.Describe how to test your changes
Reproduced the issue's exact repro with this repo's Rewriter against dd-trace-js's
csiMethods. Before: throws for both matching and undefined input. After: returns "hit"/"miss" correctly, and generated code closes the parenthesis after.includes('needle').While investigating, found and fixed a second bug in the same visitor: a braceless else branch (e.g.
if (x) fn(); else a?.trim();) was never instrumented, since the old override never visitedif_stmt.alt. Refactored by removing the special-case override entirely and relying on swc's default IfStmt traversal, which correctly visits test, cons, and alt.Added regression tests for both cases. Full unit suite (npm test) passes, 357/357; integration suite (npm run test:integration) passes, 6/6; cargo clippy --workspace -- -D warnings clean.
Checklist