@@ -268,33 +268,34 @@ namespace ts {
268268 return fs . readFileSync ( "/.src/index.d.ts" ) . toString ( ) ;
269269 }
270270
271+ function addSyntheticComment ( nodeFilter : ( node : Node ) => boolean ) {
272+ return ( context : TransformationContext ) => {
273+ return ( sourceFile : SourceFile ) : SourceFile => {
274+ return visitNode ( sourceFile , rootTransform , isSourceFile ) ;
275+ } ;
276+ function rootTransform < T extends Node > ( node : T ) : VisitResult < T > {
277+ if ( nodeFilter ( node ) ) {
278+ setEmitFlags ( node , EmitFlags . NoLeadingComments ) ;
279+ setSyntheticLeadingComments ( node , [ { kind : SyntaxKind . MultiLineCommentTrivia , text : "comment" , pos : - 1 , end : - 1 , hasTrailingNewLine : true } ] ) ;
280+ }
281+ return visitEachChild ( node , rootTransform , context ) ;
282+ }
283+ } ;
284+ }
285+
271286 // https://github.com/Microsoft/TypeScript/issues/24096
272287 testBaseline ( "transformAddCommentToArrowReturnValue" , ( ) => {
273288 return transpileModule ( `const foo = () =>
274289 void 0
275290` , {
276291 transformers : {
277- before : [ addSyntheticComment ] ,
292+ before : [ addSyntheticComment ( isVoidExpression ) ] ,
278293 } ,
279294 compilerOptions : {
280295 target : ScriptTarget . ES5 ,
281296 newLine : NewLineKind . CarriageReturnLineFeed ,
282297 }
283298 } ) . outputText ;
284-
285- function addSyntheticComment ( context : TransformationContext ) {
286- return ( sourceFile : SourceFile ) : SourceFile => {
287- return visitNode ( sourceFile , rootTransform , isSourceFile ) ;
288- } ;
289- function rootTransform < T extends Node > ( node : T ) : VisitResult < T > {
290- if ( isVoidExpression ( node ) ) {
291- setEmitFlags ( node , EmitFlags . NoLeadingComments ) ;
292- setSyntheticLeadingComments ( node , [ { kind : SyntaxKind . SingleLineCommentTrivia , text : "// comment!" , pos : - 1 , end : - 1 , hasTrailingNewLine : true } ] ) ;
293- return node ;
294- }
295- return visitEachChild ( node , rootTransform , context ) ;
296- }
297- }
298299 } ) ;
299300
300301 // https://github.com/Microsoft/TypeScript/issues/17594
@@ -304,27 +305,13 @@ const exportedSeparately = 2;
304305export {exportedSeparately};
305306` , {
306307 transformers : {
307- before : [ addSyntheticComment ] ,
308+ before : [ addSyntheticComment ( isVariableStatement ) ] ,
308309 } ,
309310 compilerOptions : {
310311 target : ScriptTarget . ES5 ,
311312 newLine : NewLineKind . CarriageReturnLineFeed ,
312313 }
313314 } ) . outputText ;
314-
315- function addSyntheticComment ( context : TransformationContext ) {
316- return ( sourceFile : SourceFile ) : SourceFile => {
317- return visitNode ( sourceFile , rootTransform , isSourceFile ) ;
318- } ;
319- function rootTransform < T extends Node > ( node : T ) : VisitResult < T > {
320- if ( isVariableStatement ( node ) ) {
321- setEmitFlags ( node , EmitFlags . NoLeadingComments ) ;
322- setSyntheticLeadingComments ( node , [ { kind : SyntaxKind . MultiLineCommentTrivia , text : "* @type {number} " , pos : - 1 , end : - 1 , hasTrailingNewLine : true } ] ) ;
323- return node ;
324- }
325- return visitEachChild ( node , rootTransform , context ) ;
326- }
327- }
328315 } ) ;
329316
330317 // https://github.com/Microsoft/TypeScript/issues/17594
@@ -339,26 +326,14 @@ export * from 'somewhere';
339326export {Value};
340327` , {
341328 transformers : {
342- before : [ addSyntheticComment ] ,
329+ before : [ addSyntheticComment ( n => isImportDeclaration ( n ) || isExportDeclaration ( n ) || isImportSpecifier ( n ) || isExportSpecifier ( n ) ) ] ,
343330 } ,
344331 compilerOptions : {
345332 target : ScriptTarget . ES5 ,
346333 newLine : NewLineKind . CarriageReturnLineFeed ,
347334 }
348335 } ) . outputText ;
349-
350- function addSyntheticComment ( context : TransformationContext ) {
351- return ( sourceFile : SourceFile ) : SourceFile => {
352- return visitNode ( sourceFile , rootTransform , isSourceFile ) ;
353- } ;
354- function rootTransform < T extends Node > ( node : T ) : VisitResult < T > {
355- if ( isImportDeclaration ( node ) || isExportDeclaration ( node ) || isImportSpecifier ( node ) || isExportSpecifier ( node ) ) {
356- setEmitFlags ( node , EmitFlags . NoLeadingComments ) ;
357- setSyntheticLeadingComments ( node , [ { kind : SyntaxKind . MultiLineCommentTrivia , text : `comment!` , pos : - 1 , end : - 1 , hasTrailingNewLine : true } ] ) ;
358- }
359- return visitEachChild ( node , rootTransform , context ) ;
360- }
361- }
362- } ) ; } ) ;
336+ } ) ;
337+ } ) ;
363338}
364339
0 commit comments