Skip to content

Commit 7d6e08e

Browse files
authored
Merge pull request #21461 from github/asger/js-shebang-bun-tsx
JS: Recognise bun and tsx in shebang lines
2 parents f11815c + b8c44be commit 7d6e08e

File tree

7 files changed

+23
-6
lines changed

7 files changed

+23
-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
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
| shebang_bun.ts |
2+
| shebang_node.ts |
3+
| shebang_tsx.ts |
4+
| tsconfig.json |
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import javascript
2+
3+
from File f
4+
select f.getRelativePath()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env bun
2+
const x: number = 1;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env node
2+
const x: number = 1;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env tsx
2+
const x: number = 1;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"include": ["."]
3+
}

0 commit comments

Comments
 (0)