js-on-sx: optional chaining ?.
Parser: jp-parse-postfix handles op "?." followed by ident / [ / ( emitting (js-optchain-member obj name), (js-optchain-index obj k), or (js-optchain-call callee args). Transpile: each emits (js-optchain-get obj key) or (js-optchain-call fn args). Runtime: js-optchain-get and js-optchain-call short-circuit to js-undefined when receiver is null/undefined. 423/425 unit (+5), 148/148 slice unchanged.
This commit is contained in:
@@ -111,6 +111,12 @@
|
||||
(js-transpile-postfix (nth ast 1) (nth ast 2)))
|
||||
((js-tag? ast "js-prefix")
|
||||
(js-transpile-prefix (nth ast 1) (nth ast 2)))
|
||||
((js-tag? ast "js-optchain-member")
|
||||
(js-transpile-optchain-member (nth ast 1) (nth ast 2)))
|
||||
((js-tag? ast "js-optchain-index")
|
||||
(js-transpile-optchain-index (nth ast 1) (nth ast 2)))
|
||||
((js-tag? ast "js-optchain-call")
|
||||
(js-transpile-optchain-call (nth ast 1) (nth ast 2)))
|
||||
((js-tag? ast "js-switch")
|
||||
(js-transpile-switch (nth ast 1) (nth ast 2)))
|
||||
((js-tag? ast "js-new")
|
||||
@@ -819,6 +825,30 @@
|
||||
(js-collect-funcdecls (rest stmts))))
|
||||
(else (js-collect-funcdecls (rest stmts))))))
|
||||
|
||||
(define
|
||||
js-transpile-optchain-member
|
||||
(fn
|
||||
(obj-ast name)
|
||||
(list (js-sym "js-optchain-get") (js-transpile obj-ast) name)))
|
||||
|
||||
(define
|
||||
js-transpile-optchain-index
|
||||
(fn
|
||||
(obj-ast key-ast)
|
||||
(list
|
||||
(js-sym "js-optchain-get")
|
||||
(js-transpile obj-ast)
|
||||
(js-transpile key-ast))))
|
||||
|
||||
(define
|
||||
js-transpile-optchain-call
|
||||
(fn
|
||||
(callee-ast args)
|
||||
(list
|
||||
(js-sym "js-optchain-call")
|
||||
(js-transpile callee-ast)
|
||||
(js-transpile-args args))))
|
||||
|
||||
(define
|
||||
js-transpile-stmt-list
|
||||
(fn
|
||||
|
||||
Reference in New Issue
Block a user