Skip to content

Commit 7633314

Browse files
committed
Move var metadata to public APIs only
Rewrite-clj v1 exposes its public API through code generation using an import-vars like technique, but statically. Internal functions that were promoted to the public API were specifying :deprecated metadata. The meant that the deprecated public API would delegate to an internal deprecated fn. The ClojureScript compiler, by default, warns on calls to deprecated functions. Users of rewrite-clj were seeing warnings from the cljs compiler for our internal deprecated calls. This is admittedly kinda sloppy. This was not an issue for rewrite-cljs, because it used no form of import-vars at all. It is not an issue with using rewrite-clj (v0 also used import-vars - the potemkim load time variant) under Clojure because it does not issue any warning for deprecated calls. To solve this issue, we now only specify :deprecated metadata on our public API by specifying fn metadata not at the source, but in our import definitions. For consistency I've also moved :added metadata that existed on other promoted fns to our import definitions. Fixes #153
1 parent 886716e commit 7633314

File tree

12 files changed

+91
-69
lines changed

12 files changed

+91
-69
lines changed

CHANGELOG.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ For a list of breaking changes see link:#v1-breaking[breaking changes].
1717
=== Unreleased
1818

1919
* rewrite-clj now exports clj-kondo config for its public API https://github.com/clj-commons/rewrite-clj/issues/146[#146]
20+
* ClojureScript compiler should no longer emit invalid deprecated warnings https://github.com/clj-commons/rewrite-clj/issues/153[#153]
2021
* Internal rewrite-clj developer facing:
2122
* Switched from babashka scripts to babashka tasks, developer guide updated accordingly
2223

doc/02-developer-guide.adoc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,18 @@ Is expressed in our templates as:
130130
[my.ns2 my-var4 my-var5]]}}
131131
----
132132

133+
Any `:added` and `:deprecated` metadata should be defined in the template and not on the reference var.
134+
This keeps the metadata on the public API vars only and avoids having the ClojureScript compiler warn about deprecated calls on internal sources within rewrite-clj:
135+
136+
//:test-doc-blocks/skip
137+
[source,clojure]
138+
----
139+
#_{:import-vars/import
140+
{:from [[my.ns1
141+
^{:deprecated "1.2.3"} obsolete-fn
142+
^{:added "1.2.4"} new-fn]]}}
143+
----
144+
133145
We also carry over rewrite-cljc support for `:import-vars/import-with-mods`, via an optional `:opts`.
134146
See `template/rewrite_clj/zip.cljc` for example usage.
135147

doc/design/01-merging-rewrite-clj-and-rewrite-cljs.adoc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,9 @@ We'll start `./src` using, otherwise using same template filename.
626626
Extension will match template (clj vs cljc for us).
627627

628628
Ok, so what code should we be generating?
629-
We want to definitely bring over the docstring (sometimes altered) and some metadata (`:added` `:deprecated`).
629+
We want to definitely bring over the docstring (sometimes altered).
630+
We'll have the import definition specify `:added` and `:deprecated` metadata.
631+
(Original version had this metadata specified on internal source var, cljs compiler warned about calls to internal deprecated fns from public API, which was not nice for folks using rewrite-clj under cljs).
630632
For the var itself we have choices.
631633

632634
. We could simply point to the source var.

script/lread/apply_import_vars.clj

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@
7979
(defn- syms-to-import [import-vecs]
8080
(for [[ns-sym & ns-vars] import-vecs
8181
ns-var ns-vars]
82-
(symbol (str ns-sym) (str ns-var))))
82+
(with-meta (symbol (str ns-sym) (str ns-var))
83+
(meta ns-var))))
8384

8485
(defn- imported-var-name [var-metadata opts]
8586
(if-let [sym-pattern (:sym-to-pattern opts)]
@@ -123,7 +124,7 @@
123124
(defn- assert-sym-meta [ctx var-meta]
124125
(doseq [al (:arglists var-meta)]
125126
(when (not (every? symbol? al))
126-
(throw (ex-info (format "appply-import-vars: args expected to be symbols only (no destructuring), found: %s\nfor var: %s\nin ns: %s\n%s"
127+
(throw (ex-info (format "appply-import-vars: args expected to be symbols only (no destructuring), found: %s\nfor var: %s\nin ns: %s\n%s"
127128
al (:name var-meta) (:ns var-meta) ctx) {})))))
128129

129130
(defn- delegator-nodes [var-meta]
@@ -153,18 +154,18 @@
153154
(zraw/insert-left (nseq/list-node
154155
(concat (list (if (:macro vm) 'defmacro 'defn)
155156
(nws/spaces 1)
156-
(let [target-meta (select-keys vm [:deprecated :added])
157+
(let [target-meta (meta import-sym)
157158
name (imported-var-name vm opts)]
158159
(if (seq target-meta)
159160
(nmeta/meta-node target-meta name)
160161
name))
161162
(nws/newlines 1)
162163
(nws/spaces 2)
163-
;; mimic what parser does
164+
;; mimic what parser does
164165
(nstring/string-node (some->> (imported-docstring vm opts)
165166
string/split-lines
166167
;; TODO: this escaping seems too awkward
167-
(map #(-> %
168+
(map #(-> %
168169
(string/replace "\\" "\\\\")
169170
(string/replace "\"" "\\\"")))
170171
(into []))))
@@ -213,8 +214,8 @@
213214
stale-cnt (->> (process-templates)
214215
(reduce (fn [stale-cnt {:keys [template-filename target-filename changed?]}]
215216
(println "Template:" template-filename)
216-
(println (if changed? "X" " ") "Target:" target-filename (if changed?
217-
(-> "STALE" ansi/bold-red-bg ansi/black)
217+
(println (if changed? "X" " ") "Target:" target-filename (if changed?
218+
(-> "STALE" ansi/bold-red-bg ansi/black)
218219
"(no changes)"))
219220
(if changed? (inc stale-cnt) stale-cnt))
220221
0))]
@@ -226,10 +227,10 @@
226227
update-cnt (->> templates
227228
(reduce (fn [update-cnt {:keys [template-filename target-filename changed? target-clj]}]
228229
(println "Template:" template-filename)
229-
(println " Target:" target-filename (if changed?
230+
(println " Target:" target-filename (if changed?
230231
(-> "UPDATE"
231232
ansi/bold-green-bg
232-
ansi/black)
233+
ansi/black)
233234
"(no changes detected)"))
234235
(if changed?
235236
(do

src/rewrite_clj/node.cljc

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
"Create, update, convert and integorate nodes.
44
55
All nodes represent Clojure/ClojureScript/EDN.
6-
6+
77
Because this API contains many functions, we offer the following categorized listing:
8-
8+
99
**Node creation**
1010
[[comma-node]]
11-
[[comment-node]]
11+
[[comment-node]]
1212
[[deref-node]]
1313
[[eval-node]]
1414
[[fn-node]]
@@ -38,7 +38,7 @@
3838
**Whitespace creation convenience**
3939
[[spaces]]
4040
[[newlines]]
41-
[[comma-separated]]
41+
[[comma-separated]]
4242
[[line-separated]]
4343
[[whitespace-nodes]]
4444
@@ -52,7 +52,7 @@
5252
[[child-sexprs]]
5353
5454
**Convert node to string**
55-
[[string]]
55+
[[string]]
5656
5757
**Node interogation**
5858
[[tag]]
@@ -63,13 +63,13 @@
6363
[[printable-only?]]
6464
6565
**Update node**
66-
[[replace-children]]
67-
66+
[[replace-children]]
67+
6868
**Namespaced map element support**
6969
[[map-context-apply]]
7070
[[map-context-clear]]
7171
72-
**Test type**
72+
**Test type**
7373
[[node?]]
7474
[[comment?]]
7575
[[whitespace-or-comment?]]
@@ -158,7 +158,7 @@
158158
159159
Optional `opts` can specify:
160160
- `:auto-resolve` specify a function to customize namespaced element auto-resolve behavior, see [docs on namespaced elements](/doc/01-user-guide.adoc#namespaced-elements)
161-
161+
162162
See docs for [sexpr nuances](/doc/01-user-guide.adoc#sexpr-nuances)."
163163
([node] (rewrite-clj.node.protocols/sexpr node))
164164
([node opts] (rewrite-clj.node.protocols/sexpr node opts)))
@@ -174,7 +174,7 @@
174174
175175
Optional `opts` can specify:
176176
- `:auto-resolve` specify a function to customize namespaced element auto-resolve behavior, see [docs on namespaced elements](/doc/01-user-guide.adoc#namespaced-elements)
177-
177+
178178
See docs for [sexpr nuances](/doc/01-user-guide.adoc#sexpr-nuances)."
179179
([nodes] (rewrite-clj.node.protocols/sexprs nodes))
180180
([nodes opts] (rewrite-clj.node.protocols/sexprs nodes opts)))

src/rewrite_clj/node/protocols.cljc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
4242
Optional `opts` can specify:
4343
- `:auto-resolve` specify a function to customize namespaced element auto-resolve behavior, see [docs on namespaced elements](/doc/01-user-guide.adoc#namespaced-elements)
44-
44+
4545
See docs for [sexpr nuances](/doc/01-user-guide.adoc#sexpr-nuances)."
4646
([node] (sexpr node {}))
4747
([node opts] (sexpr* node opts)))
@@ -51,7 +51,7 @@
5151
5252
Optional `opts` can specify:
5353
- `:auto-resolve` specify a function to customize namespaced element auto-resolve behavior, see [docs on namespaced elements](/doc/01-user-guide.adoc#namespaced-elements)
54-
54+
5555
See docs for [sexpr nuances](/doc/01-user-guide.adoc#sexpr-nuances)."
5656
([nodes]
5757
(sexprs nodes {}))
@@ -222,7 +222,7 @@
222222
[form]
223223
(apply dissoc (meta form) [:line :column :end-line :end-column]))
224224

225-
(defn ^{:deprecated "0.4.0"} value
225+
(defn value
226226
"DEPRECATED: Get first child as a pair of tag/sexpr (if inner node),
227227
or just the node's own sexpr. (use explicit analysis of `children`
228228
`child-sexprs` instead) "
@@ -232,4 +232,4 @@
232232
(first)
233233
((juxt tag sexpr)))
234234
(sexpr node)))
235-
235+

src/rewrite_clj/zip.cljc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@
134134
[[postwalk]]
135135
[[subedit->]]
136136
[[subedit->>]]
137-
137+
138138
**Sequence operations**
139139
[[map]]
140140
[[map-keys]]
@@ -737,7 +737,7 @@
737737
738738
Pre-order traversal visits root before children.
739739
For example, traversal order of `(1 (2 3 (4 5) 6 (7 8)) 9)` is:
740-
740+
741741
1. `(1 (2 3 (4 5) 6 (7 8)) 9)`
742742
2. `1`
743743
3. `(2 3 (4 5) 6 (7 8))`
@@ -780,16 +780,16 @@
780780
781781
Pre-order traversal visits children before root.
782782
For example, traversal order of `(1 (2 3 (4 5) 6 (7 8)) 9)` is:
783-
783+
784784
1. `1`
785785
2. `2`
786-
3. `3`
786+
3. `3`
787787
4. `4`
788788
5. `5`
789789
6. `(4 5)`
790790
7. `6`
791791
8. `7`
792-
9. `8`
792+
9. `8`
793793
10. `(7 8)`
794794
11. `(2 3 (4 5) 6 (7 8))`
795795
12. `9`
@@ -904,7 +904,7 @@
904904
([zloc n] (rewrite-clj.zip.whitespace/append-newline zloc n))
905905
([zloc] (rewrite-clj.zip.whitespace/append-newline zloc)))
906906
;; TODO: clj-kondo barfs on an empty reader cond
907-
#?(:clj
907+
#?(:clj
908908

909909
;; DO NOT EDIT FILE, automatically imported from: rewrite-clj.zip.base
910910
(defn of-file

src/rewrite_clj/zip/base.cljc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
([zloc]
7070
(some-> zloc zraw/node (node/sexpr (get-opts zloc)))))
7171

72-
(defn ^{:added "0.4.4"} child-sexprs
72+
(defn child-sexprs
7373
"Return s-expression (the Clojure forms) of children of current node in `zloc`.
7474
7575
See docs for [sexpr nuances](/doc/01-user-guide.adoc#sexpr-nuances)."
@@ -81,7 +81,7 @@
8181
[zloc]
8282
(or (some-> zloc zraw/node node/length) 0))
8383

84-
(defn ^{:deprecated "0.4.0"} value
84+
(defn value
8585
"DEPRECATED. Return a tag/s-expression pair for inner nodes, or
8686
the s-expression itself for leaves."
8787
[zloc]
@@ -111,22 +111,22 @@
111111

112112
;; ## Write
113113

114-
(defn ^{:added "0.4.0"} string
114+
(defn string
115115
"Return string representing the current node in `zloc`."
116116
[zloc]
117117
(some-> zloc zraw/node node/string))
118118

119-
(defn ^{:deprecated "0.4.0"} ->string
119+
(defn ->string
120120
"DEPRECATED. Renamed to [[string]]."
121121
[zloc]
122122
(string zloc))
123123

124-
(defn ^{:added "0.4.0"} root-string
124+
(defn root-string
125125
"Return string representing the zipped-up `zloc` zipper."
126126
[zloc]
127127
(some-> zloc zraw/root node/string))
128128

129-
(defn ^{:deprecated "0.4.0"} ->root-string
129+
(defn ->root-string
130130
"DEPRECATED. Renamed to [[root-string]]."
131131
[zloc]
132132
(root-string zloc))

src/rewrite_clj/zip/walk.cljc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
2929
Pre-order traversal visits root before children.
3030
For example, traversal order of `(1 (2 3 (4 5) 6 (7 8)) 9)` is:
31-
31+
3232
1. `(1 (2 3 (4 5) 6 (7 8)) 9)`
3333
2. `1`
3434
3. `(2 3 (4 5) 6 (7 8))`
@@ -80,21 +80,21 @@
8080
:else
8181
loc))))
8282

83-
(defn ^{:added "0.4.9"} postwalk
83+
(defn postwalk
8484
"Return zipper modified by an isolated depth-first post-order traversal.
8585
8686
Pre-order traversal visits children before root.
8787
For example, traversal order of `(1 (2 3 (4 5) 6 (7 8)) 9)` is:
88-
88+
8989
1. `1`
9090
2. `2`
91-
3. `3`
91+
3. `3`
9292
4. `4`
9393
5. `5`
9494
6. `(4 5)`
9595
7. `6`
9696
8. `7`
97-
9. `8`
97+
9. `8`
9898
10. `(7 8)`
9999
11. `(2 3 (4 5) 6 (7 8))`
100100
12. `9`

src/rewrite_clj/zip/whitespace.cljc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262

6363
;; ## Insertion
6464

65-
(defn ^{:added "0.5.0"} insert-space-left
65+
(defn insert-space-left
6666
"Return zipper with `n` space whitespace node inserted to the left of the current node in `zloc`, without moving location.
6767
`n` defaults to 1."
6868
([zloc] (insert-space-left zloc 1))
@@ -72,7 +72,7 @@
7272
(zraw/insert-left zloc (nwhitespace/spaces n))
7373
zloc)))
7474

75-
(defn ^{:added "0.5.0"} insert-space-right
75+
(defn insert-space-right
7676
"Return zipper with `n` space whitespace node inserted to the right of the current node in `zloc`, without moving location.
7777
`n` defaults to 1."
7878
([zloc] (insert-space-right zloc 1))
@@ -82,14 +82,14 @@
8282
(zraw/insert-right zloc (nwhitespace/spaces n))
8383
zloc)))
8484

85-
(defn ^{:added "0.5.0"} insert-newline-left
85+
(defn insert-newline-left
8686
"Return zipper with `n` newlines node inserted to the left of the current node in `zloc`, without moving location.
8787
`n` defaults to 1."
8888
([zloc] (insert-newline-left zloc 1))
8989
([zloc n]
9090
(zraw/insert-left zloc (nwhitespace/newlines n))))
9191

92-
(defn ^{:added "0.5.0"} insert-newline-right
92+
(defn insert-newline-right
9393
"Return zipper with `n` newlines node inserted to the right of the current node in `zloc`, without moving location.
9494
`n` defaults to 1."
9595
([zloc] (insert-newline-right zloc 1))
@@ -98,28 +98,28 @@
9898

9999
;; ## Deprecated Functions
100100

101-
(defn ^{:deprecated "0.5.0"} prepend-space
101+
(defn prepend-space
102102
"DEPRECATED: renamed to [[insert-space-left]]."
103103
([zloc n]
104104
(insert-space-left zloc (or n 1)))
105105
([zloc]
106106
(prepend-space zloc nil)))
107107

108-
(defn ^{:deprecated "0.5.0"} append-space
108+
(defn append-space
109109
"DEPRECATED: renamed to [[insert-space-right]]."
110110
([zloc n]
111111
(insert-space-right zloc (or n 1)))
112112
([zloc]
113113
(append-space zloc nil)))
114114

115-
(defn ^{:deprecated "0.5.0"} prepend-newline
115+
(defn prepend-newline
116116
"DEPRECATED: renamed to [[insert-newline-left]]."
117117
([zloc n]
118118
(insert-newline-left zloc (or n 1)))
119119
([zloc]
120120
(prepend-newline zloc nil)))
121121

122-
(defn ^{:deprecated "0.5.0"} append-newline
122+
(defn append-newline
123123
"DEPRECATED: renamed to [[insert-newline-right]]."
124124
([zloc n]
125125
(insert-newline-right zloc (or n 1)))

0 commit comments

Comments
 (0)