Skip to content

fix: Deduce doc_name from name when resolving loop counter#2172

Merged
amitu merged 3 commits intomainfrom
fix/loop-idx
Jul 29, 2025
Merged

fix: Deduce doc_name from name when resolving loop counter#2172
amitu merged 3 commits intomainfrom
fix/loop-idx

Conversation

@siddhantk232
Copy link
Copy Markdown
Contributor

@siddhantk232 siddhantk232 commented Jul 29, 2025

The for loop (with counter) does not work properly when the variable it's looping over is coming from a different ftd file.

;; index.ftd in package "test"
-- import: test/x

-- string list things:

-- string: one
-- string: two
-- string: three

-- end: things


-- x.test:
in: $things
;; in x.ftd

-- component test:
string list in: 

-- ftd.column:

;; looping over $test.in which is coming from `index.ftd`
-- ftd.integer: $idx
for: x, idx in $test.in

-- end: ftd.column

-- end: test

The output will be "undefined" printed 3 times in the html page. This happens because it's trying to index into the global to find the idx but the right thing is to read the function local index (loop counter). Here's the current js output:

 parenti0.setProperty(fastn_dom.PropertyKind.Children, fastn.mutableList([function (root, inherited) {
      __args__.in.forLoop(root, function (root, item, index) {
        let rooti0 = fastn_dom.createKernel(root, fastn_dom.ElementKind.Integer);
        // The following line is the culprit
        rooti0.setProperty(fastn_dom.PropertyKind.IntegerValue, global.swyn_ui_fifthtry_site_pages_quiz_quiz_detail__idx, inherited);
        return rooti0;
      });
    }
    ]), inherited);

The fixed js output in this PR looks like:

let parenti0 = fastn_dom.createKernel(parent, fastn_dom.ElementKind.Column);
    parenti0.setProperty(fastn_dom.PropertyKind.Children, fastn.mutableList([function (root, inherited) {
      __args__.in.forLoop(root, function (root, item, index) {
        let rooti0 = fastn_dom.createKernel(root, fastn_dom.ElementKind.Integer);
        // This is the correct output
        rooti0.setProperty(fastn_dom.PropertyKind.IntegerValue, index, inherited);
        return rooti0;
      });
    }
    ]), inherited);

This issue was first discovered by @MeenuKumari28

@siddhantk232 siddhantk232 requested a review from amitu July 29, 2025 05:22
@amitu amitu merged commit 8f14289 into main Jul 29, 2025
1 check passed
@amitu amitu deleted the fix/loop-idx branch July 29, 2025 05:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants