Skip to content

Commit 9845413

Browse files
committed
Added comments and a few Debug assertions
1 parent f02ce1f commit 9845413

File tree

2 files changed

+78
-3
lines changed

2 files changed

+78
-3
lines changed

src/compiler/emitter.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,21 @@ namespace ts {
236236
};
237237

238238
function printNode(hint: EmitHint, node: Node, sourceFile: SourceFile): string {
239+
switch (hint) {
240+
case EmitHint.SourceFile:
241+
Debug.assert(isSourceFile(node), "Expected a SourceFile node.");
242+
break;
243+
case EmitHint.IdentifierName:
244+
Debug.assert(isIdentifier(node), "Expected an Identifier node.");
245+
break;
246+
case EmitHint.Expression:
247+
Debug.assert(isExpression(node), "Expected an Expression node.");
248+
break;
249+
}
250+
switch (node.kind) {
251+
case SyntaxKind.SourceFile: return printFile(<SourceFile>node);
252+
case SyntaxKind.Bundle: return printBundle(<Bundle>node);
253+
}
239254
writeNode(hint, node, sourceFile, beginPrint());
240255
return endPrint();
241256
}

src/compiler/types.ts

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3912,18 +3912,78 @@
39123912
export type Transformer = (context: TransformationContext) => (node: SourceFile) => SourceFile;
39133913

39143914
export interface Printer {
3915+
/**
3916+
* Print a node and its subtree as-is, without any emit transformations.
3917+
* @param hint A value indicating the purpose of a node. This is primarily used to
3918+
* distinguish between an `Identifier` used in an expression position, versus an
3919+
* `Identifier` used as an `IdentifierName` as part of a declaration. For most nodes you
3920+
* should just pass `Unspecified`.
3921+
* @param node The node to print. The node and its subtree are printed as-is, without any
3922+
* emit transformations.
3923+
* @param sourceFile A source file that provides context for the node. The source text of
3924+
* the file is used to emit the original source content for literals and identifiers, while
3925+
* the identifiers of the source file are used when generating unique names to avoid
3926+
* collisions.
3927+
*/
39153928
printNode(hint: EmitHint, node: Node, sourceFile: SourceFile): string;
3929+
/**
3930+
* Prints a source file as-is, without any emit transformations.
3931+
*/
39163932
printFile(sourceFile: SourceFile): string;
3933+
/**
3934+
* Prints a bundle of source files as-is, without any emit transformations.
3935+
*/
39173936
printBundle(bundle: Bundle): string;
39183937
/*@internal*/ writeNode(hint: EmitHint, node: Node, sourceFile: SourceFile, writer: EmitTextWriter): void;
39193938
/*@internal*/ writeFile(sourceFile: SourceFile, writer: EmitTextWriter): void;
39203939
/*@internal*/ writeBundle(bundle: Bundle, writer: EmitTextWriter): void;
39213940
}
39223941

39233942
export interface PrintHandlers {
3924-
hasGlobalName?: (name: string) => boolean;
3925-
onEmitNode?: (hint: EmitHint, node: Node, emitCallback: (hint: EmitHint, node: Node) => void) => void;
3926-
onSubstituteNode?: (hint: EmitHint, node: Node, emitCallback: (hint: EmitHint, node: Node) => void) => void;
3943+
/**
3944+
* A hook used by the Printer when generating unique names to avoid collisions with
3945+
* globally defined names that exist outside of the current source file.
3946+
*/
3947+
hasGlobalName?(name: string): boolean;
3948+
/**
3949+
* A hook used by the Printer to provide notifications prior to emitting a node. A
3950+
* compatible implementation **must** invoke `emitCallback` with the provided `hint` and
3951+
* `node` values.
3952+
* @param hint A hint indicating the intended purpose of the node.
3953+
* @param node The node to emit.
3954+
* @param emitCallback A callback that, when invoked, will emit the node.
3955+
* @example
3956+
* ```ts
3957+
* var printer = createPrinter(printerOptions, {
3958+
* onEmitNode(hint, node, emitCallback) {
3959+
* // set up or track state prior to emitting the node...
3960+
* emitCallback(hint, node);
3961+
* // restore state after emitting the node...
3962+
* }
3963+
* });
3964+
* ```
3965+
*/
3966+
onEmitNode?(hint: EmitHint, node: Node, emitCallback: (hint: EmitHint, node: Node) => void): void;
3967+
/**
3968+
* A hook used by the Printer to perform just-in-time substitution of a node. This is
3969+
* primarily used by node transformations that need to substitute one node for another,
3970+
* such as replacing `myExportedVar` with `exports.myExportedVar`. A compatible
3971+
* implementation **must** invoke `emitCallback` eith the provided `hint` and either
3972+
* the provided `node`, or its substitute.
3973+
* @param hint A hint indicating the intended purpose of the node.
3974+
* @param node The node to emit.
3975+
* @param emitCallback A callback that, when invoked, will emit the node.
3976+
* @example
3977+
* ```ts
3978+
* var printer = createPrinter(printerOptions, {
3979+
* onSubstituteNode(hint, node, emitCallback) {
3980+
* // perform substitution if necessary...
3981+
* emitCallback(hint, node);
3982+
* }
3983+
* });
3984+
* ```
3985+
*/
3986+
onSubstituteNode?(hint: EmitHint, node: Node, emitCallback: (hint: EmitHint, node: Node) => void): void;
39273987
/*@internal*/ onEmitSourceMapOfNode?: (hint: EmitHint, node: Node, emitCallback: (hint: EmitHint, node: Node) => void) => void;
39283988
/*@internal*/ onEmitSourceMapOfToken?: (node: Node, token: SyntaxKind, pos: number, emitCallback: (token: SyntaxKind, pos: number) => number) => number;
39293989
/*@internal*/ onEmitSourceMapOfPosition?: (pos: number) => void;

0 commit comments

Comments
 (0)