Skip to content

Commit 525edf8

Browse files
fix(runtime): handle BOM before shebang (#158)
1 parent 142f8f0 commit 525edf8

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

src/runtime.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export const DEFAULT_MAX = 3;
2323

2424
/** Strip a leading `#!…` shebang line so `#!/usr/bin/env moshscript` files parse. */
2525
export function stripShebang(src) {
26-
return src.startsWith("#!") ? src.replace(/^#![^\n]*\r?\n?/, "") : src;
26+
return src.replace(/^\uFEFF?#![^\r\n]*(?:\r?\n|$)/, "");
2727
}
2828

2929
// The loop governor: owns `alive` truthiness and the iteration budget.

test/runtime.test.mjs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,15 @@ test("a leading shebang line is stripped, not executed", async () => {
8484
assert.deepEqual(calls, ["ran"]);
8585
});
8686

87+
test("a UTF-8 BOM before a shebang is stripped", async () => {
88+
const { calls, registry } = recorder();
89+
await runScript(`\uFEFF#!/usr/bin/env moshscript\npush("ran");`, { commands: registry });
90+
assert.deepEqual(calls, ["ran"]);
91+
});
92+
8793
test("stripShebang leaves shebang-less source untouched", () => {
8894
assert.equal(stripShebang('push("x");'), 'push("x");');
95+
assert.equal(stripShebang('\uFEFFpush("x");'), '\uFEFFpush("x");');
8996
assert.equal(stripShebang("#!/usr/bin/env moshscript\npush();"), "push();");
9097
});
9198

0 commit comments

Comments
 (0)