@@ -63,9 +63,10 @@ const jsDocTransformerFactory = ({
63
63
anyAlias,
64
64
typeMap : optionsTypeMap ,
65
65
} : Options ) => ( context : ts . TransformationContext ) => {
66
+ const { factory } = context ;
66
67
const anyType = anyAlias
67
- ? ts . createTypeReferenceNode ( anyAlias , undefined )
68
- : ts . createKeywordTypeNode ( ts . SyntaxKind . AnyKeyword ) ;
68
+ ? factory . createTypeReferenceNode ( anyAlias , undefined )
69
+ : factory . createKeywordTypeNode ( ts . SyntaxKind . AnyKeyword ) ;
69
70
const typeMap : TypeMap = { ...defaultTypeMap , ...optionsTypeMap } ;
70
71
71
72
function visit < T extends ts . Node > ( origNode : T ) : T {
@@ -78,7 +79,9 @@ const jsDocTransformerFactory = ({
78
79
79
80
function visitFunctionLike < T extends ts . SignatureDeclaration > ( node : T , insideClass : boolean ) : T {
80
81
const modifiers =
81
- ts . isMethodDeclaration ( node ) && insideClass ? modifiersFromJSDoc ( node ) : node . modifiers ;
82
+ ts . isMethodDeclaration ( node ) && insideClass
83
+ ? modifiersFromJSDoc ( node , factory )
84
+ : node . modifiers ;
82
85
const parameters = visitParameters ( node ) ;
83
86
const returnType = annotateReturns ? visitReturnType ( node ) : node . type ;
84
87
if (
@@ -90,8 +93,8 @@ const jsDocTransformerFactory = ({
90
93
}
91
94
92
95
const newNode = ts . getMutableClone ( node ) as any ;
93
- newNode . modifiers = ts . createNodeArray ( modifiers ) ;
94
- newNode . parameters = ts . createNodeArray ( parameters ) ;
96
+ newNode . modifiers = factory . createNodeArray ( modifiers ) ;
97
+ newNode . parameters = factory . createNodeArray ( parameters ) ;
95
98
newNode . type = returnType ;
96
99
return newNode ;
97
100
}
@@ -121,10 +124,10 @@ const jsDocTransformerFactory = ({
121
124
! param . initializer &&
122
125
ts . isIdentifier ( param . name ) &&
123
126
( paramNode . isBracketed || ts . isJSDocOptionalType ( typeNode ) )
124
- ? ts . createToken ( ts . SyntaxKind . QuestionToken )
127
+ ? factory . createToken ( ts . SyntaxKind . QuestionToken )
125
128
: param . questionToken ;
126
129
127
- const newParam = ts . createParameter (
130
+ const newParam = factory . createParameterDeclaration (
128
131
param . decorators ,
129
132
param . modifiers ,
130
133
param . dotDotDotToken ,
@@ -192,25 +195,25 @@ const jsDocTransformerFactory = ({
192
195
}
193
196
194
197
function visitJSDocOptionalType ( node : ts . JSDocOptionalType ) {
195
- return ts . createUnionTypeNode ( [
198
+ return factory . createUnionTypeNode ( [
196
199
ts . visitNode ( node . type , visitJSDocType ) ,
197
- ts . createKeywordTypeNode ( ts . SyntaxKind . UndefinedKeyword ) ,
200
+ factory . createKeywordTypeNode ( ts . SyntaxKind . UndefinedKeyword ) ,
198
201
] ) ;
199
202
}
200
203
201
204
function visitJSDocNullableType ( node : ts . JSDocNullableType ) {
202
- return ts . createUnionTypeNode ( [
205
+ return factory . createUnionTypeNode ( [
203
206
ts . visitNode ( node . type , visitJSDocType ) ,
204
- ts . createKeywordTypeNode ( ts . SyntaxKind . NullKeyword as any ) ,
207
+ factory . createKeywordTypeNode ( ts . SyntaxKind . NullKeyword as any ) ,
205
208
] ) ;
206
209
}
207
210
208
211
function visitJSDocVariadicType ( node : ts . JSDocVariadicType ) {
209
- return ts . createArrayTypeNode ( ts . visitNode ( node . type , visitJSDocType ) ) ;
212
+ return factory . createArrayTypeNode ( ts . visitNode ( node . type , visitJSDocType ) ) ;
210
213
}
211
214
212
215
function visitJSDocFunctionType ( node : ts . JSDocFunctionType ) {
213
- return ts . createFunctionTypeNode (
216
+ return factory . createFunctionTypeNode (
214
217
undefined ,
215
218
node . parameters . map ( visitJSDocParameter ) ,
216
219
node . type ?? anyType ,
@@ -227,7 +230,7 @@ const jsDocTransformerFactory = ({
227
230
}
228
231
} ) ;
229
232
}
230
- return ts . createTypeLiteralNode ( propertySignatures ) ;
233
+ return factory . createTypeLiteralNode ( propertySignatures ) ;
231
234
}
232
235
233
236
function visitJSDocPropertyLikeTag ( node : ts . JSDocPropertyLikeTag ) {
@@ -240,12 +243,14 @@ const jsDocTransformerFactory = ({
240
243
type = anyType ;
241
244
}
242
245
const questionToken =
243
- node . isBracketed || optionalType ? ts . createToken ( ts . SyntaxKind . QuestionToken ) : undefined ;
246
+ node . isBracketed || optionalType
247
+ ? factory . createToken ( ts . SyntaxKind . QuestionToken )
248
+ : undefined ;
244
249
if ( ts . isIdentifier ( node . name ) ) {
245
- return ts . createPropertySignature ( undefined , node . name , questionToken , type , undefined ) ;
250
+ return factory . createPropertySignature ( undefined , node . name , questionToken , type ) ;
246
251
}
247
252
// Assumption: the leaf field on the QualifiedName belongs directly to the parent object type.
248
- return ts . createPropertySignature ( undefined , node . name . right , questionToken , type , undefined ) ;
253
+ return factory . createPropertySignature ( undefined , node . name . right , questionToken , type ) ;
249
254
}
250
255
251
256
function visitJSDocParameter ( node : ts . ParameterDeclaration ) {
@@ -257,8 +262,10 @@ const jsDocTransformerFactory = ({
257
262
node . type . kind === ts . SyntaxKind . JSDocVariadicType &&
258
263
index === node . parent . parameters . length - 1 ;
259
264
const name = node . name || ( isRest ? 'rest' : `arg${ index } ` ) ;
260
- const dotdotdot = isRest ? ts . createToken ( ts . SyntaxKind . DotDotDotToken ) : node . dotDotDotToken ;
261
- return ts . createParameter (
265
+ const dotdotdot = isRest
266
+ ? factory . createToken ( ts . SyntaxKind . DotDotDotToken )
267
+ : node . dotDotDotToken ;
268
+ return factory . createParameterDeclaration (
262
269
node . decorators ,
263
270
node . modifiers ,
264
271
dotdotdot ,
@@ -290,35 +297,35 @@ const jsDocTransformerFactory = ({
290
297
}
291
298
}
292
299
293
- name = ts . createIdentifier ( text ) ;
300
+ name = factory . createIdentifier ( text ) ;
294
301
if ( ( text === 'Array' || text === 'Promise' ) && ! node . typeArguments ) {
295
- args = ts . createNodeArray ( [ anyType ] ) ;
302
+ args = factory . createNodeArray ( [ anyType ] ) ;
296
303
} else if ( acceptsTypeParameters ) {
297
304
args = ts . visitNodes ( node . typeArguments , visitJSDocType ) ;
298
305
}
299
306
if ( ! acceptsTypeParameters ) {
300
307
args = undefined ;
301
308
}
302
309
}
303
- return ts . createTypeReferenceNode ( name , args ) ;
310
+ return factory . createTypeReferenceNode ( name , args ) ;
304
311
}
305
312
306
313
function visitJSDocIndexSignature ( node : ts . TypeReferenceNode ) {
307
314
const typeArguments = node . typeArguments ! ;
308
- const index = ts . createParameter (
315
+ const index = factory . createParameterDeclaration (
309
316
/* decorators */ undefined ,
310
317
/* modifiers */ undefined ,
311
318
/* dotDotDotToken */ undefined ,
312
319
typeArguments [ 0 ] . kind === ts . SyntaxKind . NumberKeyword ? 'n' : 's' ,
313
320
/* questionToken */ undefined ,
314
- ts . createTypeReferenceNode (
321
+ factory . createTypeReferenceNode (
315
322
typeArguments [ 0 ] . kind === ts . SyntaxKind . NumberKeyword ? 'number' : 'string' ,
316
323
[ ] ,
317
324
) ,
318
325
/* initializer */ undefined ,
319
326
) ;
320
- const indexSignature = ts . createTypeLiteralNode ( [
321
- ts . createIndexSignature (
327
+ const indexSignature = factory . createTypeLiteralNode ( [
328
+ factory . createIndexSignature (
322
329
/* decorators */ undefined ,
323
330
/* modifiers */ undefined ,
324
331
[ index ] ,
@@ -337,6 +344,7 @@ const accessibilityMask =
337
344
338
345
function modifiersFromJSDoc (
339
346
methodDeclaration : ts . MethodDeclaration ,
347
+ factory : ts . NodeFactory ,
340
348
) : ReadonlyArray < ts . Modifier > | undefined {
341
349
let modifierFlags = ts . getCombinedModifierFlags ( methodDeclaration ) ;
342
350
if ( ( modifierFlags & accessibilityMask ) !== 0 ) {
@@ -354,7 +362,7 @@ function modifiersFromJSDoc(
354
362
return methodDeclaration . modifiers ;
355
363
}
356
364
357
- return ts . createModifiersFromModifierFlags ( modifierFlags ) ;
365
+ return factory . createModifiersFromModifierFlags ( modifierFlags ) ;
358
366
}
359
367
360
368
// Copied from: https://github.com/microsoft/TypeScript/blob/v4.0.2/src/compiler/utilities.ts#L1879
0 commit comments