Skip to content

fix(transpiler): keep for-loop counters raw in switch-arm return arguments - #318

Open
yusufcansehirli wants to merge 1 commit into
LuxAlgo:devfrom
yusufcansehirli:fix/for-loop-counter-in-return-argument
Open

yusufcansehirli wants to merge 1 commit into
LuxAlgo:devfrom
yusufcansehirli:fix/for-loop-counter-in-return-argument

Conversation

@yusufcansehirli

Copy link
Copy Markdown

Problem

transpile() can emit invalid JavaScript for a for loop whose counter is reached through the complex-return-argument walker in transformReturnStatement():

for (let iR = 1; 1 <= 3 ? $.get(iR, 0) <= 3 : $.get(iR, 0) >= 3; 1 <= 3 ? $.get(iR, 0)++ : $.get(iR, 0)--) {

$.get(iR, 0)++ is a postfix update on a call expression, which is invalid JavaScript — SyntaxError: Invalid left-hand side expression in postfix operation when the generated function is constructed.

The path is reached when a function's last statement is a bare switch: the implicit return argument is walked by the return-argument transformer, and the default traversal descends into the arm IIFEs and their ForStatement headers.

Repro (12 lines):

//@version=6
indicator("For Counter in Switch Arm")

draw(cond) =>
    float s = 0.0
    switch
        cond =>
            for iR = 1 to 3
                s += iR
        => s

plot(1)

Fix

  • Add a ForStatement() {} no-op visitor to the complex return-argument walker so for-headers are never descended into (the loop's declaration/test/update/body are handled by the statement machinery when the enclosing function is transformed).
  • Skip loop variables in that walker's Identifier visitor.

Test

tests/transpiler/for-loop-counter-return-argument.test.ts — minimal-repro regression test. Before the fix it fails (generated code contains $.get(iR, 0)++/--); after the fix the counter stays a raw JS local and the generated function constructs and runs.

Verification

  • tests/transpiler/ — 33 files / 504 tests pass.
  • Full suite: 172 files pass; 2 files (pagination, request-cross-tf) are flaky and also fail intermittently on unpatched main in this environment — unrelated pre-existing flakiness.

@github-actions

github-actions Bot commented Sep 18, 2026 •

Copy link
Copy Markdown

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@yusufcansehirli
yusufcansehirli force-pushed the fix/for-loop-counter-in-return-argument branch from 9456080 to f283b4c Compare September 18, 2026 21:20
@yusufcansehirli

Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA

This branch has not been deployed

No deployments
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.

1 participant