You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.