Merge branch 'transforms-printer' into transforms-transformer-ts

This commit is contained in:
Ron Buckton 2016-03-02 15:20:02 -08:00
commit e66e51d809
3 changed files with 57 additions and 40 deletions

View File

@ -151,8 +151,7 @@ const _super = (function (geti, seti) {
let isEmitNotificationEnabled: (node: Node) => boolean;
let expressionSubstitution: (node: Expression) => Expression;
let identifierSubstitution: (node: Identifier) => Identifier;
let onBeforeEmitNode: (node: Node) => void;
let onAfterEmitNode: (node: Node) => void;
let onEmitNode: (node: Node, emit: (node: Node) => void) => void;
let nodeToGeneratedName: string[];
let generatedNameSet: Map<string>;
let tempFlags: TempFlags;
@ -213,8 +212,7 @@ const _super = (function (geti, seti) {
isEmitNotificationEnabled = undefined;
expressionSubstitution = undefined;
identifierSubstitution = undefined;
onBeforeEmitNode = undefined;
onAfterEmitNode = undefined;
onEmitNode = undefined;
tempFlags = TempFlags.Auto;
currentSourceFile = undefined;
currentText = undefined;
@ -234,8 +232,7 @@ const _super = (function (geti, seti) {
isEmitNotificationEnabled = context.isEmitNotificationEnabled;
expressionSubstitution = context.expressionSubstitution;
identifierSubstitution = context.identifierSubstitution;
onBeforeEmitNode = context.onBeforeEmitNode;
onAfterEmitNode = context.onAfterEmitNode;
onEmitNode = context.onEmitNode;
return printSourceFile;
}
@ -249,21 +246,52 @@ const _super = (function (geti, seti) {
return node;
}
/**
* Emits a node.
*/
function emit(node: Node) {
emitWithWorker(node, emitWorker);
emitNodeWithNotificationOption(node, emitWithoutNotificationOption);
}
/**
* Emits a node without calling onEmitNode.
* NOTE: Do not call this method directly.
*/
function emitWithoutNotificationOption(node: Node) {
emitNodeWithWorker(node, emitWorker);
}
/**
* Emits an expression node.
*/
function emitExpression(node: Expression) {
emitWithWorker(node, emitExpressionWorker);
emitNodeWithNotificationOption(node, emitExpressionWithoutNotificationOption);
}
function emitWithWorker(node: Node, emitWorker: (node: Node) => void) {
if (node) {
const adviseOnEmit = isEmitNotificationEnabled(node);
if (adviseOnEmit && onBeforeEmitNode) {
onBeforeEmitNode(node);
}
/**
* Emits an expression without calling onEmitNode.
* NOTE: Do not call this method directly.
*/
function emitExpressionWithoutNotificationOption(node: Expression) {
emitNodeWithWorker(node, emitExpressionWorker);
}
/**
* Emits a node with emit notification if available.
*/
function emitNodeWithNotificationOption(node: Node, emit: (node: Node) => void) {
if (node) {
if (isEmitNotificationEnabled(node)) {
onEmitNode(node, emit);
}
else {
emit(node);
}
}
}
function emitNodeWithWorker(node: Node, emitWorker: (node: Node) => void) {
if (node) {
const leadingComments = getLeadingComments(node, getNotEmittedParent);
const trailingComments = getTrailingComments(node, getNotEmittedParent);
emitLeadingComments(node, leadingComments);
@ -271,24 +299,9 @@ const _super = (function (geti, seti) {
emitWorker(node);
emitEnd(node);
emitTrailingComments(node, trailingComments);
if (adviseOnEmit && onAfterEmitNode) {
onAfterEmitNode(node);
}
}
}
function getNotEmittedParent(node: Node): Node {
if (getNodeEmitFlags(node) & NodeEmitFlags.EmitCommentsOfNotEmittedParent) {
const parent = getOriginalNode(node).parent;
if (getNodeEmitFlags(parent) & NodeEmitFlags.IsNotEmittedNode) {
return parent;
}
}
return undefined;
}
function emitWorker(node: Node): void {
const kind = node.kind;
switch (kind) {
@ -2361,6 +2374,17 @@ const _super = (function (geti, seti) {
&& rangeEndIsOnSameLineAsRangeStart(block, block);
}
function getNotEmittedParent(node: Node): Node {
if (getNodeEmitFlags(node) & NodeEmitFlags.EmitCommentsOfNotEmittedParent) {
const parent = getOriginalNode(node).parent;
if (getNodeEmitFlags(parent) & NodeEmitFlags.IsNotEmittedNode) {
return parent;
}
}
return undefined;
}
function isUniqueName(name: string): boolean {
return !resolver.hasGlobalName(name) &&
!hasProperty(currentFileIdentifiers, name) &&

View File

@ -78,8 +78,7 @@ namespace ts {
expressionSubstitution: node => node,
enableExpressionSubstitution,
isExpressionSubstitutionEnabled,
onBeforeEmitNode: node => { },
onAfterEmitNode: node => { },
onEmitNode: (node, emit) => emit(node),
enableEmitNotification,
isEmitNotificationEnabled,
};

View File

@ -2865,16 +2865,10 @@ namespace ts {
isEmitNotificationEnabled(node: Node): boolean;
/**
* Hook used to notify transformers immediately before the pretty printer
* emits a node.
* Hook used to allow transformers to capture state before or after
* the printer emits a node.
*/
onBeforeEmitNode?: (node: Node) => void;
/**
* Hook used to notify transformers immediately after the pretty printer
* emits a node.
*/
onAfterEmitNode?: (node: Node) => void;
onEmitNode?: (node: Node, emit: (node: Node) => void) => void;
}
/* @internal */