Skip to content

Commit d7a0c1d

Browse files
authored
CLJS-3368: let binding can shadow globals, leading to strange behavior (#171)
* check :js-globals for shadowing in analysis of let bindings * use the standard analyzer context in the REPL * add a compiler test
1 parent 0d5b1ba commit d7a0c1d

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

src/main/clojure/cljs/analyzer.cljc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2384,7 +2384,8 @@
23842384
(let [init-expr (analyze-let-binding-init env init (cons {:params bes} *loop-lets*))
23852385
line (get-line name env)
23862386
col (get-col name env)
2387-
shadow (handle-symbol-local name (get-in env [:locals name]))
2387+
shadow (or (handle-symbol-local name (get-in env [:locals name]))
2388+
(get-in env [:js-globals name]))
23882389
be {:name name
23892390
:line line
23902391
:column col

src/main/clojure/cljs/repl.cljc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1113,7 +1113,7 @@
11131113
ana/*fn-invoke-direct* (and static-fns fn-invoke-direct)
11141114
*repl-opts* opts]
11151115
(try
1116-
(let [env {:context :expr :locals {}}
1116+
(let [env (assoc (ana/empty-env) :context :expr)
11171117
special-fns (merge default-special-fns special-fns)
11181118
is-special-fn? (set (keys special-fns))
11191119
request-prompt (Object.)

src/test/clojure/cljs/compiler_tests.clj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,14 @@
365365
ns-info (env/ensure (comp/emit-source input output "cljs" {}))]
366366
(is (= 'foo.foo (:ns ns-info)))))
367367

368+
(deftest test-3368-global-shadowing
369+
(testing "Let binding which use JS global names should get shadowed"
370+
(let [code (env/with-compiler-env (env/default-compiler-env)
371+
(compile-form-seq
372+
'[(defn foo []
373+
(let [window js/window]
374+
window))]))]
375+
(is (re-find #"window__\$1" code)))))
368376

369377
;; CLJS-1225
370378

0 commit comments

Comments
 (0)