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

Support :discouraged-instance-method linter for Java interop #2234

Open
tatut opened this issue Dec 7, 2023 · 0 comments
Open

Support :discouraged-instance-method linter for Java interop #2234

tatut opened this issue Dec 7, 2023 · 0 comments
Projects

Comments

@tatut
Copy link

tatut commented Dec 7, 2023

Is your feature request related to a problem? Please describe.
Some Java APIs have instance methods that are discouraged.
For example when using the Playwright Java API, the Locator class has many methods which are inherently flaky
and should not be used in tests.

It would be good to be able to configure those per project so that you don't accidentally use a discouraged
method, when better alternatives exist.

Describe the solution you'd like
A :discouraged-instance-method linter that would warn about specific. Class & method combination.
Similar to :discouraged-var for Clojure.

Describe alternatives you've considered
You can always use existing Clojure libraries that wrap Java APIs when such exist or provide project specific wrappers.
That doesn't prevent you from dropping down to Java interop and using it directly.

Additional context

Configuring the hypothetical linter with

{:linters {:discouraged-instance-method {com.microsoft.playwright.Locator/all {:message "Flaky method! Use PlaywrightAssertions API."}}}}

And having the following code:

(ns playwright-all
  (:import (com.microsoft.playwright Locator)
           (com.microsoft.playwright.assertions PlaywrightAssertions)
           (org.opentest4j AssertionFailedError)))

(defn has-n-elements-flaky [^Locator locator n]
  ;; .all is inherently flaky, should not be used
  (= n (count (.all locator))))

(defn has-n-elements-correct [^Locator locator n]
  ;; Correct t
  (try
    (-> locator PlaywrightAssertions/assertThat
        (.hasCount n))
    true
    (catch AssertionFailedError _afe
      false)))

would issue a warning on the .all call on line 8.

@borkdude borkdude added this to Needs triage in clj-kondo via automation Dec 7, 2023
@borkdude borkdude moved this from Needs triage to Medium priority (new / enhance) in clj-kondo Dec 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
clj-kondo
  
Medium priority (new / enhance)
Development

No branches or pull requests

1 participant