使用ShadowCLJS定义Pathom Mutation时let宏展开语法错误排查
排查Pathom Mutation的语法错误问题
先来看你遇到的具体情况:你在ShadowCLJS项目里用自定义的defmutation宏写了一个简单的Mutation,结果触发了语法错误。
你的代码
引入自定义宏:
[jchat.server-components.pathom-wrappers :refer [defmutation defresolver]]
编写的Mutation:
(defmutation reset-users-db "Removes all users" [] {::pc/output [:message]} {:message "ok"})
错误信息
Syntax error macroexpanding clojure.core/let at (user.clj:47:1). nil - failed: simple-symbol? at: [:bindings :form :local-symbol] spec: :clojure.core.specs.alpha/local-name nil - failed: vector? at: [:bindings :form :seq-destructure] spec: :clojure.core.specs.alpha/seq-binding-form nil - failed: map? at: [:bindings :form :map-destructure] spec: :clojure.core.specs.alpha/map-bindings nil - failed: map? at: [:bindings :form :map-destructure] spec: :clojure.core.specs.alpha/map-special-binding
问题根源分析
从你提供的宏展开结果就能找到问题所在:
(do (com.wsscode.pathom.connect/defmutation reset-users-db [env__26870__auto__ params__26871__auto__] #:com.wsscode.pathom.connect{:output [:message]} (clojure.core/let [nil env__26870__auto__ nil params__26871__auto__] {:message "ok"})) (jchat.server-components.pathom-wrappers/register! reset-users-db))
注意到里面的let表达式:(clojure.core/let [nil env__26870__auto__ nil params__26871__auto__] ...)——这里用nil作为绑定符号,而Clojure的let要求绑定必须是合法的简单符号,nil显然不符合这个要求,这就是触发spec验证错误的原因。
问题出在你自定义的defmutation宏上:它没有正确处理[](也就是不需要env和参数)的情况,错误地把nil当成了绑定变量名来生成代码。
解决方案
有两个思路可以解决这个问题:
临时快速修复:哪怕你不需要使用env和参数,也在参数列表里写占位符(比如
_),这样宏就能生成合法的绑定符号:(defmutation reset-users-db "Removes all users" [_ _] {::pc/output [:message]} {:message "ok"})修复自定义宏:修改
jchat.server-components.pathom-wrappers里的defmutation宏实现,处理参数列表为[]的场景,避免生成nil作为绑定符号。比如当参数列表是空向量时,不要生成多余的let绑定,或者用合法的占位符代替nil。
内容的提问来源于stack exchange,提问作者Gravity123
相关产品推荐
相关产品推荐

