Skip to content

Commit

Permalink
Also support pragmas in top level do
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude committed Dec 14, 2023
1 parent 3700d67 commit 91d5456
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
11 changes: 3 additions & 8 deletions src/squint/compiler.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -424,13 +424,7 @@
(let [next-t (-> (transpile-form next-form env)
not-empty)
next-js
(if (re-matches #"^(/\*|//|\"|\').*" (str next-t))
(let [js (str next-t "\n")]
(if-let [p (:pragmas env)]
(do (swap! p str js)
nil)
js))
(statement next-t))]
(cc/save-pragma env next-t)]
(recur (str transpiled next-js)))))))))

(defn compile-string*
Expand All @@ -447,7 +441,8 @@
cc/*target* :squint
*jsx* false
cc/*repl* (:repl opts cc/*repl*)]
(let [opts (merge {:ns-state (atom {})} opts)
(let [opts (merge {:ns-state (atom {})
:top-level true} opts)
imported-vars (atom {})
public-vars (atom #{})
aliases (atom (merge aliases {core-alias cc/*core-package*}))
Expand Down
12 changes: 11 additions & 1 deletion src/squint/compiler_common.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -282,13 +282,23 @@
(cond-> (format "(%sfunction () {\n %s\n})()" (if *async* "async " "") s)
*async* (wrap-await return?))))

(defn save-pragma [env next-t]
(if (and (:top-level env)
(re-matches #"^(/\*|//|\"|\').*" (str next-t)))
(let [js (str next-t "\n")]
(if-let [p (:pragmas env)]
(do (swap! p str js)
nil)
js))
(statement next-t)))

(defn emit-do [env exprs]
(let [bl (butlast exprs)
l (last exprs)
ctx (:context env)
statement-env (assoc env :context :statement)
iife? (and (seq bl) (= :expr ctx))
s (cond-> (str (str/join "" (map #(statement (emit % statement-env)) bl))
s (cond-> (str (str/join "" (map #(save-pragma env (emit % statement-env)) bl))
(emit l (assoc env :context
(if iife? :return
ctx))))
Expand Down
17 changes: 9 additions & 8 deletions test/squint/compiler_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2131,15 +2131,16 @@ new Foo();")
(is (eq [1 2 3 4 5 6] (jsv! '(into [] cat [[1 2 3] [4 5 6]])))))

(deftest pragmas-test
(let [{:keys [pragmas javascript]} (compiler/compile-string* "
\"use client\"
(let [code "\"use client\"
(js* \"// ts-check\")
(defn foo [] (merge nil nil))")]
(is (str/includes? pragmas "use client"))
(is (str/includes? pragmas "// ts-check"))
(is (not (str/includes? pragmas ";")))
(is (< (str/index-of javascript "use client") (str/index-of javascript "ts-check")))
(is (< (str/index-of javascript "ts-check") (str/index-of javascript "import")))))
(defn foo [] (merge nil nil))"]
(doseq [code [code (str/replace "(do %s)" "%s" code)]]
(let [{:keys [pragmas javascript]} (compiler/compile-string* code)]
(is (str/includes? pragmas "use client"))
(is (str/includes? pragmas "// ts-check"))
(is (not (str/includes? pragmas ";")))
(is (< (str/index-of javascript "use client") (str/index-of javascript "ts-check")))
(is (< (str/index-of javascript "ts-check") (str/index-of javascript "import")))))))

(defn init []
(t/run-tests 'squint.compiler-test 'squint.jsx-test 'squint.string-test))

0 comments on commit 91d5456

Please sign in to comment.