Skip to content

Commit ebb4764

Browse files
committed
Switch from onBefore/onAfterEmitNode to onEmitNode
1 parent b1d8828 commit ebb4764

File tree

3 files changed

+55
-38
lines changed

3 files changed

+55
-38
lines changed

src/compiler/printer.ts

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,7 @@ const _super = (function (geti, seti) {
151151
let isEmitNotificationEnabled: (node: Node) => boolean;
152152
let expressionSubstitution: (node: Expression) => Expression;
153153
let identifierSubstitution: (node: Identifier) => Identifier;
154-
let onBeforeEmitNode: (node: Node) => void;
155-
let onAfterEmitNode: (node: Node) => void;
154+
let onEmitNode: (node: Node, emit: (node: Node) => void) => void;
156155
let nodeToGeneratedName: string[];
157156
let generatedNameSet: Map<string>;
158157
let tempFlags: TempFlags;
@@ -213,8 +212,7 @@ const _super = (function (geti, seti) {
213212
isEmitNotificationEnabled = undefined;
214213
expressionSubstitution = undefined;
215214
identifierSubstitution = undefined;
216-
onBeforeEmitNode = undefined;
217-
onAfterEmitNode = undefined;
215+
onEmitNode = undefined;
218216
tempFlags = TempFlags.Auto;
219217
currentSourceFile = undefined;
220218
currentText = undefined;
@@ -234,8 +232,7 @@ const _super = (function (geti, seti) {
234232
isEmitNotificationEnabled = context.isEmitNotificationEnabled;
235233
expressionSubstitution = context.expressionSubstitution;
236234
identifierSubstitution = context.identifierSubstitution;
237-
onBeforeEmitNode = context.onBeforeEmitNode;
238-
onAfterEmitNode = context.onAfterEmitNode;
235+
onEmitNode = context.onEmitNode;
239236
return printSourceFile;
240237
}
241238

@@ -249,46 +246,62 @@ const _super = (function (geti, seti) {
249246
return node;
250247
}
251248

249+
/**
250+
* Emits a node.
251+
*/
252252
function emit(node: Node) {
253-
emitWithWorker(node, emitWorker);
253+
emitNodeWithNotificationOption(node, emitWithoutNotificationOption);
254+
}
255+
256+
/**
257+
* Emits a node without calling onEmitNode.
258+
* NOTE: Do not call this method directly.
259+
*/
260+
function emitWithoutNotificationOption(node: Node) {
261+
emitNodeWithWorker(node, emitWorker);
254262
}
255263

264+
/**
265+
* Emits an expression node.
266+
*/
256267
function emitExpression(node: Expression) {
257-
emitWithWorker(node, emitExpressionWorker);
268+
emitNodeWithNotificationOption(node, emitExpressionWithoutNotificationOption);
269+
}
270+
271+
/**
272+
* Emits an expression without calling onEmitNode.
273+
* NOTE: Do not call this method directly.
274+
*/
275+
function emitExpressionWithoutNotificationOption(node: Expression) {
276+
emitNodeWithWorker(node, emitExpressionWorker);
258277
}
259278

260-
function emitWithWorker(node: Node, emitWorker: (node: Node) => void) {
279+
/**
280+
* Emits a node with emit notification if available.
281+
*/
282+
function emitNodeWithNotificationOption(node: Node, emit: (node: Node) => void) {
261283
if (node) {
262-
const adviseOnEmit = isEmitNotificationEnabled(node);
263-
if (adviseOnEmit && onBeforeEmitNode) {
264-
onBeforeEmitNode(node);
284+
if (isEmitNotificationEnabled(node)) {
285+
onEmitNode(node, emit);
286+
}
287+
else {
288+
emit(node);
265289
}
290+
}
291+
}
266292

293+
function emitNodeWithWorker(node: Node, emitWorker: (node: Node) => void) {
294+
if (node) {
267295
const leadingComments = getLeadingComments(node, getNotEmittedParent);
268296
const trailingComments = getTrailingComments(node, getNotEmittedParent);
269297
emitLeadingComments(node, leadingComments);
270298
emitStart(node);
271299
emitWorker(node);
272300
emitEnd(node);
273301
emitTrailingComments(node, trailingComments);
274-
275-
if (adviseOnEmit && onAfterEmitNode) {
276-
onAfterEmitNode(node);
277-
}
278302
}
279303
}
280304

281-
function getNotEmittedParent(node: Node): Node {
282-
if (getNodeEmitFlags(node) & NodeEmitFlags.EmitCommentsOfNotEmittedParent) {
283-
const parent = getOriginalNode(node).parent;
284-
if (getNodeEmitFlags(parent) & NodeEmitFlags.IsNotEmittedNode) {
285-
return parent;
286-
}
287-
}
288-
289-
return undefined;
290-
}
291-
292305
function emitWorker(node: Node): void {
293306
const kind = node.kind;
294307
switch (kind) {
@@ -2361,6 +2374,17 @@ const _super = (function (geti, seti) {
23612374
&& rangeEndIsOnSameLineAsRangeStart(block, block);
23622375
}
23632376

2377+
function getNotEmittedParent(node: Node): Node {
2378+
if (getNodeEmitFlags(node) & NodeEmitFlags.EmitCommentsOfNotEmittedParent) {
2379+
const parent = getOriginalNode(node).parent;
2380+
if (getNodeEmitFlags(parent) & NodeEmitFlags.IsNotEmittedNode) {
2381+
return parent;
2382+
}
2383+
}
2384+
2385+
return undefined;
2386+
}
2387+
23642388
function isUniqueName(name: string): boolean {
23652389
return !resolver.hasGlobalName(name) &&
23662390
!hasProperty(currentFileIdentifiers, name) &&

src/compiler/transformer.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ namespace ts {
4646
expressionSubstitution: node => node,
4747
enableExpressionSubstitution,
4848
isExpressionSubstitutionEnabled,
49-
onBeforeEmitNode: node => { },
50-
onAfterEmitNode: node => { },
49+
onEmitNode: (node, emit) => emit(node),
5150
enableEmitNotification,
5251
isEmitNotificationEnabled,
5352
};

src/compiler/types.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2865,16 +2865,10 @@ namespace ts {
28652865
isEmitNotificationEnabled(node: Node): boolean;
28662866

28672867
/**
2868-
* Hook used to notify transformers immediately before the pretty printer
2869-
* emits a node.
2868+
* Hook used to allow transformers to capture state before or after
2869+
* the printer emits a node.
28702870
*/
2871-
onBeforeEmitNode?: (node: Node) => void;
2872-
2873-
/**
2874-
* Hook used to notify transformers immediately after the pretty printer
2875-
* emits a node.
2876-
*/
2877-
onAfterEmitNode?: (node: Node) => void;
2871+
onEmitNode?: (node: Node, emit: (node: Node) => void) => void;
28782872
}
28792873

28802874
/* @internal */

0 commit comments

Comments
 (0)