Skip to content
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

Merged
merged 1 commit into from
Dec 7, 2024

Conversation

camsaul
Copy link
Contributor

@camsaul camsaul commented Aug 21, 2024

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 your ns form.

@@ -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
Copy link
Contributor Author

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

Copy link
Owner

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.

camsaul added a commit to metabase/metabase that referenced this pull request Aug 21, 2024
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)
Copy link
Contributor Author

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 Show resolved Hide resolved
@@ -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
Copy link
Owner

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.

cljfmt/src/cljfmt/core.cljc Outdated Show resolved Hide resolved
cljfmt/src/cljfmt/core.cljc Outdated Show resolved Hide resolved
cljfmt/test/cljfmt/core_test.cljc Show resolved Hide resolved
@camsaul camsaul requested a review from weavejester October 7, 2024 22:09
@camsaul
Copy link
Contributor Author

camsaul commented Dec 6, 2024

@weavejester, anything else you want me to change here?

@weavejester
Copy link
Owner

No, that all looks good. Thanks for the PR.

@weavejester weavejester merged commit fc3340d into weavejester:master Dec 7, 2024
1 check passed
@camsaul camsaul deleted the fix-351 branch December 7, 2024 01:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Indentation rules break if namespace form in :require has metadata on it
2 participants