1- import { type AstTypes , Walker , parseScript , serializeScript , stripAst } from '../index.ts' ;
1+ import {
2+ type AdditionalCommentMap ,
3+ type AstTypes ,
4+ Walker ,
5+ parseScript ,
6+ serializeScript ,
7+ stripAst
8+ } from '../index.ts' ;
29import decircular from 'decircular' ;
310import dedent from 'dedent' ;
411
5- export function addJsDocTypeComment ( node : AstTypes . Node , options : { type : string } ) : void {
12+ export function addJsDocTypeComment (
13+ node : AstTypes . Node ,
14+ additionalComments : AdditionalCommentMap ,
15+ options : { type : string }
16+ ) : void {
617 const comment : AstTypes . Comment = {
718 type : 'Block' ,
819 value : `* @type {${ options . type } } `
920 } ;
1021
11- addComment ( node , comment ) ;
22+ addComment ( node , additionalComments , comment ) ;
1223}
1324
1425export function addJsDocComment (
1526 node : AstTypes . Node ,
27+ additionalComments : AdditionalCommentMap ,
1628 options : { params : Record < string , string > }
1729) : void {
1830 const commentLines : string [ ] = [ ] ;
@@ -25,16 +37,23 @@ export function addJsDocComment(
2537 value : `*\n * ${ commentLines . join ( '\n * ' ) } \n `
2638 } ;
2739
28- addComment ( node , comment ) ;
40+ addComment ( node , additionalComments , comment ) ;
2941}
3042
31- function addComment ( node : AstTypes . Node , comment : AstTypes . Comment ) {
32- node . leadingComments ??= [ ] ;
33-
34- const found = node . leadingComments . find (
35- ( item ) => item . type === 'Block' && item . value === comment . value
36- ) ;
37- if ( ! found ) node . leadingComments . push ( comment ) ;
43+ function addComment (
44+ node : AstTypes . Node ,
45+ additionalComments : AdditionalCommentMap ,
46+ comment : AstTypes . Comment
47+ ) {
48+ const found = additionalComments
49+ . get ( node )
50+ ?. find ( ( item ) => item . type === 'Block' && item . value === comment . value ) ;
51+
52+ if ( ! found ) {
53+ const comments = additionalComments . get ( node ) ?? [ ] ;
54+ comments . push ( { ...comment , position : 'leading' } ) ;
55+ additionalComments . set ( node , comments ) ;
56+ }
3857}
3958
4059export function typeAnnotate (
0 commit comments