Skip to content

Commit a6131e7

Browse files
Update shebang.ts
1 parent cf13cc1 commit a6131e7

File tree

1 file changed

+11
-20
lines changed

1 file changed

+11
-20
lines changed

utils/src/ast-grep/shebang.ts

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,21 @@ export const getShebang = (root: SgRoot) => {
1919
},
2020
});
2121

22-
if (!allShebangs.length) return null;
22+
// Find the last consecutive shebang from the start of the file
23+
let lastValidShebang = null;
24+
let expectedLine = 0;
2325

24-
// Check if first shebang is at line 0 (start of file)
25-
const firstShebang = allShebangs[0];
26-
if (firstShebang.range().start.line !== 0) {
27-
return null; // Shebang not at start of file
28-
}
26+
for (const shebang of allShebangs) {
27+
const range = shebang.range();
2928

30-
// Collect all consecutive shebangs from the start
31-
const validShebangs = [firstShebang];
32-
for (let i = 1; i < allShebangs.length; i++) {
33-
const prevLine = allShebangs[i - 1].range().end.line;
34-
const currentLine = allShebangs[i].range().start.line;
35-
36-
// Check if this shebang is on the next consecutive line
37-
if (currentLine === prevLine || currentLine === prevLine + 1) {
38-
validShebangs.push(allShebangs[i]);
39-
} else {
40-
break; // Stop at first non-consecutive shebang
41-
}
29+
// Shebang must be at the expected line (0 for first, then consecutive)
30+
if (range.start.line !== expectedLine) break;
31+
32+
lastValidShebang = shebang;
33+
expectedLine = range.end.line + 1;
4234
}
4335

44-
// Return the last consecutive shebang from the start
45-
return validShebangs[validShebangs.length - 1];
36+
return lastValidShebang;
4637
};
4738

4839
/**

0 commit comments

Comments
 (0)