fix: nested loop protection without braces causing ReferenceError: _LP is not defined#4184
Conversation
|
Hi @TakagiHitoshi — just following up on this PR since it's been open for a little while without review. jsPreprocess.js: track directParent in collectLoopsToProtect, handle the no-braces case in injectProtection 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 |
|
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 |
|
Thanks for flagging that — good call, I should have linked it originally. I've updated the PR description to reference it. |
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 anyBlockStatement, the preprocessor was silently dropping thevar _LP0 = Date.now()variable declaration. This resulted inReferenceError: _LP0 is not definedwhen executing the code in the preview.Solution
In
jsPreprocess.js:directParent(the immediate parent loop or statement) incollectLoopsToProtect.injectProtection, whenparentBlock.body.indexOf(loop) === -1, check if it's nested directly underdirectParentwithout braces.BlockStatementcontaining both the prepended variable declaration and the loop itself.no-continuerule) by restructuring the loops collection conditional logic.Tests
forloops andwhileloops without braces inside other loops.