@@ -13,6 +13,7 @@ import { lexLineComments } from "./lineLexer.js";
13
13
import { parseComment } from "./parser.js" ;
14
14
import type { FileRegistry } from "../../models/FileRegistry.js" ;
15
15
import { assertNever , i18n , type Logger } from "#utils" ;
16
+ import type { Context } from "../context.js" ;
16
17
17
18
export interface CommentParserConfig {
18
19
blockTags : Set < string > ;
@@ -24,6 +25,22 @@ export interface CommentParserConfig {
24
25
commentStyle : CommentStyle ;
25
26
}
26
27
28
+ export interface CommentContext {
29
+ config : CommentParserConfig ;
30
+ logger : Logger ;
31
+ checker : ts . TypeChecker ;
32
+ files : FileRegistry ;
33
+ createSymbolId : Context [ "createSymbolId" ] ;
34
+ }
35
+
36
+ export interface CommentContextOptionalChecker {
37
+ config : CommentParserConfig ;
38
+ logger : Logger ;
39
+ checker ?: ts . TypeChecker | undefined ;
40
+ files : FileRegistry ;
41
+ createSymbolId : Context [ "createSymbolId" ] ;
42
+ }
43
+
27
44
const jsDocCommentKinds = [
28
45
ts . SyntaxKind . JSDocPropertyTag ,
29
46
ts . SyntaxKind . JSDocCallbackTag ,
@@ -45,10 +62,7 @@ export function clearCommentCache() {
45
62
46
63
function getCommentWithCache (
47
64
discovered : DiscoveredComment | undefined ,
48
- config : CommentParserConfig ,
49
- logger : Logger ,
50
- checker : ts . TypeChecker | undefined ,
51
- files : FileRegistry ,
65
+ context : CommentContextOptionalChecker ,
52
66
) {
53
67
if ( ! discovered ) return ;
54
68
@@ -68,22 +82,19 @@ function getCommentWithCache(
68
82
file . text ,
69
83
ranges [ 0 ] . pos ,
70
84
ranges [ 0 ] . end ,
85
+ context . createSymbolId ,
71
86
jsDoc ,
72
- checker ,
87
+ context . checker ,
73
88
) ,
74
- config ,
75
89
file ,
76
- logger ,
77
- files ,
90
+ context ,
78
91
) ;
79
92
break ;
80
93
case ts . SyntaxKind . SingleLineCommentTrivia :
81
94
comment = parseComment (
82
95
lexLineComments ( file . text , ranges ) ,
83
- config ,
84
96
file ,
85
- logger ,
86
- files ,
97
+ context ,
87
98
) ;
88
99
break ;
89
100
default :
@@ -100,18 +111,15 @@ function getCommentWithCache(
100
111
101
112
function getCommentImpl (
102
113
commentSource : DiscoveredComment | undefined ,
103
- config : CommentParserConfig ,
104
- logger : Logger ,
105
114
moduleComment : boolean ,
106
- checker : ts . TypeChecker | undefined ,
107
- files : FileRegistry ,
115
+ context : CommentContext ,
108
116
) {
109
117
const comment = getCommentWithCache (
110
118
commentSource ,
111
- config ,
112
- logger ,
113
- config . useTsLinkResolution ? checker : undefined ,
114
- files ,
119
+ {
120
+ ... context ,
121
+ checker : context . config . useTsLinkResolution ? context . checker : undefined ,
122
+ } ,
115
123
) ;
116
124
117
125
if ( comment ?. getTag ( "@import" ) || comment ?. getTag ( "@license" ) ) {
@@ -145,10 +153,7 @@ function getCommentImpl(
145
153
export function getComment (
146
154
symbol : ts . Symbol ,
147
155
kind : ReflectionKind ,
148
- config : CommentParserConfig ,
149
- logger : Logger ,
150
- checker : ts . TypeChecker ,
151
- files : FileRegistry ,
156
+ context : CommentContext ,
152
157
) : Comment | undefined {
153
158
const declarations = symbol . declarations || [ ] ;
154
159
@@ -158,16 +163,13 @@ export function getComment(
158
163
) {
159
164
return getJsDocComment (
160
165
declarations [ 0 ] as ts . JSDocPropertyLikeTag ,
161
- config ,
162
- logger ,
163
- checker ,
164
- files ,
166
+ context ,
165
167
) ;
166
168
}
167
169
168
170
const sf = declarations . find ( ts . isSourceFile ) ;
169
171
if ( sf ) {
170
- return getFileComment ( sf , config , logger , checker , files ) ;
172
+ return getFileComment ( sf , context ) ;
171
173
}
172
174
173
175
const isModule = declarations . some ( ( decl ) => {
@@ -181,25 +183,19 @@ export function getComment(
181
183
discoverComment (
182
184
symbol ,
183
185
kind ,
184
- logger ,
185
- config . commentStyle ,
186
- checker ,
187
- ! config . suppressCommentWarningsInDeclarationFiles ,
186
+ context . logger ,
187
+ context . config . commentStyle ,
188
+ context . checker ,
189
+ ! context . config . suppressCommentWarningsInDeclarationFiles ,
188
190
) ,
189
- config ,
190
- logger ,
191
191
isModule ,
192
- checker ,
193
- files ,
192
+ context ,
194
193
) ;
195
194
196
195
if ( ! comment && kind === ReflectionKind . Property ) {
197
196
return getConstructorParamPropertyComment (
198
197
symbol ,
199
- config ,
200
- logger ,
201
- checker ,
202
- files ,
198
+ context ,
203
199
) ;
204
200
}
205
201
@@ -209,40 +205,28 @@ export function getComment(
209
205
export function getNodeComment (
210
206
node : ts . Node ,
211
207
moduleComment : boolean ,
212
- config : CommentParserConfig ,
213
- logger : Logger ,
214
- checker : ts . TypeChecker | undefined ,
215
- files : FileRegistry ,
208
+ context : CommentContext ,
216
209
) {
217
210
return getCommentImpl (
218
- discoverNodeComment ( node , config . commentStyle ) ,
219
- config ,
220
- logger ,
211
+ discoverNodeComment ( node , context . config . commentStyle ) ,
221
212
moduleComment ,
222
- checker ,
223
- files ,
213
+ context ,
224
214
) ;
225
215
}
226
216
227
217
export function getFileComment (
228
218
file : ts . SourceFile ,
229
- config : CommentParserConfig ,
230
- logger : Logger ,
231
- checker : ts . TypeChecker | undefined ,
232
- files : FileRegistry ,
219
+ context : CommentContext ,
233
220
) : Comment | undefined {
234
221
for (
235
222
const commentSource of discoverFileComments (
236
223
file ,
237
- config . commentStyle ,
224
+ context . config . commentStyle ,
238
225
)
239
226
) {
240
227
const comment = getCommentWithCache (
241
228
commentSource ,
242
- config ,
243
- logger ,
244
- config . useTsLinkResolution ? checker : undefined ,
245
- files ,
229
+ context ,
246
230
) ;
247
231
248
232
if ( comment ?. getTag ( "@license" ) || comment ?. getTag ( "@import" ) ) {
@@ -261,16 +245,13 @@ export function getFileComment(
261
245
262
246
function getConstructorParamPropertyComment (
263
247
symbol : ts . Symbol ,
264
- config : CommentParserConfig ,
265
- logger : Logger ,
266
- checker : ts . TypeChecker ,
267
- files : FileRegistry ,
248
+ context : CommentContext ,
268
249
) : Comment | undefined {
269
250
const decl = symbol . declarations ?. find ( ts . isParameter ) ;
270
251
if ( ! decl ) return ;
271
252
272
253
const ctor = decl . parent ;
273
- const comment = getSignatureComment ( ctor , config , logger , checker , files ) ;
254
+ const comment = getSignatureComment ( ctor , context ) ;
274
255
275
256
const paramTag = comment ?. getIdentifiedTag ( symbol . name , "@param" ) ;
276
257
if ( paramTag ) {
@@ -282,18 +263,12 @@ function getConstructorParamPropertyComment(
282
263
283
264
export function getSignatureComment (
284
265
declaration : ts . SignatureDeclaration | ts . JSDocSignature ,
285
- config : CommentParserConfig ,
286
- logger : Logger ,
287
- checker : ts . TypeChecker ,
288
- files : FileRegistry ,
266
+ context : CommentContext ,
289
267
) : Comment | undefined {
290
268
return getCommentImpl (
291
- discoverSignatureComment ( declaration , checker , config . commentStyle ) ,
292
- config ,
293
- logger ,
269
+ discoverSignatureComment ( declaration , context . checker , context . config . commentStyle ) ,
294
270
false ,
295
- checker ,
296
- files ,
271
+ context ,
297
272
) ;
298
273
}
299
274
@@ -304,10 +279,7 @@ export function getJsDocComment(
304
279
| ts . JSDocTypedefTag
305
280
| ts . JSDocTemplateTag
306
281
| ts . JSDocEnumTag ,
307
- config : CommentParserConfig ,
308
- logger : Logger ,
309
- checker : ts . TypeChecker | undefined ,
310
- files : FileRegistry ,
282
+ context : CommentContext ,
311
283
) : Comment | undefined {
312
284
const file = declaration . getSourceFile ( ) ;
313
285
@@ -331,10 +303,7 @@ export function getJsDocComment(
331
303
jsDoc : parent ,
332
304
inheritedFromParentDeclaration : false ,
333
305
} ,
334
- config ,
335
- logger ,
336
- config . useTsLinkResolution ? checker : undefined ,
337
- files ,
306
+ context ,
338
307
) ! ;
339
308
340
309
// And pull out the tag we actually care about.
@@ -352,7 +321,7 @@ export function getJsDocComment(
352
321
// We could just put the same comment on everything, but due to how comment parsing works,
353
322
// we'd have to search for any @template with a name starting with the first type parameter's name
354
323
// which feels horribly hacky.
355
- logger . warn (
324
+ context . logger . warn (
356
325
i18n . multiple_type_parameters_on_template_tag_unsupported ( ) ,
357
326
declaration ,
358
327
) ;
@@ -378,7 +347,7 @@ export function getJsDocComment(
378
347
// was a comment attached. If there wasn't, then don't error about failing to find
379
348
// a tag because this is unsupported.
380
349
if ( ! ts . isJSDocTemplateTag ( declaration ) ) {
381
- logger . error (
350
+ context . logger . error (
382
351
i18n . failed_to_find_jsdoc_tag_for_name_0 ( name ) ,
383
352
declaration ,
384
353
) ;
0 commit comments