clj-kondo

https://github.com/clj-kondo/clj-kondo
2020-11-13T02:20:28.419300Z

I'm using (create-ns) and (alias) manually to create some namespaces only for the purpose of spec. Wonder if there is a way I could do it so clj-kondo wouldn't show warning Unresolved namespace marketplace. Are you missing a require? (clj-kondo-clj)

dominicm 2020-11-13T03:37:06.419700Z

@didibus are they top level calls?

2020-11-13T08:08:48.420800Z

No they arn't.

2020-11-13T08:09:25.421900Z

Though I could switch the function to a macro that returned an (alias 'foo 'bar) if that can help

dominicm 2020-11-13T08:09:46.422200Z

@didibus are they dynamic?

dominicm 2020-11-13T08:09:57.422500Z

Or maybe they can't be

2020-11-13T08:10:38.423900Z

They're not dynamic. Like the namespace is in the code. I wrap a call to create-ns and alias in a function. And I call that function with a hard-coded symbol

2020-11-13T08:11:26.425Z

Maybe I could hook into clj-kondo and add symbols passed too my custom function to it's set of known alias? Is that possible?

dominicm 2020-11-13T08:20:35.426Z

The problem I think you have is the function.

dominicm 2020-11-13T08:21:09.427Z

It's worth testing with the function inlined to see if the magic already works.

dominicm 2020-11-13T08:21:34.427800Z

If it does, then I'd try hooks potentially.

dominicm 2020-11-13T08:27:23.428800Z

@didibus

(ns foo)

(create-ns 'com.bar.baz)
(alias 'bar 'com.bar.baz)

::bar/foo
This lints fine. So I'd try hooks.

borkdude 2020-11-13T08:32:08.429600Z

yes, a hook could work. but clj-kondo could also be tweaked to recognize the (alias 'foo (create-ns 'bar)) pattern is that is used a lot

borkdude 2020-11-13T08:32:50.430500Z

and you can also do that yourself via a hook right now. or use #_{:clj-kondo/ignore [:unresolved-namespace]}. This is only needed in the first occurrence.

2020-11-13T11:24:05.433Z

hi, I get error indications from clj-kondo when I use async/alt! in clojurescript, the errors report the binding symbol in a result-expression as unresolved symbol, anybody else seen this? works fine in clojure..

borkdude 2020-11-13T11:24:51.433100Z

Can you make a small .cljs file / snippet that shows the problem?

2020-11-13T11:31:59.433500Z

hmm, I’m not really familiar with slack and how to send snippets, that was not what I intended

2020-11-13T11:32:23.433900Z

(ns repro
  (:require-macros [cljs.core.async.macros :refer [go alt!]])
  (:require [clojure.core.async :as async]))

(let [ch (async/chan)]
  (go
    (alt!
      ch ([value]
          (println value)))))

2020-11-13T11:32:27.434100Z

that was better

2020-11-13T11:33:26.434300Z

when I have that code in a cljs file in VS Code, I get error squiggly on the value binding symbol

borkdude 2020-11-13T11:34:18.434500Z

Yeah, I see. If you write:

(let [ch (async/chan)]
  (go
    (async/alt!
      ch ([value]
          (println value)))))
then it goes away, but I'm not sure if that's valid in CLJS

borkdude 2020-11-13T11:36:16.434700Z

I will ask in #clojurescript

2020-11-13T11:40:17.434900Z

ok, so it’s related to :require-macros ?

borkdude 2020-11-13T11:40:43.435100Z

clj-kondo understands clojure.core.async/alt but it doesn't have a special rule for understanding the CLJS way of using that

borkdude 2020-11-13T11:40:56.435300Z

but if you're lucky this is fixed in newer version of CLJS

2020-11-13T11:40:56.435500Z

I see

2020-11-13T11:41:18.435700Z

thanks

borkdude 2020-11-13T11:41:57.435900Z

You can fix it now with this config:

(ns repro
  {:clj-kondo/config '{:lint-as {cljs.core.async.macros/alt! clojure.core.async/alt!}}}
  (:require-macros [cljs.core.async.macros :refer [go alt!]])
  (:require [clojure.core.async :as async]))

(let [ch (async/chan)]
  (go
    (alt!
      ch ([value]
          (println value)))))

borkdude 2020-11-13T11:42:07.436100Z

so:

{:lint-as {cljs.core.async.macros/alt! clojure.core.async/alt!}}

borkdude 2020-11-13T11:42:34.436300Z

Maybe post that in an issue and then we can include that in clj-kondo itself

2020-11-13T11:43:48.436500Z

ok, thanks again

2020-11-13T12:01:31.436700Z

I guess that an issue for this is unnecessary then?

borkdude 2020-11-13T12:07:29.436900Z

yeah, I think so

2020-11-13T20:47:13.437100Z

I'm doing:

(create-ns ns)
  (alias alias-sym ns)

2020-11-13T20:48:08.437400Z

But I have this wrapped in a utility function which is in another namespace, which I call like so:

(defalias 'alias 'namespace)

2020-11-13T20:56:54.437600Z

Hum, this makes me wonder, does inlining work with clj-kondo? Like with definline or :inline meta ?

2020-11-13T20:58:38.437800Z

Doesn't look like it does

2020-11-13T21:18:34.438300Z

Managed to fix it with :lint-as my/fn clojure.core/alias That was easy πŸ˜„

1πŸ‘
2020-11-13T21:21:21.439Z

I had assumed :lint-as was only for macros for some reason, but ya, happy to see it works for functions as well

borkdude 2020-11-13T21:24:03.440400Z

yeah, clj-kondo doesn't care about macro or function, it only cares about the surface form

1πŸ‘Œ
2020-11-13T21:24:31.440900Z

Another question, for Alias consistency it seems you need to configure it. Would there not be a way that it just complained on alias that differed in a code base? Like if I had two namespace, one where foo.bar is foo and another where foo.bar is bar, then both alias would show a warning saying they are inconsistent with one another?

borkdude 2020-11-13T21:26:35.443100Z

@didibus That has come up in the issue when writing that linter. The reason we didn't do that is that currently clj-kondo doesn't read information about other namespaces other than vars from the cache. And you would have to make a choice either way: like what if two namespaces have str and two other namespaces have s. You would have to specify one of both in the config then anyway. So it just made the most sense to just start out with a baseline

borkdude 2020-11-13T21:27:30.443500Z

You can retrieve the aliases from all namespaces from the analysis config yourself and then spit out some baseline, probably

2020-11-13T21:31:14.445100Z

That makes sense, though, I think an automatic one would be much more useful. Generally, I don't care if its s or str, but I care that whichever it is is the same everywhere. So I'd expect that the first one someone uses would become the standard, as the next person to alias it would see the warning: Inconsistent alias, aliased as s in foo And so they'd fix their inconsistent usage. But I recon the added complexity of doing so due to needing info from other namespaces for it.

borkdude 2020-11-13T21:34:53.447Z

The problem is that this requires knowledge of an entire code-base, while linting only one file (in editor usage)

borkdude 2020-11-13T21:35:49.447500Z

You can write such a linter yourself based on the analysis output of course, but then you would not see it in your editor

borkdude 2020-11-13T21:35:53.447700Z

Maybe useful for CI

2020-11-13T21:52:42.448100Z

Hum, ya that's fair. Not something the cache could make use of?

borkdude 2020-11-13T22:01:19.449900Z

We could write to a special cache file that this linter could read from which would be an index of namespace (clojure.string) -> frequency table of aliases, or something? I haven't thought this through in detail, but feel free to think more about it.

1πŸ‘
2020-11-13T22:02:30.450200Z

Seed is planted πŸ˜› Its not my most important concern, just thought about it as I was reading on the linter

seancorfield 2020-11-13T22:15:08.452100Z

Re: namespace aliases -- is there a linter for sanity-checking the alias (in the absence of configuration) based on the common guideline that a namespace should be aliased to the last segment of its name (if that wouldn't be ambiguous in a file)?

seancorfield 2020-11-13T22:15:39.452700Z

Is that a common enough guideline to be worth implementing?

seancorfield 2020-11-13T22:17:10.453900Z

Obviously there are exceptions (`clojure.spec.alpha :as s` and clojure.string :as str are the most common I've seen in books and tutorials and it seems that datomic.client.api :as d is canonical).

seancorfield 2020-11-13T22:17:36.454300Z

Maybe some built-in (overridable) defaults for common nses?

borkdude 2020-11-13T22:18:24.454800Z

yeah, there could be some predicate (that can be executed with sci) to determine the desired alias

borkdude 2020-11-13T22:18:38.455Z

with a good built-in default predicate

borkdude 2020-11-13T22:18:57.455300Z

the linter already supports a config, which would be the overrides

borkdude 2020-11-13T22:20:21.455800Z

> is there a linter for sanity-checking the alias No > Is that a common enough guideline to be worth implementing? Possibly

borkdude 2020-11-13T22:20:57.456200Z

I tend to do edamame.core :as e as well

borkdude 2020-11-13T22:21:25.456800Z

or sci.core :as sci, so I think it would maybe be hard to come up with a predicate that would satisfy the entire community

borkdude 2020-11-13T22:23:09.457900Z

Also, the function would have to give a unique answer for a given lib. What if there is already an e in the config, what would it recommend: ed, e2 ?

seancorfield 2020-11-13T22:26:19.459100Z

I'll give it some thought, and see if I can come up with a good heuristic. In the presence of ambiguity -- either against the config or other aliases in the current ns -- just not running the check would seem to be the safest option.