Emit first encounter shebang at the top of a output file

This commit is contained in:
Kanchalai Tanglertsampan
2017-02-23 15:27:22 -08:00
parent eca4af5029
commit 231c3cb8ab
3 changed files with 24 additions and 9 deletions

View File

@@ -274,6 +274,7 @@ namespace ts {
function writeBundle(bundle: Bundle, output: EmitTextWriter) {
const previousWriter = writer;
setWriter(output);
emitShebangIfNeeded(bundle);
emitHelpersIndirect(bundle);
for (const sourceFile of bundle.sourceFiles) {
print(EmitHint.SourceFile, sourceFile, sourceFile);
@@ -285,6 +286,7 @@ namespace ts {
function writeFile(sourceFile: SourceFile, output: EmitTextWriter) {
const previousWriter = writer;
setWriter(output);
emitShebangIfNeeded(sourceFile);
print(EmitHint.SourceFile, sourceFile, sourceFile);
reset();
writer = previousWriter;
@@ -2051,7 +2053,6 @@ namespace ts {
function emitSourceFile(node: SourceFile) {
writeLine();
emitShebang();
emitBodyIndirect(node, node.statements, emitSourceFileWorker);
}
@@ -2095,11 +2096,26 @@ namespace ts {
// Helpers
//
function emitShebang() {
const shebang = getShebang(currentSourceFile.text);
if (shebang) {
write(shebang);
writeLine();
function emitShebangIfNeeded(sourceFileOrBundle: Bundle | SourceFile) {
if (sourceFileOrBundle.kind === SyntaxKind.SourceFile) {
emitShebangInSourceFile(sourceFileOrBundle as SourceFile);
}
else {
for (const sourceFile of (sourceFileOrBundle as Bundle).sourceFiles) {
// Emit only the first Shebang with encounter
if (emitShebangInSourceFile(sourceFile)) {
break;
}
}
}
function emitShebangInSourceFile(sourceFile: SourceFile): boolean {
const shebang = getShebang(sourceFile.text);
if (shebang) {
write(shebang);
writeLine();
return true;
}
}
}

View File

@@ -4,6 +4,7 @@ class Doo {}
class Scooby extends Doo {}
//// [outFile.js]
#!/usr/bin/env gjs
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
@@ -14,7 +15,6 @@ var __extends = (this && this.__extends) || (function () {
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
#!/usr/bin/env gjs
var Doo = (function () {
function Doo() {
}

View File

@@ -11,6 +11,7 @@ class Dood {}
class Scoobyd extends Dood {}
//// [outFile.js]
#!/usr/bin/env gjs
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
@@ -21,7 +22,6 @@ var __extends = (this && this.__extends) || (function () {
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
#!/usr/bin/env gjs
var Doo = (function () {
function Doo() {
}
@@ -34,7 +34,6 @@ var Scooby = (function (_super) {
}
return Scooby;
}(Doo));
#!/usr/bin/env js
var Dood = (function () {
function Dood() {
}