Skip to content

Commit 6256ff8

Browse files
author
Bruce Hauman
committed
refactor: improve babashka shebang detection with regex pattern
Replace hardcoded string prefix checks with a flexible regex pattern that matches any absolute path ending in bb or env bb, allows 1-3 spaces between env and bb, supports optional arguments after bb, and prevents false positives from excessive whitespace
1 parent 586fe6b commit 6256ff8

File tree

1 file changed

+24
-25
lines changed

1 file changed

+24
-25
lines changed

src/clojure_mcp/utils/valid_paths.clj

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -82,39 +82,38 @@
8282

8383
(validate-path path current-dir allowed-dirs)))
8484

85-
(defn- babashka-shebang?
86-
[file-path]
87-
(when (path-exists? file-path)
88-
(try
89-
(with-open [r (io/reader file-path)]
90-
(let [line (-> r line-seq first)]
91-
(and line
92-
(or (str/starts-with? line "#!/usr/bin/env bb")
93-
(str/starts-with? line "#!/usr/bin/bb")
94-
(str/starts-with? line "#!/bin/bb")))))
95-
(catch Exception _ false))))
96-
97-
(defn clojure-file?
98-
"Checks if a file path has a Clojure-related extension or Babashka shebang."
99-
85+
(defn- babashka-shebang?
86+
[file-path]
87+
(when (path-exists? file-path)
88+
(try
89+
(with-open [r (io/reader file-path)]
90+
(let [line (-> r line-seq first)]
91+
(and line
92+
(re-matches #"^#!/[^\s]+/(bb|env\s{1,3}bb)(\s.*)?$" line))))
93+
(catch Exception _ false))))
94+
95+
(defn clojure-file?
96+
"Checks if a file path has a Clojure-related extension or Babashka shebang.
97+
10098
Supported extensions:
10199
- .clj (Clojure)
102100
- .cljs (ClojureScript)
103101
- .cljc (Clojure/ClojureScript shared)
104102
- .bb (Babashka)
105103
- .edn (Extensible Data Notation)
106104
- .lpy (Librepl)
105+
107106
Also detects files starting with a Babashka shebang (`bb`)."
108-
[file-path]
109-
(when file-path
110-
(let [lower-path (str/lower-case file-path)]
111-
(or (str/ends-with? lower-path ".clj")
112-
(str/ends-with? lower-path ".cljs")
113-
(str/ends-with? lower-path ".cljc")
114-
(str/ends-with? lower-path ".bb")
115-
(str/ends-with? lower-path ".lpy")
116-
(str/ends-with? lower-path ".edn")
117-
(babashka-shebang? file-path)))))
107+
[file-path]
108+
(when file-path
109+
(let [lower-path (str/lower-case file-path)]
110+
(or (str/ends-with? lower-path ".clj")
111+
(str/ends-with? lower-path ".cljs")
112+
(str/ends-with? lower-path ".cljc")
113+
(str/ends-with? lower-path ".bb")
114+
(str/ends-with? lower-path ".lpy")
115+
(str/ends-with? lower-path ".edn")
116+
(babashka-shebang? file-path)))))
118117

119118
(defn extract-paths-from-bash-command
120119
"Extract file/directory paths from a bash command string.

0 commit comments

Comments
 (0)