Skip to content

Commit

Permalink
Improve pragma detection
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude committed Dec 14, 2023
1 parent 91d5456 commit 6c3007c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/squint/compiler_common.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@

(defn save-pragma [env next-t]
(if (and (:top-level env)
(re-matches #"^(/\*|//|\"|\').*" (str next-t)))
(re-find #"^(/\*|//|\"|\')" (str next-t)))
(let [js (str next-t "\n")]
(if-let [p (:pragmas env)]
(do (swap! p str js)
Expand Down
11 changes: 9 additions & 2 deletions test/squint/compiler_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2133,14 +2133,21 @@ new Foo();")
(deftest pragmas-test
(let [code "\"use client\"
(js* \"// ts-check\")
(js* \"/*
yolo
*/\")
(js* \"'use server'\")
(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")))))))
(is (< (str/index-of javascript "use client")
(str/index-of javascript "ts-check")
(str/index-of javascript "yolo")
(str/index-of javascript "'use server'")
(str/index-of javascript "import")))))))

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

0 comments on commit 6c3007c

Please sign in to comment.