11import { EmitFlags } from "../../enums/emitFlags.js" ;
2- import type {
3- Node ,
4- SyntaxKind ,
2+ import { SyntaxKind } from "../../enums/syntaxKind.js" ;
3+ import {
4+ getOriginalNode ,
5+ type Node ,
56} from "../ast/index.ts" ;
67import {
78 encodeNode ,
89 uint8ArrayToBase64 ,
910} from "./node/encoder.ts" ;
11+ import {
12+ getRemoteNodeLocation ,
13+ type RemoteSourceFile ,
14+ sourceFileDescriptor ,
15+ } from "./node/node.ts" ;
16+ import type {
17+ PrintOriginalNode ,
18+ SourceFileDescriptor ,
19+ } from "./proto.generated.ts" ;
1020
1121export interface PrintNodeOptions {
1222 preserveSourceNewlines ?: boolean | undefined ;
@@ -43,12 +53,38 @@ export interface EmitContext {
4353 addSyntheticTrailingComment ( node : Node , kind : CommentKind , text : string , hasTrailingNewLine : boolean , hasLeadingNewLine : boolean ) : Node ;
4454 setSyntheticTrailingComments < T extends Node > ( node : T , comments : SynthesizedComment [ ] ) : T ;
4555 getSyntheticTrailingComments ( node : Node ) : readonly SynthesizedComment [ ] | undefined ;
56+
57+ /**
58+ * Suppresses emission of the source comments before and after `node`. Comments inside the
59+ * node are still emitted. `NoLeadingComments` and `NoTrailingComments` hide comments at a
60+ * node's start and end, including those of descendants sharing that position, so the flags
61+ * are also added to first and last descendants until one with a source position is reached.
62+ */
63+ suppressLeadingAndTrailingComments < T extends Node > ( node : T ) : T ;
4664}
4765
4866/** @internal */
49- export function encodePrintNode ( node : Node , emitContext : EmitContext | undefined ) : { data : string ; emitNodes : Record < number , EmitNode > | undefined ; } {
67+ export function encodePrintNode ( node : Node , emitContext : EmitContext | undefined ) : {
68+ data : string ;
69+ emitNodes : Record < number , EmitNode > | undefined ;
70+ originalFiles : SourceFileDescriptor [ ] | undefined ;
71+ originalNodes : Record < number , PrintOriginalNode > | undefined ;
72+ } {
5073 let emitNodes : Record < number , EmitNode > | undefined ;
74+ let originalFiles : SourceFileDescriptor [ ] | undefined ;
75+ let originalNodes : Record < number , PrintOriginalNode > | undefined ;
76+ const fileIndices = new Map < RemoteSourceFile , number > ( ) ;
5177 const encoded = encodeNode ( node , ( node , index ) => {
78+ const original = getOriginalNode ( node ) ;
79+ const location = original !== node ? getRemoteNodeLocation ( original ) : undefined ;
80+ if ( location ) {
81+ let file = fileIndices . get ( location . sourceFile ) ;
82+ if ( file === undefined ) {
83+ file = ( originalFiles ??= [ ] ) . push ( sourceFileDescriptor ( location . sourceFile ) ) - 1 ;
84+ fileIndices . set ( location . sourceFile , file ) ;
85+ }
86+ ( originalNodes ??= { } ) [ index ] = { file, index : location . index } ;
87+ }
5288 const emitNode = emitContext ?. getEmitNode ( node ) ;
5389 if ( emitNode === undefined ) return ;
5490 ( emitNodes ??= { } ) [ index ] = {
@@ -57,7 +93,7 @@ export function encodePrintNode(node: Node, emitContext: EmitContext | undefined
5793 trailingComments : emitNode . trailingComments ?. map ( c => ( { ...c } ) ) ,
5894 } ;
5995 } ) ;
60- return { data : uint8ArrayToBase64 ( encoded ) , emitNodes } ;
96+ return { data : uint8ArrayToBase64 ( encoded ) , emitNodes, originalFiles , originalNodes } ;
6197}
6298
6399/** @internal */
@@ -108,5 +144,41 @@ export function createEmitContext(): EmitContext {
108144 getSyntheticTrailingComments ( node ) {
109145 return emitNodes . get ( node ) ?. trailingComments ;
110146 } ,
147+
148+ suppressLeadingAndTrailingComments ( node ) {
149+ for ( let n : Node | undefined = node ; n ; n = n . pos < 0 ? getFirstChild ( n ) : undefined ) {
150+ getOrCreateEmitNode ( n ) . emitFlags |= EmitFlags . NoLeadingComments ;
151+ }
152+ for ( let n : Node | undefined = node ; n ; n = n . end < 0 ? getLastChild ( n ) : undefined ) {
153+ getOrCreateEmitNode ( n ) . emitFlags |= EmitFlags . NoTrailingComments ;
154+ }
155+ return node ;
156+ } ,
111157 } ;
112158}
159+
160+ function getFirstChild ( node : Node ) : Node | undefined {
161+ return node . forEachChild ( child => child ) ;
162+ }
163+
164+ function getLastChild ( node : Node ) : Node | undefined {
165+ let lastChild : Node | undefined ;
166+ node . forEachChild (
167+ child => {
168+ if ( nodeIsPresent ( child ) ) lastChild = child ;
169+ } ,
170+ children => {
171+ for ( let i = children . length - 1 ; i >= 0 ; i -- ) {
172+ if ( nodeIsPresent ( children [ i ] ) ) {
173+ lastChild = children [ i ] ;
174+ break ;
175+ }
176+ }
177+ } ,
178+ ) ;
179+ return lastChild ;
180+ }
181+
182+ function nodeIsPresent ( node : Node ) : boolean {
183+ return node . pos !== node . end || node . pos < 0 || node . kind === SyntaxKind . EndOfFile ;
184+ }
0 commit comments