File tree Expand file tree Collapse file tree 3 files changed +20
-11
lines changed Expand file tree Collapse file tree 3 files changed +20
-11
lines changed Original file line number Diff line number Diff line change 3045
3045
cnt (count xs)]
3046
3046
(cond
3047
3047
(> cnt 1 )
3048
- (throw (error env ( str " Only one :refer-global form is allowed per namespace definition " ( count xs)) ))
3048
+ (throw (error env " Only one :refer-global form is allowed per namespace definition" ))
3049
3049
3050
3050
(== cnt 1 )
3051
- (let [[_ refers & {:keys [rename] :as renames-only}] (first xs)
3052
- err-str " Only [:refer-global (names)] and optionally `:rename {from to}` specs supported" ]
3053
- (when-not (and (vector? refers) (every? symbol refers))
3051
+ (let [[_ & {:keys [only rename] :as parsed-spec}] (first xs)
3052
+ err-str " Only (:refer-global :only [names]) and optionally `:rename {from to}` specs supported" ]
3053
+ (when-not (or (empty? only)
3054
+ (and (vector? only)
3055
+ (every? symbol only)))
3054
3056
(throw (error env err-str)))
3055
- (when-not (or (empty? renames-only)
3056
- (and (= 1 (count renames-only))
3057
- (contains? renames-only :rename )
3058
- (map? rename)
3057
+ (when-not (or (empty? rename)
3058
+ (and (map? rename)
3059
3059
(every? symbol (mapcat identity rename))))
3060
- (throw (error env (str err-str (pr-str renames-only)))))
3061
- {:use (zipmap refers (repeat 'js))
3060
+ (throw (error env (str err-str (pr-str parsed-spec)))))
3061
+ (when-not (every? #{:only :rename } (keys parsed-spec))
3062
+ (throw (error env (str err-str (pr-str parsed-spec)))))
3063
+ {:use (zipmap only (repeat 'js))
3062
3064
:rename (into {}
3063
3065
(map (fn [[orig new-name]]
3064
3066
[new-name (symbol " js" (str orig))]))
Original file line number Diff line number Diff line change 3116
3116
[& args]
3117
3117
`(~'ns* ~(cons :refer-clojure args)))
3118
3118
3119
+ (core/defmacro refer-global
3120
+ " Refer global js vars. Supports renaming via :rename.
3121
+
3122
+ (refer-global :only '[Date Symbol] :rename '{Symbol Sym})"
3123
+ [& args]
3124
+ `(~'ns* ~(cons :refer-global args)))
3125
+
3119
3126
; ; INTERNAL - do not use, only for Node.js
3120
3127
(core/defmacro load-file* [f]
3121
3128
`(goog/nodeGlobalRequire ~f))
Original file line number Diff line number Diff line change 389
389
390
390
(deftest test-parse-global-refer
391
391
(let [parsed (ana/parse-global-refer-spec {}
392
- '((:refer-global [Date] :rename {Symbol JSSymbol})))]
392
+ '((:refer-global :only [Date] :rename {Symbol JSSymbol})))]
393
393
(is (= parsed
394
394
'{:use {Date js}
395
395
:rename {JSSymbol js/Symbol}}))))
You can’t perform that action at this time.
0 commit comments