Skip to content

analyzer: introduce a :compile-like key #14

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

Merged
merged 2 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## master (unreleased)

* `analyzer`: include a `:compile-like` key which indicates if the error happened at a "compile-like" phase.
* It represents exceptions that happen at runtime (and therefore never include a `:phase`) which however, represent code that cannot possibly work, and therefore are a "compile-like" exception (i.e. a linter could have caught them).
* The set of conditions which are considered a 'compile-like' exception is private and subject to change.
* Use Orchard [0.15.1](https://github.com/clojure-emacs/orchard/blob/v0.15.1/CHANGELOG.md#0151-2023-09-21).

## 0.2.0 (2023-08-20)

## Changes
Expand Down
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
:url "https://github.com/clojure-emacs/haystack"
:license {:name "Eclipse Public License"
:url "https://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[cider/orchard "0.11.0"]
:dependencies [[cider/orchard "0.15.1"]
[instaparse "1.4.12" :exclusions [org.clojure/clojure]]]
:pedantic? ~(if (System/getenv "CI")
:abort
Expand Down
17 changes: 16 additions & 1 deletion src/haystack/analyzer.clj
Original file line number Diff line number Diff line change
Expand Up @@ -338,14 +338,29 @@
(flag-duplicates)
(flag-tooling)))))

(defn- compile-like-exception?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should add some of your explanations from the PR here, so it's clear why we need this.

"'Compile-like' exceptions are those that happen at runtime
(and therefore never include a `:phase`) which however,
represent code that cannot possibly work,
and therefore are a 'compile-like' exception (i.e. a linter could have caught them)."
[{cause-type :type
^String
cause-message :message}]
(and (= cause-type 'java.lang.IllegalArgumentException)
(or (some-> cause-message (.startsWith "No matching field"))
(some-> cause-message (.startsWith "No matching method")))))

(defn- analyze-cause
"Analyze the `cause-data` of an exception, in `Throwable->map` format."
[cause-data print-fn]
(let [pprint-str #(let [writer (StringWriter.)]
(print-fn % writer)
(str writer))
phase (-> cause-data :data :clojure.error/phase)
m {:class (name (:type cause-data))
:phase (-> cause-data :data :clojure.error/phase)
:phase phase
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At first I was tempted to put a synthetic value under :phase, but that might be surprising to anyone using Haystack as a lib.

I plan to do some transparent changes in cider.el such that we can we can change this default:

(defcustom cider-clojure-compilation-error-phases '("read-source"
                                                    "macro-syntax-check"
                                                    "macroexpansion"
                                                    "compile-syntax-check"
                                                    "compilation"
+                                                   "cider/compile-like"

:compile-like (boolean (and (not phase)
(compile-like-exception? cause-data)))
:message (:message cause-data)
:stacktrace (analyze-stacktrace-data
(cond (seq (:trace cause-data))
Expand Down
47 changes: 46 additions & 1 deletion test/haystack/analyzer_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,52 @@
(eval '(let [1]))
(catch Throwable e
(sut/analyze e)))
(map :phase))))))))
(map :phase)))))
(testing "Does not include `:phase` for vanilla runtime exceptions"
(is (= [nil]
(->> (try
(throw (ex-info "" {}))
(catch Throwable e
(sut/analyze e)))
(map :phase)))))))

(testing "`:compile-like`"
(testing "For non-existing fields"
(is (= [true]
(->> (try
(eval '(.-foo ""))
(catch Throwable e
(sut/analyze e)))
(map :compile-like)))))
(testing "For non-existing methods"
(is (= [true]
(->> (try
(eval '(-> "" (.foo 1 2)))
(catch Throwable e
(sut/analyze e)))
(map :compile-like)))))
(testing "For vanilla exceptions"
(is (= [false]
(->> (try
(throw (ex-info "." {}))
(catch Throwable e
(sut/analyze e)))
(map :compile-like)))))
(testing "For vanilla `IllegalArgumentException`s"
(is (= [false]
(->> (try
(throw (IllegalArgumentException. "foo"))
(catch Throwable e
(sut/analyze e)))
(map :compile-like)))))
(testing "For exceptions with a `:phase`"
(is (#{[false false] ;; normal expectation
[false]} ;; clojure 1.8
(->> (try
(eval '(let [1]))
(catch Throwable e
(sut/analyze e)))
(map :compile-like)))))))

(deftest tooling-frame-name?
(are [frame-name expected] (testing frame-name
Expand Down