venantius / yagni

A Leiningen plugin for finding dead code
Eclipse Public License 1.0
219 stars 10 forks source link

use case: whitelisting whole namespace #40

Closed edvorg closed 6 years ago

edvorg commented 6 years ago

Hi, I'm using a tool called joplin that performs migration for postgresql. The way it works is it loads all clj files in directory that you have set in project.clj. Each file defines a changelog which is basically a list of changes that should be applied to to schema. Currently yagni highlights everything in that directory as dead code because it's never called directly. Is there any way to define a namespace whose files and everything that is used from these files would never appear as dead code?

Example: Joplin is set up to migrate from namespace raven.persistence.migrations and that namespace has two files with contents:

(ns raven.persistence.migrations.2018-01-01-add-users
  (:require [raven.persistence.base.teams :as t]))

(declare changelog)

(let [changeset-1 ["id=1" "author=edvorg" [u/users]]]

  (defchangelog changelog "users" [changeset-1]))

(defn up [{:keys [connection-uri]}]
  (let [ds (cp/make-datasource :postgresql {:jdbc-url connection-uri})]
    (cli/entry "update" {:datasource ds :changelog changelog})))

(defn down [{:keys [connection-uri]}]
  (let [ds (cp/make-datasource :postgresql {:jdbc-url connection-uri})]
    (cli/entry "rollback" {:datasource ds :changelog changelog :chs-count "1"})))

and

(ns raven.persistence.migrations.2018-01-01-add-teams
  (:require [raven.persistence.base.teams :as t]))

(declare changelog)

(let [changeset-1 ["id=1" "author=edvorg" [t/teams]]]

  (defchangelog changelog "teams" [changeset-1]))

(defn up [{:keys [connection-uri]}]
  (let [ds (cp/make-datasource :postgresql {:jdbc-url connection-uri})]
    (cli/entry "update" {:datasource ds :changelog changelog})))

(defn down [{:keys [connection-uri]}]
  (let [ds (cp/make-datasource :postgresql {:jdbc-url connection-uri})]
    (cli/entry "rollback" {:datasource ds :changelog changelog :chs-count "1"})))

Functions up and down are never called directly and are called automatically by joplin whenever I migrate or rollback db. And I'd like to find a way to whitelist things in a wildcard kind of way

raven.persistence.migrations.*/up
raven.persistence.migrations.*/down
venantius commented 6 years ago

Hey - this is a use case that is supported by the presence of a .lein-yagni file that you can use to specify "entrypoints" (namespaces) that aren't directly called elsewhere in your program.

Wildcard support is something that you'd need to add yourself - I think you'd probably want to leverage merge-entrypoints https://github.com/venantius/yagni/blob/c830d78bc439970731999ec932bd14e7eb90bf8d/src/yagni/core.clj#L18 with some sort of mapping of wildcard namespaces as matches the return value of nss-in-dirs.

edvorg commented 6 years ago

Hey - this is a use case that is supported by the presence of a .lein-yagni file that you can use to specify "entrypoints" (namespaces) that aren't directly called elsewhere in your program.

Awesome. Do I understand correctly that I can just put namespace name (not function name) into .lein-yagni and it will work?

venantius commented 6 years ago

It should, yeah

On Sun, Apr 1, 2018 at 3:00 PM Edward Knyshov notifications@github.com wrote:

Hey - this is a use case that is supported by the presence of a .lein-yagni file that you can use to specify "entrypoints" (namespaces) that aren't directly called elsewhere in your program.

Awesome. Do I understand correctly that I can just put namespace name (not function name) into .lein-yagni and it will work?

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/venantius/yagni/issues/40#issuecomment-377788802, or mute the thread https://github.com/notifications/unsubscribe-auth/ABvYW75_zNEuILFWWcTIWCZA7H6mcB-xks5tkN1igaJpZM4TC09n .