Permalink
Browse files

CLJS-1620: In JavaScript ES2015 modules default export name is munged…

… to default$
  • Loading branch information...
anmonteiro authored and swannodette committed Jul 30, 2017
1 parent 1a21fd2 commit aff39caac5f512fdeda7ad41cdd6224d2d4ca48f
View
@@ -15,7 +15,30 @@ before_install:
before_script:
- script/bootstrap
- mkdir -p builds/out-adv
- bin/cljsc src/test/cljs "{:optimizations :advanced :output-wrapper true :verbose true :compiler-stats true :parallel-build true :output-dir \"builds/out-adv\" :npm-deps {:lodash \"4.17.4\"} :closure-warnings {:non-standard-jsdoc :off :global-this :off} :install-deps true :foreign-libs [{:file \"src/test/cljs/calculator_global.js\" :provides [\"calculator\"] :global-exports {calculator Calculator}} {:file \"src/test/cljs/es6_dep.js\" :module-type :es6 :provides [\"es6_calc\"]} {:file \"src/test/cljs/calculator_amd.js\" :module-type :amd :provides [\"calculator_amd\"] :requires [\"es6_calc\"]}]}" > builds/out-adv/core-advanced-test.js
- bin/cljsc src/test/cljs "{:optimizations :advanced
:output-wrapper true
:verbose true
:compiler-stats true
:parallel-build true
:output-dir \"builds/out-adv\"
:npm-deps {:lodash \"4.17.4\"}
:closure-warnings {:non-standard-jsdoc :off :global-this :off}
:language-in :es6
:language-out :es5
:install-deps true
:foreign-libs [{:file \"src/test/cljs/calculator_global.js\"
:provides [\"calculator\"]
:global-exports {calculator Calculator}}
{:file \"src/test/cljs/es6_dep.js\"
:module-type :es6
:provides [\"es6_calc\"]}
{:file \"src/test/cljs/calculator_amd.js\"
:module-type :amd
:provides [\"calculator_amd\"]
:requires [\"es6_calc\"]}
{:file \"src/test/cljs/es6_default_hello.js\"
:provides [\"es6_default_hello\"]
:module-type :es6}]}" > builds/out-adv/core-advanced-test.js
script:
- lein test
View
@@ -12,7 +12,30 @@ mkdir -p builds/out-adv
possible=5
ran=0
if ! bin/cljsc src/test/cljs "{:optimizations :advanced :output-wrapper true :verbose true :compiler-stats true :parallel-build true :output-dir \"builds/out-adv\" :npm-deps {:lodash \"4.17.4\"} :closure-warnings {:non-standard-jsdoc :off :global-this :off} :install-deps true :foreign-libs [{:file \"src/test/cljs/calculator_global.js\" :provides [\"calculator\"] :global-exports {calculator Calculator}} {:file \"src/test/cljs/es6_dep.js\" :module-type :es6 :provides [\"es6_calc\"]} {:file \"src/test/cljs/calculator_amd.js\" :module-type :amd :provides [\"calculator_amd\"] :requires [\"es6_calc\"]}]}" > builds/out-adv/core-advanced-test.js; then
if ! bin/cljsc src/test/cljs "{:optimizations :advanced
:output-wrapper true
:verbose true
:compiler-stats true
:parallel-build true
:output-dir \"builds/out-adv\"
:npm-deps {:lodash \"4.17.4\"}
:closure-warnings {:non-standard-jsdoc :off :global-this :off}
:install-deps true
:language-in :es6
:language-out :es5
:foreign-libs [{:file \"src/test/cljs/calculator_global.js\"
:provides [\"calculator\"]
:global-exports {calculator Calculator}}
{:file \"src/test/cljs/es6_dep.js\"
:module-type :es6
:provides [\"es6_calc\"]}
{:file \"src/test/cljs/calculator_amd.js\"
:module-type :amd
:provides [\"calculator_amd\"]
:requires [\"es6_calc\"]}
{:file \"src/test/cljs/es6_default_hello.js\"
:provides [\"es6_default_hello\"]
:module-type :es6}]}" > builds/out-adv/core-advanced-test.js; then
>&2 echo ClojureScript compilation failed
exit 1
fi;
View
@@ -12,7 +12,31 @@ possible=5
ran=0
#bin/cljsc test >out/core-test.js
if ! bin/cljsc src/test/cljs "{:optimizations :simple :static-fns true :output-dir \"builds/out-simp\" :cache-analysis true :output-wrapper true :verbose true :compiler-stats true :npm-deps {:lodash \"4.17.4\"} :closure-warnings {:non-standard-jsdoc :off :global-this :off} :install-deps true :foreign-libs [{:file \"src/test/cljs/calculator_global.js\" :provides [\"calculator\"] :global-exports {calculator Calculator}} {:file \"src/test/cljs/es6_dep.js\" :module-type :es6 :provides [\"es6_calc\"]} {:file \"src/test/cljs/calculator_amd.js\" :module-type :amd :provides [\"calculator_amd\"] :requires [\"es6_calc\"]}]}" > builds/out-simp/core-simple-test.js; then
if ! bin/cljsc src/test/cljs "{:optimizations :simple
:static-fns true
:output-dir \"builds/out-simp\"
:cache-analysis true
:output-wrapper true
:verbose true
:compiler-stats true
:npm-deps {:lodash \"4.17.4\"}
:closure-warnings {:non-standard-jsdoc :off :global-this :off}
:install-deps true
:language-in :es6
:language-out :es5
:foreign-libs [{:file \"src/test/cljs/calculator_global.js\"
:provides [\"calculator\"]
:global-exports {calculator Calculator}}
{:file \"src/test/cljs/es6_dep.js\"
:module-type :es6
:provides [\"es6_calc\"]}
{:file \"src/test/cljs/calculator_amd.js\"
:module-type :amd
:provides [\"calculator_amd\"]
:requires [\"es6_calc\"]}
{:file \"src/test/cljs/es6_default_hello.js\"
:provides [\"es6_default_hello\"]
:module-type :es6}]}" > builds/out-simp/core-simple-test.js; then
>&2 echo ClojureScript compilation failed
exit 1
fi;
@@ -2537,7 +2537,7 @@ of parameters as well."
(merge ; create the result map
(into (array-map) ; start with the default values, make sure the order is right
(reverse (for [[name [default]] (:params def)] [name [default offset]])))
(reverse (for [[name [default-def]] (:params def)] [name [default-def offset]])))
(reduce #(apply assoc %1 %2) {} (filter #(first (nth % 1)) (zipmap (keys (:params def)) params))) ; add the specified parameters, filtering out nils
flags)); and finally add the flags
@@ -576,11 +576,11 @@ goog.events.getProxy/f<@http://localhost:9000/out/goog/events/events.js:276:16"
(second (first columns)))))
(adjust [mapped]
(vec (map #(%1 %2) [inc inc identity] mapped)))]
(let [default [line column nil]]
(let [default-ret [line column nil]]
;; source maps are 0 indexed for lines
(if-let [columns (get source-map (dec line))]
(adjust (map (get-best-column columns column) [:line :col :name]))
default)))))
default-ret)))))
(defn mapped-frame
"Given opts and a canonicalized JavaScript stacktrace frame, return the
@@ -386,7 +386,7 @@
first)))
(defn mapped-line-and-column [filename line column]
(let [default [filename line column]]
(let [default-ret [filename line column]]
(if-let [source-map (:source-map (get-current-env))]
;; source maps are 0 indexed for lines
(if-let [columns (get-in source-map [filename (dec line)])]
@@ -400,8 +400,8 @@
mapping
(second (first columns))))
[:source :line :col]))
default)
default)))
default-ret)
default-ret)))
(defn file-and-line [exception depth]
;; TODO: flesh out
@@ -182,6 +182,9 @@
"volatile" "while" "with" "yield" "methods"
"null" "constructor"})
(def es5-allowed
#{"default"})
#?(:clj (def SENTINEL (Object.))
:cljs (def SENTINEL (js-obj)))
@@ -741,7 +744,10 @@
;; we need to check both keys and values of the JS module index, because
;; macroexpansion will be looking for the provided name - António Monteiro
(contains?
(into #{} (mapcat identity) (get-in @env/*compiler* [:js-module-index]))
(into #{}
(mapcat (fn [[k v]]
[k (:name v)]))
(get-in @env/*compiler* [:js-module-index]))
(str module)))
(defn node-module-dep?
@@ -791,16 +797,16 @@
(defn resolve-ns-alias
([env name]
(resolve-ns-alias env name (symbol name)))
([env name default]
([env name not-found]
(let [sym (symbol name)]
(get (:requires (:ns env)) sym default))))
(get (:requires (:ns env)) sym not-found))))
(defn resolve-macro-ns-alias
([env name]
(resolve-macro-ns-alias env name (symbol name)))
([env name default]
([env name not-found]
(let [sym (symbol name)]
(get (:require-macros (:ns env)) sym default))))
(get (:require-macros (:ns env)) sym not-found))))
(defn confirm-ns
"Given env, an analysis environment, and ns-sym, a symbol identifying a
@@ -950,6 +956,11 @@
(defmulti resolve* (fn [sym full-ns current-ns] (ns->module-type full-ns)))
(defmethod resolve* :js
[sym full-ns current-ns]
{:name (symbol (str full-ns) (str (name sym)))
:ns full-ns})
(defmethod resolve* :node
[sym full-ns current-ns]
{:name (symbol (str current-ns) (str (munge-node-lib full-ns) "." (name sym)))
@@ -985,7 +996,7 @@
module-type (ns->module-type ns)]
(case module-type
:js {:name (symbol
(or (gets @env/*compiler* :js-module-index ns)
(or (gets @env/*compiler* :js-module-index ns :name)
(resolve-ns-alias env ns)))
:ns 'js}
:node {:name (symbol (str current-ns)
@@ -1031,7 +1042,7 @@
ns)
full-ns (resolve-ns-alias env ns
(or (and (js-module-exists? ns)
(get-in @env/*compiler* [:js-module-index ns]))
(gets @env/*compiler* :js-module-index ns :name))
(symbol ns)))]
(when (some? confirm)
(when (not= current-ns full-ns)
@@ -1348,14 +1359,14 @@
:children [test-expr then-expr else-expr]}))
(defmethod parse 'case*
[op env [_ sym tests thens default :as form] name _]
[op env [_ sym tests thens default-clause :as form] name _]
(assert (symbol? sym) "case* must switch on symbol")
(assert (every? vector? tests) "case* tests must be grouped in vectors")
(let [expr-env (assoc env :context :expr)
v (disallowing-recur (analyze expr-env sym))
tests (mapv #(mapv (fn [t] (analyze expr-env t)) %) tests)
thens (mapv #(analyze env %) thens)
default (analyze env default)]
default-clause (analyze env default-clause)]
(assert (every? (fn [t]
(or
(-> t :info :const)
@@ -1364,8 +1375,8 @@
(apply concat tests))
"case* tests must be numbers, strings, or constants")
{:env env :op :case* :form form
:v v :tests tests :thens thens :default default
:children (vec (concat [v] tests thens (if default [default])))}))
:v v :tests tests :thens thens :default default-clause
:children (vec (concat [v] tests thens (if default-clause [default-clause])))}))
(defmethod parse 'throw
[op env [_ throw :as form] name _]
@@ -1404,9 +1415,9 @@
finally (when (seq fblock)
(analyze (assoc env :context :statement) `(do ~@(rest fblock))))
e (when (or (seq cblocks) dblock) (gensym "e"))
default (if-let [[_ _ name & cb] dblock]
`(cljs.core/let [~name ~e] ~@cb)
`(throw ~e))
default-block (if-let [[_ _ name & cb] dblock]
`(cljs.core/let [~name ~e] ~@cb)
`(throw ~e))
cblock (if (seq cblocks)
`(cljs.core/cond
~@(mapcat
@@ -1415,8 +1426,8 @@
`[(cljs.core/instance? ~type ~e)
(cljs.core/let [~name ~e] ~@cb)])
cblocks)
:else ~default)
default)
:else ~default-block)
default-block)
locals (:locals catchenv)
locals (if e
(assoc locals e
@@ -2339,7 +2350,7 @@
;; Google Closure compiler, e.g. module$resources$libs$calculator.
;; This means that we need to create an alias from the module name
;; given with :provides to the new name.
[lib js-module-provides] (if-some [js-module-name (get-in @env/*compiler* [:js-module-index (str lib)])]
[lib js-module-provides] (if-some [js-module-name (gets @env/*compiler* :js-module-index (str lib) :name)]
[(symbol js-module-name) lib]
[lib nil])
{alias :as referred :refer renamed :rename
@@ -2321,17 +2321,18 @@
js-modules)
js-modules (convert-js-modules js-modules opts)]
;; Write modules to disk, update compiler state and build new options
(reduce (fn [new-opts {:keys [file] :as ijs}]
(reduce (fn [new-opts {:keys [file module-type] :as ijs}]
(let [ijs (write-javascript opts ijs)
module-name (-> (deps/load-library (:out-file ijs)) first :provides first)]
(doseq [provide (:provides ijs)]
(swap! env/*compiler*
#(update-in % [:js-module-index] assoc provide module-name)))
#(update-in % [:js-module-index] assoc provide {:name module-name
:module-type module-type})))
(-> new-opts
(update-in [:libs] (comp vec conj) (:out-file ijs))
;; js-module might be defined in either, so update both
(update-in [:foreign-libs] (comp vec (fn [libs] (remove #(= (:file %) file) libs))))
(update-in [:ups-foreign-libs] (comp vec (fn [libs] (remove #(= (:file %) file) libs)))))))
(update-in [:libs] (comp vec conj) (:out-file ijs))
;; js-module might be defined in either, so update both
(update-in [:foreign-libs] (comp vec (fn [libs] (remove #(= (:file %) file) libs))))
(update-in [:ups-foreign-libs] (comp vec (fn [libs] (remove #(= (:file %) file) libs)))))))
opts js-modules)))
opts)))
@@ -36,6 +36,15 @@
(def js-reserved ana/js-reserved)
(def ^:private es5>=
(into #{}
(comp
(mapcat (fn [lang]
[lang (keyword (string/replace (name lang) #"^ecmascript" "es"))])))
[:ecmascript5 :ecmascript5-strict :ecmascript6 :ecmascript6-strict
:ecmascript-2015 :ecmascript6-typed :ecmascript-2016 :ecmascript-2017
:ecmascript-next]))
(def ^:dynamic *recompiled* nil)
(def ^:dynamic *inputs* nil)
(def ^:dynamic *source-map-data* nil)
@@ -333,9 +342,10 @@
[{:keys [info env form] :as ast}]
(if-let [const-expr (:const-expr ast)]
(emit (assoc const-expr :env env))
(let [var-name (:name info)
(let [{:keys [options] :as cenv} @env/*compiler*
var-name (:name info)
info (if (= (namespace var-name) "js")
(let [js-module-name (get-in @env/*compiler* [:js-module-index (name var-name)])]
(let [js-module-name (get-in cenv [:js-module-index (name var-name) :name])]
(or js-module-name (name var-name)))
info)]
; We need a way to write bindings out to source maps and javascript
@@ -346,10 +356,13 @@
; (prevents duplicate fn-param-names)
(emits (munge ast))
(when-not (= :statement (:context env))
(emit-wrap env
(emits
(cond-> info
(not= form 'js/-Infinity) munge))))))))
(let [reserved (cond-> js-reserved
(es5>= (:language-out options))
(set/difference ana/es5-allowed))]
(emit-wrap env
(emits
(cond-> info
(not= form 'js/-Infinity) (munge reserved))))))))))
(defmethod emit* :var-special
[{:keys [env var sym meta] :as arg}]
@@ -3,7 +3,8 @@
(:require [cljs.test :refer [deftest is]]
["lodash/array" :as array :refer [slice] :rename {slice slc}]
[calculator :as vector :refer [add] :rename {add plus}]
[es6_calc]))
[es6_calc]
[es6_default_hello :as es6hello]))
(def array #js [1 2 3])
@@ -25,3 +26,6 @@
(deftest test-cljs-2286
(is (= 3 (es6_calc/calculator.add 1 2))))
(deftest test-cljs-1620
(is (= "Hello, world!" (es6hello/default))))
@@ -0,0 +1,3 @@
export default function sayHello () {
return "Hello, world!";
};
Oops, something went wrong.

0 comments on commit aff39ca

Please sign in to comment.