reader/read-string 是否将元数据附加到表单

Does reader/read-string attach metadata to the forms

我在某处读到 cljs.reader/read-string 将元数据附加到它创建的表单,例如读取的字符串中的位置。
是真的吗?它记录在某处吗? 谢谢

read-string 没有将元数据添加到返回的表单中:

=> (meta (cljs.reader/read-string "(prn 0)"))
nil

您编译的 functions/defs/vars 将具有这种类型的元数据:

=> (meta #'my-fn)
{:ns app.core,
 :name my-fn,
 :file "src/cljs/app/core.cljs",
 :end-column 20,
 :column 1,
 :line 125,
 :end-line 125,
 :arglists ([{:keys [x]}]),
 :doc nil,
 :test nil}

我不知道 cljs.reader,但如果你使用 clojure.tools.reader,你可以做到这一点。它没有特别详细的记录,但您可以通过查看测试来了解如何:https://github.com/clojure/tools.reader/blob/master/src/test/cljs/cljs/tools/metadata_test.cljs#L62-L70

简而言之,您必须将字符串传递给 clojure.tools.reader.reader-types/indexing-push-back-reader,然后从那里传递给 clojure.tools.reader/read。 (在上面的 test/example 中,他们首先传递给 reader-types/string-push-back-reader,但这似乎并不是绝对必要的)。