|
3912 | 3912 | export type Transformer = (context: TransformationContext) => (node: SourceFile) => SourceFile;
|
3913 | 3913 |
|
3914 | 3914 | 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 | + */ |
3915 | 3928 | printNode(hint: EmitHint, node: Node, sourceFile: SourceFile): string;
|
| 3929 | + /** |
| 3930 | + * Prints a source file as-is, without any emit transformations. |
| 3931 | + */ |
3916 | 3932 | printFile(sourceFile: SourceFile): string;
|
| 3933 | + /** |
| 3934 | + * Prints a bundle of source files as-is, without any emit transformations. |
| 3935 | + */ |
3917 | 3936 | printBundle(bundle: Bundle): string;
|
3918 | 3937 | /*@internal*/ writeNode(hint: EmitHint, node: Node, sourceFile: SourceFile, writer: EmitTextWriter): void;
|
3919 | 3938 | /*@internal*/ writeFile(sourceFile: SourceFile, writer: EmitTextWriter): void;
|
3920 | 3939 | /*@internal*/ writeBundle(bundle: Bundle, writer: EmitTextWriter): void;
|
3921 | 3940 | }
|
3922 | 3941 |
|
3923 | 3942 | 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; |
3927 | 3987 | /*@internal*/ onEmitSourceMapOfNode?: (hint: EmitHint, node: Node, emitCallback: (hint: EmitHint, node: Node) => void) => void;
|
3928 | 3988 | /*@internal*/ onEmitSourceMapOfToken?: (node: Node, token: SyntaxKind, pos: number, emitCallback: (token: SyntaxKind, pos: number) => number) => number;
|
3929 | 3989 | /*@internal*/ onEmitSourceMapOfPosition?: (pos: number) => void;
|
|
0 commit comments