Skip to content

Commit 84d1828

Browse files
asgerfCopilot
andcommitted
JavaScript extractor: recognise bun and tsx in shebang lines
Update the shebang regexp (renamed NODE_INVOCATION -> JS_INVOCATION) to also match 'bun' and 'tsx' so that scripts using these runtimes are correctly identified as JavaScript files. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent f2e7dca commit 84d1828

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

javascript/extractor/src/com/semmle/js/extractor/FileExtractor.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@
2828
*/
2929
public class FileExtractor {
3030
/**
31-
* Pattern to use on the shebang line of a script to identify whether it is a Node.js script.
31+
* Pattern to use on the shebang line of a script to identify whether it is a JavaScript script.
3232
*
33-
* <p>There are many different ways of invoking the Node.js interpreter (directly, through {@code
33+
* <p>There are many different ways of invoking a JavaScript interpreter (directly, through {@code
3434
* env}, with or without flags, with or without modified environment, etc.), so we simply look for
35-
* the word {@code "node"} or {@code "nodejs"}.
35+
* the word {@code "node"}, {@code "nodejs"}, {@code "bun"}, or {@code "tsx"}.
3636
*/
37-
private static final Pattern NODE_INVOCATION = Pattern.compile("\\bnode(js)?\\b");
37+
private static final Pattern JS_INVOCATION = Pattern.compile("\\b(node(js)?|bun|tsx)\\b");
3838

3939
/** A pattern that matches strings starting with `{ "...":`, suggesting JSON data. */
4040
public static final Pattern JSON_OBJECT_START =
@@ -157,7 +157,7 @@ protected boolean contains(File f, String lcExt, ExtractorConfig config) {
157157
// do a cheap check first
158158
if (firstLine != null && firstLine.startsWith("#!")) {
159159
// now do the slightly more expensive one
160-
return NODE_INVOCATION.matcher(firstLine).find();
160+
return JS_INVOCATION.matcher(firstLine).find();
161161
}
162162
} catch (IOException e) {
163163
Exceptions.ignore(e, "We simply skip this file.");
@@ -302,7 +302,7 @@ private boolean hasUnrecognizedShebang(byte[] bytes, int length) {
302302
int lengthOfText = endOfLine - startOfText;
303303
String text = new String(bytes, startOfText, lengthOfText, StandardCharsets.UTF_8);
304304
// Check if the shebang is a recognized JavaScript intepreter.
305-
return !NODE_INVOCATION.matcher(text).find();
305+
return !JS_INVOCATION.matcher(text).find();
306306
}
307307

308308
@Override

0 commit comments

Comments
 (0)