@@ -268,33 +268,34 @@ namespace ts {
268
268
return fs . readFileSync ( "/.src/index.d.ts" ) . toString ( ) ;
269
269
}
270
270
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
+
271
286
// https://github.com/Microsoft/TypeScript/issues/24096
272
287
testBaseline ( "transformAddCommentToArrowReturnValue" , ( ) => {
273
288
return transpileModule ( `const foo = () =>
274
289
void 0
275
290
` , {
276
291
transformers : {
277
- before : [ addSyntheticComment ] ,
292
+ before : [ addSyntheticComment ( isVoidExpression ) ] ,
278
293
} ,
279
294
compilerOptions : {
280
295
target : ScriptTarget . ES5 ,
281
296
newLine : NewLineKind . CarriageReturnLineFeed ,
282
297
}
283
298
} ) . 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
- }
298
299
} ) ;
299
300
300
301
// https://github.com/Microsoft/TypeScript/issues/17594
@@ -304,27 +305,13 @@ const exportedSeparately = 2;
304
305
export {exportedSeparately};
305
306
` , {
306
307
transformers : {
307
- before : [ addSyntheticComment ] ,
308
+ before : [ addSyntheticComment ( isVariableStatement ) ] ,
308
309
} ,
309
310
compilerOptions : {
310
311
target : ScriptTarget . ES5 ,
311
312
newLine : NewLineKind . CarriageReturnLineFeed ,
312
313
}
313
314
} ) . 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
- }
328
315
} ) ;
329
316
330
317
// https://github.com/Microsoft/TypeScript/issues/17594
@@ -339,26 +326,14 @@ export * from 'somewhere';
339
326
export {Value};
340
327
` , {
341
328
transformers : {
342
- before : [ addSyntheticComment ] ,
329
+ before : [ addSyntheticComment ( n => isImportDeclaration ( n ) || isExportDeclaration ( n ) || isImportSpecifier ( n ) || isExportSpecifier ( n ) ) ] ,
343
330
} ,
344
331
compilerOptions : {
345
332
target : ScriptTarget . ES5 ,
346
333
newLine : NewLineKind . CarriageReturnLineFeed ,
347
334
}
348
335
} ) . 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
+ } ) ;
363
338
}
364
339
0 commit comments