-
Notifications
You must be signed in to change notification settings - Fork 120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix formatting when requires have metadata #352
Conversation
cljfmt/src/cljfmt/core.cljc
Outdated
@@ -568,10 +568,43 @@ | |||
#?(:clj | |||
(defn- as-zloc->alias-mapping [as-zloc] | |||
(let [alias (some-> as-zloc z/right z/sexpr) | |||
current-namespace (some-> as-zloc z/leftmost z/sexpr) | |||
;; `leftmost` might be an `uneval` node e.g. in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to write out the reasoning behind the changes here as opposed to in the PR, hope that's ok. Maybe it will save the next person who comes along some time so they don't have to work out what's going on the hard way
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think in this case it's a lot of information that isn't relevant to most readers. Consider instead something like:
current-namespace (some-> as-zloc z/leftmost find-first-symbol z/sexpr)
This informs the reader of the intent, without needing to go into detail. If the reader is curious of why, they can look up the information in the associated commit.
It's important to explain the source code, but also to be concise. Any information that isn't universally relevant is noise to those that don't need it.
grandparent-node (some-> as-zloc z/up z/up) | ||
parent-namespace (when-not (ns-require-form? grandparent-node) | ||
(first (z/child-sexprs grandparent-node)))] | ||
(when (or (z/vector? grandparent-node) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if we have something like
^{:clj-kondo/ignore [:discouraged-namespace} [my.namespace :as n]
then grandparent-node
in this case is a :meta
node so by adding the vector or string check we prevent that from accidentally returning an alias map like
{"n" "{:clj-kondo/ignore [:discouraged-namespace]}.my.namespace"}
cljfmt/src/cljfmt/core.cljc
Outdated
@@ -568,10 +568,43 @@ | |||
#?(:clj | |||
(defn- as-zloc->alias-mapping [as-zloc] | |||
(let [alias (some-> as-zloc z/right z/sexpr) | |||
current-namespace (some-> as-zloc z/leftmost z/sexpr) | |||
;; `leftmost` might be an `uneval` node e.g. in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think in this case it's a lot of information that isn't relevant to most readers. Consider instead something like:
current-namespace (some-> as-zloc z/leftmost find-first-symbol z/sexpr)
This informs the reader of the intent, without needing to go into detail. If the reader is curious of why, they can look up the information in the associated commit.
It's important to explain the source code, but also to be concise. Any information that isn't universally relevant is noise to those that don't need it.
@weavejester, anything else you want me to change here? |
No, that all looks good. Thanks for the PR. |
Fixes #351
While working on this I tried to think of all the possible weird edge cases I could and found that it also broke in a few even weirder situations e.g. if you did something like put metadata on or comments before the
x
in[x :as y]
itself. I added a pretty comprehensive test makes sure things work as expected no matter what shenanigans you're up to in yourns
form.