Address PR comments

This commit is contained in:
Yui T
2017-03-02 10:51:51 -08:00
parent b335adbb21
commit 51e40112ec
4 changed files with 67 additions and 79 deletions
+31 -40
View File
@@ -212,7 +212,6 @@ namespace ts {
} = comments;
let currentSourceFile: SourceFile;
let statementOffset: number; // We cache the index of first non prologue so we don't have to recalculate it multiple times like in emitBodyInDirect
let nodeIdToGeneratedName: string[]; // Map of generated names for specific nodes.
let autoGeneratedIdToGeneratedName: string[]; // Map of generated names for temp and loop variables.
let generatedNames: Map<string>; // Set of names generated by the NameGenerator.
@@ -312,7 +311,6 @@ namespace ts {
function setSourceFile(sourceFile: SourceFile) {
currentSourceFile = sourceFile;
statementOffset = getIndexOfFirstNonPrologueDirectives(sourceFile.statements);
comments.setSourceFile(sourceFile);
if (onSetSourceFile) {
onSetSourceFile(sourceFile);
@@ -736,19 +734,6 @@ namespace ts {
return node && substituteNode && substituteNode(hint, node) || node;
}
function emitBodyIndirect(node: Node, elements: NodeArray<Node>, emitCallback: (node: Node) => void): void {
// If the node is a sourceFile and it has prologueDirective (statmentOffSet is not zero) that is synthesize
// We will need to emit detached comment here because emitPrologueDirective will not emit comments of such prologue directive
const shouldEmitDetachedComment = node.kind !== SyntaxKind.SourceFile ? true :
statementOffset === 0 || nodeIsSynthesized((node as SourceFile).statements[statementOffset - 1]);
if (emitBodyWithDetachedComments && shouldEmitDetachedComment) {
emitBodyWithDetachedComments(node, elements, emitCallback);
}
else {
emitCallback(node);
}
}
function emitHelpersIndirect(node: Node) {
if (onEmitHelpers) {
onEmitHelpers(node, writeLines);
@@ -1652,7 +1637,12 @@ namespace ts {
? emitBlockFunctionBodyOnSingleLine
: emitBlockFunctionBodyWorker;
emitBodyIndirect(body, body.statements, emitBlockFunctionBody);
if (emitBodyWithDetachedComments) {
emitBodyWithDetachedComments(body, body.statements, emitBlockFunctionBody);
}
else {
emitBlockFunctionBody(body);
}
decreaseIndent();
writeToken(SyntaxKind.CloseBraceToken, body.statements.end, body);
@@ -2061,14 +2051,28 @@ namespace ts {
function emitSourceFile(node: SourceFile) {
writeLine();
emitBodyIndirect(node, node.statements, emitSourceFileWorker);
// If the node is a sourceFile and it has prologueDirective (statmentOffSet is not zero) that is synthesize
// We will need to emit detached comment here because emitPrologueDirective will not emit comments of such prologue directive
const statements = node.statements;
// Emit detached comment if there iare no prologue directives or this is a synthesized prologue directives.
// The synthesized node will have no leading comment so some comments may be missed.
const shouldEmitDetachedComment = statements.length === 0 ||
!isPrologueDirective(statements[0]) ||
(isPrologueDirective(statements[0]) && nodeIsSynthesized(statements[0]));
if (emitBodyWithDetachedComments && shouldEmitDetachedComment) {
emitBodyWithDetachedComments(node, statements, emitSourceFileWorker);
}
else {
emitSourceFileWorker(node);
}
}
function emitSourceFileWorker(node: SourceFile) {
const statements = node.statements;
pushNameGenerationScope();
emitHelpersIndirect(node);
emitList(node, statements, ListFormat.MultiLine, statementOffset);
const index = findIndex(statements, statement => !isPrologueDirective(statement));
emitList(node, statements, ListFormat.MultiLine, index === -1 ? statements.length : index);
popNameGenerationScope();
}
@@ -2122,40 +2126,27 @@ namespace ts {
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 encountered shebang
if (emitShebangInSourceFile(sourceFile)) {
break;
}
}
}
function emitShebangInSourceFile(sourceFile: SourceFile): boolean {
const shebang = getShebang(sourceFile.text);
const shebang = getShebang(sourceFileOrBundle.text);
if (shebang) {
write(shebang);
writeLine();
return true;
}
}
else {
for (const sourceFile of sourceFileOrBundle.sourceFiles) {
// Emit only the first encountered shebang
if (emitShebangIfNeeded(sourceFile)) {
break;
}
}
}
}
//
// Helpers
//
function getIndexOfFirstNonPrologueDirectives(statements: Statement[]): number {
for (let i = 0; i < statements.length; i++) {
if (!isPrologueDirective(statements[i])) {
return i;
}
}
return statements.length;
}
function emitModifiers(node: Node, modifiers: NodeArray<Modifier>) {
if (modifiers && modifiers.length) {
emitList(node, modifiers, ListFormat.Modifiers);