Skip to content

Commit 146284f

Browse files
committed
catch integer literal loop sources
... before they get to JavaScript, where a 0[i$] expression results in a very unhelpful SyntaxError.
1 parent 01e1c11 commit 146284f

File tree

3 files changed

+10
-0
lines changed

3 files changed

+10
-0
lines changed

lib/ast.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ast.ls

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2350,6 +2350,9 @@ class exports.For extends While
23502350
then "#idx #{ '<>'char-at pvar < 0 }#eq #tvar"
23512351
else "#pvar < 0 ? #idx >#eq #tvar : #idx <#eq #tvar"
23522352
else
2353+
if @source instanceof Literal and @source.value is /^[0-9]+$/
2354+
# There are lots of invalid loop sources, but integer literals specifically result in JavaScript compilation errors
2355+
@carp "invalid loop source #{@source.value}"
23532356
@item = Var o.scope.temporary \x if @ref
23542357
if @item or @object and @own or @let
23552358
[svar, srcPart] = @source.compile-loop-reference o, \ref, not @object

test/loop.ls

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,3 +686,7 @@ eq 1, i
686686
o = { [k, -> v] for let k, v of {a: 1, b: 2} }
687687
eq 1 o.a!
688688
eq 2 o.b!
689+
690+
# Integer literals as loop sources generate illegal JavaScript and must be
691+
# caught by us
692+
compileThrows 'invalid loop source 42' 1 '[0 for x in 42]'

0 commit comments

Comments
 (0)