Skip to content

Commit 231c3cb

Browse files
author
Kanchalai Tanglertsampan
committed
Emit first encounter shebang at the top of a output file
1 parent eca4af5 commit 231c3cb

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

src/compiler/emitter.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ namespace ts {
274274
function writeBundle(bundle: Bundle, output: EmitTextWriter) {
275275
const previousWriter = writer;
276276
setWriter(output);
277+
emitShebangIfNeeded(bundle);
277278
emitHelpersIndirect(bundle);
278279
for (const sourceFile of bundle.sourceFiles) {
279280
print(EmitHint.SourceFile, sourceFile, sourceFile);
@@ -285,6 +286,7 @@ namespace ts {
285286
function writeFile(sourceFile: SourceFile, output: EmitTextWriter) {
286287
const previousWriter = writer;
287288
setWriter(output);
289+
emitShebangIfNeeded(sourceFile);
288290
print(EmitHint.SourceFile, sourceFile, sourceFile);
289291
reset();
290292
writer = previousWriter;
@@ -2051,7 +2053,6 @@ namespace ts {
20512053

20522054
function emitSourceFile(node: SourceFile) {
20532055
writeLine();
2054-
emitShebang();
20552056
emitBodyIndirect(node, node.statements, emitSourceFileWorker);
20562057
}
20572058

@@ -2095,11 +2096,26 @@ namespace ts {
20952096
// Helpers
20962097
//
20972098

2098-
function emitShebang() {
2099-
const shebang = getShebang(currentSourceFile.text);
2100-
if (shebang) {
2101-
write(shebang);
2102-
writeLine();
2099+
function emitShebangIfNeeded(sourceFileOrBundle: Bundle | SourceFile) {
2100+
if (sourceFileOrBundle.kind === SyntaxKind.SourceFile) {
2101+
emitShebangInSourceFile(sourceFileOrBundle as SourceFile);
2102+
}
2103+
else {
2104+
for (const sourceFile of (sourceFileOrBundle as Bundle).sourceFiles) {
2105+
// Emit only the first Shebang with encounter
2106+
if (emitShebangInSourceFile(sourceFile)) {
2107+
break;
2108+
}
2109+
}
2110+
}
2111+
2112+
function emitShebangInSourceFile(sourceFile: SourceFile): boolean {
2113+
const shebang = getShebang(sourceFile.text);
2114+
if (shebang) {
2115+
write(shebang);
2116+
writeLine();
2117+
return true;
2118+
}
21032119
}
21042120
}
21052121

tests/baselines/reference/emitBundleWithShebang1.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ class Doo {}
44
class Scooby extends Doo {}
55

66
//// [outFile.js]
7+
#!/usr/bin/env gjs
78
var __extends = (this && this.__extends) || (function () {
89
var extendStatics = Object.setPrototypeOf ||
910
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
@@ -14,7 +15,6 @@ var __extends = (this && this.__extends) || (function () {
1415
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
1516
};
1617
})();
17-
#!/usr/bin/env gjs
1818
var Doo = (function () {
1919
function Doo() {
2020
}

tests/baselines/reference/emitBundleWithShebang2.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class Dood {}
1111
class Scoobyd extends Dood {}
1212

1313
//// [outFile.js]
14+
#!/usr/bin/env gjs
1415
var __extends = (this && this.__extends) || (function () {
1516
var extendStatics = Object.setPrototypeOf ||
1617
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
@@ -21,7 +22,6 @@ var __extends = (this && this.__extends) || (function () {
2122
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
2223
};
2324
})();
24-
#!/usr/bin/env gjs
2525
var Doo = (function () {
2626
function Doo() {
2727
}
@@ -34,7 +34,6 @@ var Scooby = (function (_super) {
3434
}
3535
return Scooby;
3636
}(Doo));
37-
#!/usr/bin/env js
3837
var Dood = (function () {
3938
function Dood() {
4039
}

0 commit comments

Comments
 (0)