Welcome! Please see the About page for a little more info on how this works.

+1 vote
in ClojureScript by
Not sure if it's a bug or expected behaviour, but this:


(defn test-fn []
  (let [href     js/location.href
        location "123"]
    href))


gets compiled to this (not in advanced mode):


cognician.chat.ui.pages.insights.test_fn = (function cognician$chat$ui$pages$insights$test_fn(){
var href = location.href;
var location = "123";
return href;
});


and local {{location}} var shadows global I'm trying to access in {{location.href}}.

That sort of thing is expected and one should pay attention and work around stuff like this in JS, but in CLJS it's very confusing because nothing hints what am I doing wrong and why that code fails. I remember one of ClojureScript goals was to fix JS semantics, so maybe there's a way this might be addressed? At least throw a warning, maybe?

3 Answers

0 votes
by
_Comment made by: thheller_

This came up recently on the #cljs-dev slack channel. There is definitely a bug somewhere.


(let [href     js/location.href
      location "123"]
  href)

produces

var href_51444 = location.href;
var location_51445 = "123"; // << correct


So it works at the top level, but when inside a {{defn}} (and others) we get


(ns test)
(defn test-fn []
  (let [href     js/location.href
        location "123"]
    href))



test.test_fn = (function test$test_fn(){
var href = location.href;
var location = "123"; // << incorrect
return href;
});


0 votes
by

Comment made by: dnolen

Taking a quick look it seems that maybe we aren't checking :js-globals consistently and often only looking at locals? Also now that externs inference is a thing we should probably compute :js-globals from all known externs instead of the obviously incomplete list we currently have in place.

0 votes
by
Reference: https://clojure.atlassian.net/browse/CLJS-1899 (reported by tonsky)
...