-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
RFC: kondo lint to enforce naming convention for thread not safe defn/defmacro in tests #37126
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6803646
kondo lint to warn defn/defmacro that use not thread safe form should…
qnkhuat 4ed5572
changing with-temp-env-var-value as an example
qnkhuat b666278
remove kondo config
qnkhuat f461a9d
fix in macros definition too
qnkhuat 2bb8c04
Merge branch 'master' into ngoc-kondo-lint-enforce-exclamation
qnkhuat 2c2956a
missing replacement
qnkhuat ff8e5a9
white card vars so don't we have to fix a mil places
qnkhuat c59612e
fix random error
qnkhuat File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
(ns hooks.clojure.core | ||
(:require | ||
[clj-kondo.hooks-api :as hooks] | ||
[clojure.string :as str])) | ||
|
||
(defn- node->qualified-symbol [node] | ||
(try | ||
(when (hooks/token-node? node) | ||
(let [sexpr (hooks/sexpr node)] | ||
(when (symbol? sexpr) | ||
(when-let [resolved (hooks/resolve {:name sexpr})] | ||
(symbol (name (:ns resolved)) (name (:name resolved))))))) | ||
;; some symbols like `*count/Integer` aren't resolvable. | ||
(catch Exception _ | ||
nil))) | ||
|
||
(def ^:private white-card-symbols | ||
'#{;; these toucan methods might actually set global values if it's used outside of a transaction, | ||
;; but since mt/with-temp runs in a transaction, so we'll ignore them in this case. | ||
toucan2.core/delete! | ||
toucan2.core/update! | ||
toucan2.core/insert!}) | ||
|
||
(defn- end-with-exclamation? | ||
[s] | ||
(str/ends-with? s "!")) | ||
|
||
(defn- unsafe-forms-should-end-with-exclamation* | ||
[{[defn-or-defmacro form-name] :children, :as node}] | ||
(when-not (end-with-exclamation? (:string-value form-name)) | ||
(letfn [(walk [f form] | ||
(f form) | ||
(doseq [child (:children form)] | ||
(walk f child)))] | ||
(walk (fn [form] | ||
(when-let [qualified-symbol (node->qualified-symbol form)] | ||
(when (and (not (contains? white-card-symbols qualified-symbol)) | ||
(end-with-exclamation? qualified-symbol)) | ||
(hooks/reg-finding! (assoc (meta form-name) | ||
:message (format "The name of this %s should end with `!` because it contains calls to not thread safe form `%s`." | ||
(:string-value defn-or-defmacro) qualified-symbol) | ||
:type :metabase/defmacro-in-test-contains-unthread-safe-functions))))) | ||
node)) | ||
node)) | ||
|
||
(defn unsafe-forms-should-end-with-exclamation | ||
"Used to ensure defn and defmacro in test namespace to have name ending with `!` if it's not thread-safe. | ||
|
||
A function or a macro can be defined as 'not thread safe' when their funciton name ends with a `!`." | ||
[{:keys [node cljc lang]}] | ||
(when (or (not cljc) | ||
(= lang :clj)) | ||
(unsafe-forms-should-end-with-exclamation* node)) | ||
{:node node}) | ||
|
||
(comment | ||
(require '[clj-kondo.core :as clj-kondo]) | ||
(def form (str '(defmacro a | ||
[x] | ||
`(fun-call x)))) | ||
|
||
(def form "(defmacro a | ||
[x] | ||
`(some! ~x))") | ||
(str (hooks/parse-string form)) | ||
(hooks/sexpr (hooks/parse-string form)) | ||
(binding [hooks/*reload* true] | ||
(-> form | ||
(with-in-str (clj-kondo/run! {:lint ["-"]})) | ||
:findings)) | ||
(do (unsafe-forms-should-end-with-exclamation* (hooks/parse-string form)) | ||
nil)) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 changed this one macro as an example