js-on-sx: SyntaxError on let/const/function/class as single-stmt body of if/while/for/labeled
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s

This commit is contained in:
2026-05-10 13:43:47 +00:00
parent 29a3fb4bc2
commit 7f5b77415f
2 changed files with 43 additions and 0 deletions

View File

@@ -1144,15 +1144,51 @@
((c (jp-parse-assignment st)))
(do
(jp-expect! st "punct" ")")
(jp-disallow-decl-stmt! st "if")
(let
((t (jp-parse-stmt st)))
(if
(jp-at? st "keyword" "else")
(do
(jp-advance! st)
(jp-disallow-decl-stmt! st "else")
(list (quote js-if) c t (jp-parse-stmt st)))
(list (quote js-if) c t nil))))))))
(define
jp-disallow-decl-stmt!
(fn
(st context)
(let
((t (jp-peek st)))
(cond
((and (= (get t :type) "keyword")
(or (= (get t :value) "let")
(= (get t :value) "const")
(= (get t :value) "function")
(= (get t :value) "class")))
(cond
((and (= (get t :value) "let")
(or (= (get (jp-peek-at st 1) :type) "ident")
(and (= (get (jp-peek-at st 1) :type) "punct")
(or (= (get (jp-peek-at st 1) :value) "[")
(= (get (jp-peek-at st 1) :value) "{")))))
(error
(str
"SyntaxError: Lexical declaration cannot appear in single-statement context: "
context)))
((or (= (get t :value) "const")
(= (get t :value) "function")
(= (get t :value) "class"))
(error
(str
"SyntaxError: "
(get t :value)
" declaration cannot appear in single-statement context: "
context)))
(else nil)))
(else nil)))))
(define
jp-bump!
(fn
@@ -1176,6 +1212,7 @@
((c (jp-parse-assignment st)))
(do
(jp-expect! st "punct" ")")
(jp-disallow-decl-stmt! st "while")
(jp-bump! st :loop-depth)
(let ((body (jp-parse-stmt st)))
(jp-decr! st :loop-depth)
@@ -1187,6 +1224,7 @@
(st)
(do
(jp-advance! st)
(jp-disallow-decl-stmt! st "do")
(jp-bump! st :loop-depth)
(let
((body (jp-parse-stmt st)))
@@ -1235,6 +1273,7 @@
(let
((iter (jp-parse-assignment st)))
(jp-expect! st "punct" ")")
(jp-disallow-decl-stmt! st "for-of/in")
(jp-bump! st :loop-depth)
(let
((body (jp-parse-stmt st)))
@@ -1249,6 +1288,7 @@
(let
((step (if (jp-at? st "punct" ")") nil (jp-parse-assignment st))))
(jp-expect! st "punct" ")")
(jp-disallow-decl-stmt! st "for")
(jp-bump! st :loop-depth)
(let
((body (jp-parse-stmt st)))
@@ -1513,6 +1553,7 @@
(do
(jp-advance! st)
(jp-advance! st)
(jp-disallow-decl-stmt! st "label")
(jp-parse-stmt st)))
((jp-at? st "keyword" "class") (jp-parse-class-decl st))
((jp-at? st "keyword" "throw") (jp-parse-throw-stmt st))