Skip to content

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

Open
dsaicharan072-cmyk wants to merge 1 commit 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 1 commit 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.

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