rewrite-clj

borkdude 2023-03-24T18:53:20.196309Z

@lee I ran into an edge case between coercing a node from a string and parsing a node from a string expression.

user=> (require '[rewrite-clj.node :as n])
nil
user=> (n/coerce "foo")
<token: "foo">
user=> (keys (n/coerce "foo"))
(:value :string-value)
;;; vs
user=> (require '[rewrite-clj.parser :as p])
nil
user=> (def node (p/parse-string (pr-str "foo")))
#'user/node
user=> node
<token: "foo">
user=> (keys node)
(:lines)
In clj-kondo I have a predicate string-from-token which just does :lines since normally, the string is inside of :lines but not for coerced string -> node

borkdude 2023-03-24T18:54:09.783969Z

Do you think it would be good to fix this? I'll first try it in clj-kondo's fork

borkdude 2023-03-24T18:56:07.580059Z

The correct fix might be this:

(extend-protocol NodeCoerceable
  String
  (coerce [v]
    (string-node/string-node v)))

borkdude 2023-03-24T18:56:21.593609Z

I'll do a PR later, if I don't detect any issues with clj-kondo

borkdude 2023-03-24T19:04:29.508939Z

Yeah, there is this issue again, related to what we had earlier:

user=> (node/coerce "\\s")
<token: "\s">
user=> (node/sexpr (node/coerce "\\s"))
Execution error (ExceptionInfo) at clj-kondo.impl.toolsreader.v1v2v2.clojure.tools.reader.impl.errors/throw-ex (errors.clj:26).
Unsupported escape character: \s.

borkdude 2023-03-24T19:11:11.652849Z

This weirdness may be introduced by read-string-data. I wonder why rewrite-clj has its own string parsing function, since for the rest of the tokens it just reads the next EDN "token"

borkdude 2023-03-24T19:32:05.812599Z

@lee I have a preliminary fix here: https://github.com/clj-kondo/clj-kondo/commit/a4c5baea66f4a5f4b9831807d75289930b14f34a I feel like rewrite-clj is adding some unnecessary complexity in this area, but I'm working around it by using the same parsing strategy during coercion as during parsing