clojure-emacs / clj-refactor.el

A CIDER extension that provides powerful commands for refactoring Clojure code.
GNU General Public License v3.0
773 stars 111 forks source link

cljr-clean-ns doesn't support shadow-cljs's npm import syntax #476

Closed lucywang000 closed 2 years ago

lucywang000 commented 4 years ago

Expected behavior

Given such a file:

(ns foo
  (:require ["react" :as react]
            ["@material-ui/core/Button" :default Button]))

(println react)
(println Button)

After M-x cljr-clean-ns both requires should be preserved.

Actual behavior

Both imports are removed, even though they are used in this ns.

Versions

Is this not supported, or am i missing something that could make it work?

expez commented 4 years ago

Is this not supported

It's not supported because it isn't valid clojure code. shadow-cljs didn't exist when the original code was written and nobody has added support for these kinds of libspecs (yet!).

I searched the clojure(script) docs again, briefly, and I can't find anything stating that this is OK. I guess that means shadow-cljs could break at any moment because they're relying on unspecified behavior in the compiler?

Anyway, happy to take a PR over in refactor-nrepl if you want to get involved, but I'm not going to work on this myself.

thheller commented 3 years ago

FWIW the string :require syntax is not shadow-cljs specific and started in the official CLJS codebase. shadow-cljs was just the first tool to make extensive use of it. I cannot find the relevant issue but it was added shortly before this post from 2017 was published.

aiba commented 2 years ago

Just chiming in to say I would really appreciate it if cljr-clean-ns would keep these types of imports. Also we could rename this issue to "cljr-clean-ns doesn't support npm import syntax", since as @thheller pointed out, this is not specific to shadow-cljs.

vemv commented 2 years ago

@aiba : hi there!

You created some related commits in refactor-nrepl which remain there since. What's been your experience since?

What works and what doesn't?

vemv commented 2 years ago

Alright so here's the deal!

image

If you find a new, specific issue please feel free to open one (from scratch).

Cheers - V

aiba commented 2 years ago

Thank you for looking into this!

Ignoring the shadow-cljs-specific :default syntax, I think there remains an issue in clojurescript where you require a npm dep :as foo and then use foo for its value (which is a weird cljs/npm thing but required in many cases). I'll create a new issue.

vemv commented 2 years ago

For anyone else watching: said issue is now fixed in refactor-nrepl 3.5.4 (released within a few minutes) and clj-refactor 3.5.5 (available in MELPA within a couple hours)