slamhound icon indicating copy to clipboard operation
slamhound copied to clipboard

"Invalid token" error when using abbreviated namespaced keywords

Open joodie opened this issue 11 years ago • 6 comments

Requiring a namespace using :as alias, then using the alias to refer to a keyword in that namespace throws an error in slamhound.

Given src/my_test.clj:

(ns my-test
  (:require [clojure.test :as test]))
(prn :clojure.test/foo) ;; works but no alias
(prn ::test/foo) ;; this line bugs slamhound

Results in:

> lein run -m slam.hound src/my_test.clj
Failed to reconstruct: #<File src/my_test.clj>
Invalid token: ::test/foo

I think slamhound should parse this as clojure does (AFAIKT this feature has been in clojure since 1.0, though documentation is scarce) and interprets ::alias/word as a requirement to keep the :as alias.

joodie avatar Jun 26 '14 10:06 joodie

Ah, that's interesting; I never considered the combination of auto-qualifying keywords and aliases. I'll take a look later today.

Thanks for the report!

guns avatar Jun 26 '14 11:06 guns

Hello, I finally had a chance to look at this today.

This Invalid token error occurs at read-time and therefore never reaches Slamhound's ns regrow loop.

It is possible to add a special-case just for this. It would require:

  • Breaking apart asplode/asplode into two separate processes: parse-ns and read-body. This changes the return type of asplode/asplode, and requires a bit of test refactoring
  • Adding a new regrow/check-for-read-failure fn that parses LispReader exceptions
  • Adding a new regrow/read-body fn, which is just a partial application of regrow/regrow + the new regrow/check-for-read-failure fn

At a high level then, the reconstruction algorithm becomes:

-> file
   asplode-ns
   read-body
   regrow-ns
   stitch-up

@technomancy: Do you think supporting ::alias/sym is worth the above changes? I am happy to do the work

guns avatar Jul 02 '14 19:07 guns

I'm not sure how this requires a new phase, since reading the body is already the last isolated step of asplode, but yeah, this seems reasonable.

technomancy avatar Jul 03 '14 05:07 technomancy

It doesn't require a new phase, but it avoids making asplode depend on regrow. I also think it's nicer if asplode returns a file's ns form without any interpretation.

Anyway, great! I will push up an implementation tomorrow to a feature branch for review.

guns avatar Jul 03 '14 05:07 guns

This seems more important with clojure 1.9 around the corner.

jimrthy avatar Aug 29 '17 03:08 jimrthy

Yeah, we use this in nearly every ns we have. +1

jeaye avatar Sep 25 '17 23:09 jeaye