Skip to content

Commit f976780

Browse files
committed
More descriptive error on bad macro array content
@whacked ran into this and the error wasn't particularly helpful.
1 parent bd7fb87 commit f976780

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/import-macro.ls

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,11 @@ compilerify-macro = (env, func) ->
107107

108108
return switch
109109
| internal-ast-form is null => null
110-
| typeof! internal-ast-form is \Array => env.compile-many internal-ast-form
110+
| typeof! internal-ast-form is \Array =>
111+
internal-ast-form.for-each ->
112+
if not it?
113+
throw Error "Multi-returning macro return array contained `#it`"
114+
env.compile-many internal-ast-form
111115
| otherwise =>
112116

113117
sm-ast = env.compile internal-ast-form

test.ls

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,6 +1007,21 @@ test "invalid AST returned by macro throws error" ->
10071007
'''
10081008
Error
10091009
1010+
test "macro multi-returning array with bad values throws descriptive error" ->
1011+
try
1012+
# `console.log` is invalid as a variable name, but if used as if it were
1013+
# one, without checking if the AST makes sense, this will compile to
1014+
# valid JavaScript code of `console.log('hi');`!
1015+
esl '''
1016+
(macro breaking (function () (return ((. this multi) "hi" null))))
1017+
(breaking)
1018+
'''
1019+
catch e
1020+
e.message `@equals` "Multi-returning macro return array contained `null`"
1021+
return
1022+
1023+
@fail!
1024+
10101025
test "macro return intermediates may be invalid if fixed by later macro" ->
10111026
# `...` is an invalid variable name, but since it's corrected by a later
10121027
# macro before evaluation, that's fine.

0 commit comments

Comments
 (0)