Skip to content

Commit

Permalink
Ignore hashhbang comments (#4676)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Oct 16, 2022
1 parent 21a4bdf commit 9cb38d2
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/ast/nodes/Program.ts
Expand Up @@ -32,8 +32,13 @@ export default class Program extends NodeBase {
}

render(code: MagicString, options: RenderOptions): void {
let start = this.start;
if (code.original.startsWith('#!')) {
start = Math.min(code.original.indexOf('\n') + 1, this.end);
code.remove(0, start);
}
if (this.body.length > 0) {
renderStatementList(this.body, code, this.start, this.end, options);
renderStatementList(this.body, code, start, this.end, options);
} else {
super.render(code, options);
}
Expand Down
3 changes: 3 additions & 0 deletions test/form/samples/hashbang/_config.js
@@ -0,0 +1,3 @@
module.exports = {
description: 'supports input files with leading hashbang comment'
};
3 changes: 3 additions & 0 deletions test/form/samples/hashbang/_expected.js
@@ -0,0 +1,3 @@
console.log('other');

console.log('main');
3 changes: 3 additions & 0 deletions test/form/samples/hashbang/main.js
@@ -0,0 +1,3 @@
#!/usr/bin/env node
import './other';
console.log('main');
2 changes: 2 additions & 0 deletions test/form/samples/hashbang/other.js
@@ -0,0 +1,2 @@
#!/usr/bin/env node
console.log('other');

0 comments on commit 9cb38d2

Please sign in to comment.