Skip to content

fix: nested loop protection without braces causing ReferenceError: _LP is not defined#4184

Open
dsaicharan072-cmyk wants to merge 2 commits into
processing:developfrom
dsaicharan072-cmyk:fix/nested-loop-protection-without-braces
Open

fix: nested loop protection without braces causing ReferenceError: _LP is not defined#4184
dsaicharan072-cmyk wants to merge 2 commits into
processing:developfrom
dsaicharan072-cmyk:fix/nested-loop-protection-without-braces

Conversation

@dsaicharan072-cmyk

Copy link
Copy Markdown

Describe the bug

When users run sketch files with nested loops that do not use braces (e.g., for (...) for (...) stmt), the preprocessor generates loop protection checks using _LP[idx] variables. However, because the inner loop is not a direct child of any BlockStatement, the preprocessor was silently dropping the var _LP0 = Date.now() variable declaration. This resulted in ReferenceError: _LP0 is not defined when executing the code in the preview.

Solution

In jsPreprocess.js:

  • Track directParent (the immediate parent loop or statement) in collectLoopsToProtect.
  • In injectProtection, when parentBlock.body.indexOf(loop) === -1, check if it's nested directly under directParent without braces.
  • If so, wrap the parent's body in a BlockStatement containing both the prepended variable declaration and the loop itself.
  • Resolved an ESLint violation (no-continue rule) by restructuring the loops collection conditional logic.

Tests

  • Added regression unit tests covering nested for loops and while loops without braces inside other loops.
  • All 260 client tests passed successfully.

@dsaicharan072-cmyk

dsaicharan072-cmyk commented Jun 19, 2026

Copy link
Copy Markdown
Author

Hi @TakagiHitoshi — just following up on this PR since it's been open for a little while without review.
Quick summary for convenience: this fixes the _LP0 is not defined error reported above. The root cause was that the preprocessor's loop-protection instrumentation wasn't emitting a var _LP0 declaration when a loop wasn't a direct child of a BlockStatement (i.e. brace-less nested loops like for (...) for (...) stmt). The fix wraps the parent's body in a BlockStatement so the declaration and the loop are properly scoped together, and keeps the existing brace requirement intact per the discussion above.
Changes:

jsPreprocess.js: track directParent in collectLoopsToProtect, handle the no-braces case in injectProtection
Added regression tests for nested for/while loops without braces
Fixed a no-continue ESLint violation along the way
All 260 client tests passing

Happy to make any changes if there's a preferred approach — just let me know if there's anything blocking review (e.g. changeset, CLA, etc.) and I'll address it right away. Thanks for your time!

Resolves #4183

@inaridarkfox4231

Copy link
Copy Markdown

When submitting a pull request, it's good manner to include a reference for existing issue if it's intended to resolve that. Since a reference isn't included, I'll leave one here.

resolving issue: #4183

@dsaicharan072-cmyk

dsaicharan072-cmyk commented Jun 19, 2026

Copy link
Copy Markdown
Author

Thanks for flagging that — good call, I should have linked it originally. I've updated the PR description to reference it.

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.

2 participants