@kubb/ast 5.0.0-alpha.31 → 5.0.0-alpha.32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +1556 -1067
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +42 -3
- package/dist/index.js +1519 -1067
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +2 -2
- package/dist/{visitor-z-5U8NoF.d.ts → visitor-DysNCWvh.d.ts} +794 -61
- package/package.json +1 -1
- package/src/constants.ts +7 -1
- package/src/factory.ts +296 -24
- package/src/guards.ts +17 -4
- package/src/index.ts +23 -5
- package/src/mocks.ts +6 -6
- package/src/nodes/base.ts +11 -2
- package/src/nodes/code.ts +237 -0
- package/src/nodes/file.ts +235 -0
- package/src/nodes/function.ts +35 -17
- package/src/nodes/index.ts +32 -7
- package/src/nodes/output.ts +26 -0
- package/src/nodes/root.ts +9 -8
- package/src/refs.ts +5 -5
- package/src/types.ts +14 -2
- package/src/utils.ts +177 -19
- package/src/visitor.ts +52 -25
package/package.json
CHANGED
package/src/constants.ts
CHANGED
|
@@ -15,7 +15,8 @@ export const visitorDepths = {
|
|
|
15
15
|
} as const satisfies Record<VisitorDepth, VisitorDepth>
|
|
16
16
|
|
|
17
17
|
export const nodeKinds = {
|
|
18
|
-
|
|
18
|
+
input: 'Input',
|
|
19
|
+
output: 'Output',
|
|
19
20
|
operation: 'Operation',
|
|
20
21
|
schema: 'Schema',
|
|
21
22
|
property: 'Property',
|
|
@@ -24,6 +25,11 @@ export const nodeKinds = {
|
|
|
24
25
|
functionParameter: 'FunctionParameter',
|
|
25
26
|
parameterGroup: 'ParameterGroup',
|
|
26
27
|
functionParameters: 'FunctionParameters',
|
|
28
|
+
type: 'Type',
|
|
29
|
+
file: 'File',
|
|
30
|
+
import: 'Import',
|
|
31
|
+
export: 'Export',
|
|
32
|
+
source: 'Source',
|
|
27
33
|
} as const satisfies Record<string, NodeKind>
|
|
28
34
|
|
|
29
35
|
/**
|
package/src/factory.ts
CHANGED
|
@@ -1,18 +1,31 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
import { trimExtName } from '@internals/utils'
|
|
1
4
|
import type { InferSchemaNode } from './infer.ts'
|
|
2
5
|
import type {
|
|
6
|
+
ArrowFunctionNode,
|
|
7
|
+
ConstNode,
|
|
8
|
+
ExportNode,
|
|
9
|
+
FileNode,
|
|
10
|
+
FunctionNode,
|
|
3
11
|
FunctionParameterNode,
|
|
4
12
|
FunctionParametersNode,
|
|
13
|
+
ImportNode,
|
|
14
|
+
InputNode,
|
|
5
15
|
ObjectSchemaNode,
|
|
6
16
|
OperationNode,
|
|
17
|
+
OutputNode,
|
|
7
18
|
ParameterGroupNode,
|
|
8
19
|
ParameterNode,
|
|
20
|
+
ParamsTypeNode,
|
|
9
21
|
PrimitiveSchemaType,
|
|
10
22
|
PropertyNode,
|
|
11
23
|
ResponseNode,
|
|
12
|
-
RootNode,
|
|
13
24
|
SchemaNode,
|
|
25
|
+
SourceNode,
|
|
14
26
|
TypeNode,
|
|
15
27
|
} from './nodes/index.ts'
|
|
28
|
+
import { combineExports, combineImports, combineSources } from './utils.ts'
|
|
16
29
|
|
|
17
30
|
/**
|
|
18
31
|
* Syncs property/parameter schema optionality flags from `required` and `schema.nullable`.
|
|
@@ -48,26 +61,48 @@ type CreateSchemaInput = CreateSchemaObjectInput | DistributiveOmit<Exclude<Sche
|
|
|
48
61
|
type CreateSchemaOutput<T extends CreateSchemaInput> = InferSchemaNode<T> & { kind: 'Schema' }
|
|
49
62
|
|
|
50
63
|
/**
|
|
51
|
-
* Creates
|
|
64
|
+
* Creates an `InputNode` with stable defaults for `schemas` and `operations`.
|
|
52
65
|
*
|
|
53
66
|
* @example
|
|
54
67
|
* ```ts
|
|
55
|
-
* const
|
|
56
|
-
* // { kind: '
|
|
68
|
+
* const input = createInput()
|
|
69
|
+
* // { kind: 'Input', schemas: [], operations: [] }
|
|
57
70
|
* ```
|
|
58
71
|
*
|
|
59
72
|
* @example
|
|
60
73
|
* ```ts
|
|
61
|
-
* const
|
|
74
|
+
* const input = createInput({ schemas: [petSchema] })
|
|
62
75
|
* // keeps default operations: []
|
|
63
76
|
* ```
|
|
64
77
|
*/
|
|
65
|
-
export function
|
|
78
|
+
export function createInput(overrides: Partial<Omit<InputNode, 'kind'>> = {}): InputNode {
|
|
66
79
|
return {
|
|
67
80
|
schemas: [],
|
|
68
81
|
operations: [],
|
|
69
82
|
...overrides,
|
|
70
|
-
kind: '
|
|
83
|
+
kind: 'Input',
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Creates an `OutputNode` with a stable default for `files`.
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
* ```ts
|
|
92
|
+
* const output = createOutput()
|
|
93
|
+
* // { kind: 'Output', files: [] }
|
|
94
|
+
* ```
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* ```ts
|
|
98
|
+
* const output = createOutput({ files: [petFile] })
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
export function createOutput(overrides: Partial<Omit<OutputNode, 'kind'>> = {}): OutputNode {
|
|
102
|
+
return {
|
|
103
|
+
files: [],
|
|
104
|
+
...overrides,
|
|
105
|
+
kind: 'Output',
|
|
71
106
|
}
|
|
72
107
|
}
|
|
73
108
|
|
|
@@ -176,6 +211,8 @@ export function createSchema(props: CreateSchemaInput): SchemaNode {
|
|
|
176
211
|
return { primitive: inferredPrimitive, ...props, kind: 'Schema' } as CreateSchemaOutput<typeof props>
|
|
177
212
|
}
|
|
178
213
|
|
|
214
|
+
type UserPropertyNode = Pick<PropertyNode, 'name' | 'schema'> & Partial<Omit<PropertyNode, 'kind' | 'name' | 'schema'>>
|
|
215
|
+
|
|
179
216
|
/**
|
|
180
217
|
* Creates a `PropertyNode`.
|
|
181
218
|
*
|
|
@@ -201,7 +238,7 @@ export function createSchema(props: CreateSchemaInput): SchemaNode {
|
|
|
201
238
|
* // required=true, no optional/nullish
|
|
202
239
|
* ```
|
|
203
240
|
*/
|
|
204
|
-
export function createProperty(props:
|
|
241
|
+
export function createProperty(props: UserPropertyNode): PropertyNode {
|
|
205
242
|
const required = props.required ?? false
|
|
206
243
|
|
|
207
244
|
return {
|
|
@@ -278,24 +315,24 @@ export function createResponse(
|
|
|
278
315
|
*
|
|
279
316
|
* @example Required typed param
|
|
280
317
|
* ```ts
|
|
281
|
-
* createFunctionParameter({ name: 'petId', type:
|
|
318
|
+
* createFunctionParameter({ name: 'petId', type: createParamsType({ variant: 'reference', name: 'string' }) })
|
|
282
319
|
* // → petId: string
|
|
283
320
|
* ```
|
|
284
321
|
*
|
|
285
322
|
* @example Optional param
|
|
286
323
|
* ```ts
|
|
287
|
-
* createFunctionParameter({ name: 'params', type:
|
|
324
|
+
* createFunctionParameter({ name: 'params', type: createParamsType({ variant: 'reference', name: 'QueryParams' }), optional: true })
|
|
288
325
|
* // → params?: QueryParams
|
|
289
326
|
* ```
|
|
290
327
|
*
|
|
291
328
|
* @example Param with default (implicitly optional; cannot combine with `optional: true`)
|
|
292
329
|
* ```ts
|
|
293
|
-
* createFunctionParameter({ name: 'config', type:
|
|
330
|
+
* createFunctionParameter({ name: 'config', type: createParamsType({ variant: 'reference', name: 'RequestConfig' }), default: '{}' })
|
|
294
331
|
* // → config: RequestConfig = {}
|
|
295
332
|
* ```
|
|
296
333
|
*/
|
|
297
334
|
export function createFunctionParameter(
|
|
298
|
-
props: { name: string; type?:
|
|
335
|
+
props: { name: string; type?: ParamsTypeNode; rest?: boolean } & ({ optional: true; default?: never } | { optional?: false; default?: string }),
|
|
299
336
|
): FunctionParameterNode {
|
|
300
337
|
return {
|
|
301
338
|
optional: false,
|
|
@@ -313,26 +350,26 @@ export function createFunctionParameter(
|
|
|
313
350
|
*
|
|
314
351
|
* @example Reference type (TypeScript: `QueryParams`)
|
|
315
352
|
* ```ts
|
|
316
|
-
*
|
|
353
|
+
* createParamsType({ variant: 'reference', name: 'QueryParams' })
|
|
317
354
|
* ```
|
|
318
355
|
*
|
|
319
356
|
* @example Struct type (TypeScript: `{ petId: string }`)
|
|
320
357
|
* ```ts
|
|
321
|
-
*
|
|
358
|
+
* createParamsType({ variant: 'struct', properties: [{ name: 'petId', optional: false, type: createParamsType({ variant: 'reference', name: 'string' }) }] })
|
|
322
359
|
* ```
|
|
323
360
|
*
|
|
324
361
|
* @example Member type (TypeScript: `DeletePetPathParams['petId']`)
|
|
325
362
|
* ```ts
|
|
326
|
-
*
|
|
363
|
+
* createParamsType({ variant: 'member', base: 'DeletePetPathParams', key: 'petId' })
|
|
327
364
|
* ```
|
|
328
365
|
*/
|
|
329
|
-
export function
|
|
366
|
+
export function createParamsType(
|
|
330
367
|
props:
|
|
331
368
|
| { variant: 'reference'; name: string }
|
|
332
|
-
| { variant: 'struct'; properties: Array<{ name: string; optional: boolean; type:
|
|
369
|
+
| { variant: 'struct'; properties: Array<{ name: string; optional: boolean; type: ParamsTypeNode }> }
|
|
333
370
|
| { variant: 'member'; base: string; key: string },
|
|
334
|
-
):
|
|
335
|
-
return { ...props, kind: '
|
|
371
|
+
): ParamsTypeNode {
|
|
372
|
+
return { ...props, kind: 'ParamsType' } as ParamsTypeNode
|
|
336
373
|
}
|
|
337
374
|
|
|
338
375
|
/**
|
|
@@ -342,8 +379,8 @@ export function createTypeNode(
|
|
|
342
379
|
* ```ts
|
|
343
380
|
* createParameterGroup({
|
|
344
381
|
* properties: [
|
|
345
|
-
* createFunctionParameter({ name: 'id', type:
|
|
346
|
-
* createFunctionParameter({ name: 'name', type:
|
|
382
|
+
* createFunctionParameter({ name: 'id', type: createParamsType({ variant: 'reference', name: 'string' }), optional: false }),
|
|
383
|
+
* createFunctionParameter({ name: 'name', type: createParamsType({ variant: 'reference', name: 'string' }), optional: true }),
|
|
347
384
|
* ],
|
|
348
385
|
* default: '{}',
|
|
349
386
|
* })
|
|
@@ -354,7 +391,7 @@ export function createTypeNode(
|
|
|
354
391
|
* @example Inline (spread) — children emitted as individual top-level parameters
|
|
355
392
|
* ```ts
|
|
356
393
|
* createParameterGroup({
|
|
357
|
-
* properties: [createFunctionParameter({ name: 'petId', type:
|
|
394
|
+
* properties: [createFunctionParameter({ name: 'petId', type: createParamsType({ variant: 'reference', name: 'string' }), optional: false })],
|
|
358
395
|
* inline: true,
|
|
359
396
|
* })
|
|
360
397
|
* // declaration → petId: string
|
|
@@ -377,8 +414,8 @@ export function createParameterGroup(
|
|
|
377
414
|
* ```ts
|
|
378
415
|
* createFunctionParameters({
|
|
379
416
|
* params: [
|
|
380
|
-
* createFunctionParameter({ name: 'petId', type:
|
|
381
|
-
* createFunctionParameter({ name: 'config', type:
|
|
417
|
+
* createFunctionParameter({ name: 'petId', type: createParamsType({ variant: 'reference', name: 'string' }), optional: false }),
|
|
418
|
+
* createFunctionParameter({ name: 'config', type: createParamsType({ variant: 'reference', name: 'RequestConfig' }), optional: false, default: '{}' }),
|
|
382
419
|
* ],
|
|
383
420
|
* })
|
|
384
421
|
* ```
|
|
@@ -396,3 +433,238 @@ export function createFunctionParameters(props: Partial<Omit<FunctionParametersN
|
|
|
396
433
|
kind: 'FunctionParameters',
|
|
397
434
|
}
|
|
398
435
|
}
|
|
436
|
+
|
|
437
|
+
/**
|
|
438
|
+
* Creates an `ImportNode` representing a language-agnostic import/dependency declaration.
|
|
439
|
+
*
|
|
440
|
+
* @example Named import
|
|
441
|
+
* ```ts
|
|
442
|
+
* createImport({ name: ['useState'], path: 'react' })
|
|
443
|
+
* // import { useState } from 'react'
|
|
444
|
+
* ```
|
|
445
|
+
*
|
|
446
|
+
* @example Type-only import
|
|
447
|
+
* ```ts
|
|
448
|
+
* createImport({ name: ['FC'], path: 'react', isTypeOnly: true })
|
|
449
|
+
* // import type { FC } from 'react'
|
|
450
|
+
* ```
|
|
451
|
+
*/
|
|
452
|
+
export function createImport(props: Omit<ImportNode, 'kind'>): ImportNode {
|
|
453
|
+
return { ...props, kind: 'Import' }
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
/**
|
|
457
|
+
* Creates an `ExportNode` representing a language-agnostic export/public API declaration.
|
|
458
|
+
*
|
|
459
|
+
* @example Named export
|
|
460
|
+
* ```ts
|
|
461
|
+
* createExport({ name: ['Pet'], path: './Pet' })
|
|
462
|
+
* // export { Pet } from './Pet'
|
|
463
|
+
* ```
|
|
464
|
+
*
|
|
465
|
+
* @example Wildcard export
|
|
466
|
+
* ```ts
|
|
467
|
+
* createExport({ path: './utils' })
|
|
468
|
+
* // export * from './utils'
|
|
469
|
+
* ```
|
|
470
|
+
*/
|
|
471
|
+
export function createExport(props: Omit<ExportNode, 'kind'>): ExportNode {
|
|
472
|
+
return { ...props, kind: 'Export' }
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
/**
|
|
476
|
+
* Creates a `SourceNode` representing a fragment of source code within a file.
|
|
477
|
+
*
|
|
478
|
+
* @example
|
|
479
|
+
* ```ts
|
|
480
|
+
* createSource({ name: 'Pet', value: 'export type Pet = { id: number }', isExportable: true })
|
|
481
|
+
* ```
|
|
482
|
+
*/
|
|
483
|
+
export function createSource(props: Omit<SourceNode, 'kind'>): SourceNode {
|
|
484
|
+
return { ...props, kind: 'Source' }
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
type UserFileNode<TMeta extends object = object> = Omit<FileNode<TMeta>, 'kind' | 'id' | 'name' | 'extname' | 'imports' | 'exports' | 'sources'> &
|
|
488
|
+
Pick<Partial<FileNode<TMeta>>, 'imports' | 'exports' | 'sources'>
|
|
489
|
+
|
|
490
|
+
/**
|
|
491
|
+
* Creates a fully resolved `FileNode` from a file input descriptor.
|
|
492
|
+
*
|
|
493
|
+
* Computes:
|
|
494
|
+
* - `id` — SHA256 hash of the file path
|
|
495
|
+
* - `name` — `baseName` without extension
|
|
496
|
+
* - `extname` — extension extracted from `baseName`
|
|
497
|
+
*
|
|
498
|
+
* Deduplicates:
|
|
499
|
+
* - `sources` via `combineSources`
|
|
500
|
+
* - `exports` via `combineExports`
|
|
501
|
+
* - `imports` via `combineImports` (also filters unused imports)
|
|
502
|
+
*
|
|
503
|
+
* @throws {Error} when `baseName` has no extension.
|
|
504
|
+
*
|
|
505
|
+
* @example
|
|
506
|
+
* ```ts
|
|
507
|
+
* const file = createFile({
|
|
508
|
+
* baseName: 'petStore.ts',
|
|
509
|
+
* path: 'src/models/petStore.ts',
|
|
510
|
+
* sources: [createSource({ name: 'Pet', value: 'export type Pet = { id: number }' })],
|
|
511
|
+
* imports: [createImport({ name: ['z'], path: 'zod' })],
|
|
512
|
+
* exports: [createExport({ name: ['Pet'], path: './petStore' })],
|
|
513
|
+
* })
|
|
514
|
+
* // file.id = SHA256 hash of 'src/models/petStore.ts'
|
|
515
|
+
* // file.name = 'petStore'
|
|
516
|
+
* // file.extname = '.ts'
|
|
517
|
+
* ```
|
|
518
|
+
*/
|
|
519
|
+
export function createFile<TMeta extends object = object>(input: UserFileNode<TMeta>): FileNode<TMeta> {
|
|
520
|
+
const rawExtname = path.extname(input.baseName)
|
|
521
|
+
// Handle dotfile basename like '.ts' where path.extname returns ''
|
|
522
|
+
const extname = (rawExtname || (input.baseName.startsWith('.') ? input.baseName : '')) as `.${string}`
|
|
523
|
+
if (!extname) {
|
|
524
|
+
throw new Error(`No extname found for ${input.baseName}`)
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
const source = (input.sources ?? []).map((item) => item.value).join('\n\n')
|
|
528
|
+
const resolvedExports = input.exports?.length ? combineExports(input.exports) : []
|
|
529
|
+
const resolvedImports = input.imports?.length && source ? combineImports(input.imports, resolvedExports, source) : []
|
|
530
|
+
const resolvedSources = input.sources?.length ? combineSources(input.sources) : []
|
|
531
|
+
|
|
532
|
+
return {
|
|
533
|
+
kind: 'File',
|
|
534
|
+
...input,
|
|
535
|
+
id: createHash('sha256').update(input.path).digest('hex'),
|
|
536
|
+
name: trimExtName(input.baseName),
|
|
537
|
+
extname,
|
|
538
|
+
imports: resolvedImports,
|
|
539
|
+
exports: resolvedExports,
|
|
540
|
+
sources: resolvedSources,
|
|
541
|
+
meta: input.meta ?? ({} as TMeta),
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
/**
|
|
546
|
+
* Creates a `ConstNode` representing a TypeScript `const` declaration.
|
|
547
|
+
*
|
|
548
|
+
* Mirrors the `Const` component from `@kubb/react-fabric`.
|
|
549
|
+
* The component's `children` are represented as `nodes`.
|
|
550
|
+
*
|
|
551
|
+
* @example Simple constant
|
|
552
|
+
* ```ts
|
|
553
|
+
* createConst({ name: 'pet' })
|
|
554
|
+
* // const pet = ...
|
|
555
|
+
* ```
|
|
556
|
+
*
|
|
557
|
+
* @example Exported constant with type and `as const`
|
|
558
|
+
* ```ts
|
|
559
|
+
* createConst({ name: 'pets', export: true, type: 'Pet[]', asConst: true })
|
|
560
|
+
* // export const pets: Pet[] = ... as const
|
|
561
|
+
* ```
|
|
562
|
+
*
|
|
563
|
+
* @example With JSDoc and child nodes
|
|
564
|
+
* ```ts
|
|
565
|
+
* createConst({
|
|
566
|
+
* name: 'config',
|
|
567
|
+
* export: true,
|
|
568
|
+
* JSDoc: { comments: ['@description App configuration'] },
|
|
569
|
+
* nodes: [],
|
|
570
|
+
* })
|
|
571
|
+
* ```
|
|
572
|
+
*/
|
|
573
|
+
export function createConst(props: Omit<ConstNode, 'kind'>): ConstNode {
|
|
574
|
+
return { ...props, kind: 'Const' }
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
/**
|
|
578
|
+
* Creates a `TypeNode` representing a TypeScript `type` alias declaration.
|
|
579
|
+
*
|
|
580
|
+
* Mirrors the `Type` component from `@kubb/react-fabric`.
|
|
581
|
+
* The component's `children` are represented as `nodes`.
|
|
582
|
+
*
|
|
583
|
+
* @example Simple type alias
|
|
584
|
+
* ```ts
|
|
585
|
+
* createType({ name: 'Pet' })
|
|
586
|
+
* // type Pet = ...
|
|
587
|
+
* ```
|
|
588
|
+
*
|
|
589
|
+
* @example Exported type with JSDoc
|
|
590
|
+
* ```ts
|
|
591
|
+
* createType({
|
|
592
|
+
* name: 'PetStatus',
|
|
593
|
+
* export: true,
|
|
594
|
+
* JSDoc: { comments: ['@description Status of a pet'] },
|
|
595
|
+
* })
|
|
596
|
+
* // export type PetStatus = ...
|
|
597
|
+
* ```
|
|
598
|
+
*/
|
|
599
|
+
export function createType(props: Omit<TypeNode, 'kind'>): TypeNode {
|
|
600
|
+
return { ...props, kind: 'Type' }
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
/**
|
|
604
|
+
* Creates a `FunctionNode` representing a TypeScript `function` declaration.
|
|
605
|
+
*
|
|
606
|
+
* Mirrors the `Function` component from `@kubb/react-fabric`.
|
|
607
|
+
* The component's `children` are represented as `nodes`.
|
|
608
|
+
*
|
|
609
|
+
* @example Simple function
|
|
610
|
+
* ```ts
|
|
611
|
+
* createFunction({ name: 'getPet' })
|
|
612
|
+
* // function getPet() { ... }
|
|
613
|
+
* ```
|
|
614
|
+
*
|
|
615
|
+
* @example Exported async function with return type
|
|
616
|
+
* ```ts
|
|
617
|
+
* createFunction({ name: 'fetchPet', export: true, async: true, returnType: 'Pet' })
|
|
618
|
+
* // export async function fetchPet(): Promise<Pet> { ... }
|
|
619
|
+
* ```
|
|
620
|
+
*
|
|
621
|
+
* @example Function with generics and params
|
|
622
|
+
* ```ts
|
|
623
|
+
* createFunction({
|
|
624
|
+
* name: 'identity',
|
|
625
|
+
* export: true,
|
|
626
|
+
* generics: ['T'],
|
|
627
|
+
* params: 'value: T',
|
|
628
|
+
* returnType: 'T',
|
|
629
|
+
* })
|
|
630
|
+
* // export function identity<T>(value: T): T { ... }
|
|
631
|
+
* ```
|
|
632
|
+
*/
|
|
633
|
+
export function createFunction(props: Omit<FunctionNode, 'kind'>): FunctionNode {
|
|
634
|
+
return { ...props, kind: 'Function' }
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
/**
|
|
638
|
+
* Creates an `ArrowFunctionNode` representing a TypeScript arrow function.
|
|
639
|
+
*
|
|
640
|
+
* Mirrors the `Function.Arrow` component from `@kubb/react-fabric`.
|
|
641
|
+
* The component's `children` are represented as `nodes`.
|
|
642
|
+
*
|
|
643
|
+
* @example Simple arrow function
|
|
644
|
+
* ```ts
|
|
645
|
+
* createArrowFunction({ name: 'getPet' })
|
|
646
|
+
* // const getPet = () => { ... }
|
|
647
|
+
* ```
|
|
648
|
+
*
|
|
649
|
+
* @example Single-line exported arrow function
|
|
650
|
+
* ```ts
|
|
651
|
+
* createArrowFunction({ name: 'double', export: true, params: 'n: number', singleLine: true })
|
|
652
|
+
* // export const double = (n: number) => ...
|
|
653
|
+
* ```
|
|
654
|
+
*
|
|
655
|
+
* @example Async arrow function with generics
|
|
656
|
+
* ```ts
|
|
657
|
+
* createArrowFunction({
|
|
658
|
+
* name: 'fetchPet',
|
|
659
|
+
* export: true,
|
|
660
|
+
* async: true,
|
|
661
|
+
* generics: ['T'],
|
|
662
|
+
* params: 'id: string',
|
|
663
|
+
* returnType: 'T',
|
|
664
|
+
* })
|
|
665
|
+
* // export const fetchPet = async <T>(id: string): Promise<T> => { ... }
|
|
666
|
+
* ```
|
|
667
|
+
*/
|
|
668
|
+
export function createArrowFunction(props: Omit<ArrowFunctionNode, 'kind'>): ArrowFunctionNode {
|
|
669
|
+
return { ...props, kind: 'ArrowFunction' }
|
|
670
|
+
}
|
package/src/guards.ts
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
FunctionParameterNode,
|
|
3
3
|
FunctionParametersNode,
|
|
4
|
+
InputNode,
|
|
4
5
|
Node,
|
|
5
6
|
NodeKind,
|
|
6
7
|
OperationNode,
|
|
8
|
+
OutputNode,
|
|
7
9
|
ParameterGroupNode,
|
|
8
10
|
ParameterNode,
|
|
9
11
|
PropertyNode,
|
|
10
12
|
ResponseNode,
|
|
11
|
-
RootNode,
|
|
12
13
|
SchemaNode,
|
|
13
14
|
SchemaNodeByType,
|
|
14
15
|
} from './nodes/index.ts'
|
|
@@ -31,16 +32,28 @@ function isKind<T extends Node>(kind: NodeKind) {
|
|
|
31
32
|
}
|
|
32
33
|
|
|
33
34
|
/**
|
|
34
|
-
* Returns `true` when the input is
|
|
35
|
+
* Returns `true` when the input is an `InputNode`.
|
|
35
36
|
*
|
|
36
37
|
* @example
|
|
37
38
|
* ```ts
|
|
38
|
-
* if (
|
|
39
|
+
* if (isInputNode(node)) {
|
|
39
40
|
* console.log(node.schemas.length)
|
|
40
41
|
* }
|
|
41
42
|
* ```
|
|
42
43
|
*/
|
|
43
|
-
export const
|
|
44
|
+
export const isInputNode = isKind<InputNode>('Input')
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Returns `true` when the input is an `OutputNode`.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```ts
|
|
51
|
+
* if (isOutputNode(node)) {
|
|
52
|
+
* console.log(node.files.length)
|
|
53
|
+
* }
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
export const isOutputNode = isKind<OutputNode>('Output')
|
|
44
57
|
|
|
45
58
|
/**
|
|
46
59
|
* Returns `true` when the input is an `OperationNode`.
|
package/src/index.ts
CHANGED
|
@@ -1,19 +1,28 @@
|
|
|
1
1
|
export type { ScalarPrimitive } from './constants.ts'
|
|
2
|
-
export { httpMethods, isScalarPrimitive, mediaTypes, schemaTypes } from './constants.ts'
|
|
2
|
+
export { httpMethods, isScalarPrimitive, mediaTypes, nodeKinds, schemaTypes } from './constants.ts'
|
|
3
3
|
export {
|
|
4
|
+
createArrowFunction,
|
|
5
|
+
createConst,
|
|
6
|
+
createExport,
|
|
7
|
+
createFile,
|
|
8
|
+
createFunction,
|
|
4
9
|
createFunctionParameter,
|
|
5
10
|
createFunctionParameters,
|
|
11
|
+
createImport,
|
|
12
|
+
createInput,
|
|
6
13
|
createOperation,
|
|
14
|
+
createOutput,
|
|
7
15
|
createParameter,
|
|
8
16
|
createParameterGroup,
|
|
17
|
+
createParamsType,
|
|
9
18
|
createProperty,
|
|
10
19
|
createResponse,
|
|
11
|
-
createRoot,
|
|
12
20
|
createSchema,
|
|
13
|
-
|
|
21
|
+
createSource,
|
|
22
|
+
createType,
|
|
14
23
|
syncOptionality,
|
|
15
24
|
} from './factory.ts'
|
|
16
|
-
export { isOperationNode, isSchemaNode, narrowSchema } from './guards.ts'
|
|
25
|
+
export { isInputNode, isOperationNode, isOutputNode, isSchemaNode, narrowSchema } from './guards.ts'
|
|
17
26
|
export type { ParserOptions } from './infer.ts'
|
|
18
27
|
export type { Printer, PrinterFactoryOptions, PrinterPartial } from './printer.ts'
|
|
19
28
|
export { createPrinterFactory, definePrinter } from './printer.ts'
|
|
@@ -21,5 +30,14 @@ export { extractRefName } from './refs.ts'
|
|
|
21
30
|
export { childName, collectImports, enumPropName, findDiscriminator } from './resolvers.ts'
|
|
22
31
|
export { mergeAdjacentObjects, setDiscriminatorEnum, setEnumName, simplifyUnion } from './transformers.ts'
|
|
23
32
|
export type { OperationParamsResolver } from './utils.ts'
|
|
24
|
-
export {
|
|
33
|
+
export {
|
|
34
|
+
caseParams,
|
|
35
|
+
combineExports,
|
|
36
|
+
combineImports,
|
|
37
|
+
combineSources,
|
|
38
|
+
createDiscriminantNode,
|
|
39
|
+
createOperationParams,
|
|
40
|
+
isStringType,
|
|
41
|
+
syncSchemaRef,
|
|
42
|
+
} from './utils.ts'
|
|
25
43
|
export { collect, composeTransformers, transform, walk } from './visitor.ts'
|
package/src/mocks.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { createOperation, createParameter, createProperty, createResponse,
|
|
2
|
-
import type {
|
|
1
|
+
import { createInput, createOperation, createParameter, createProperty, createResponse, createSchema } from './factory.ts'
|
|
2
|
+
import type { InputNode } from './nodes/root.ts'
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Builds a minimal sample AST with one `Pet` schema and one `getPetById` operation.
|
|
6
6
|
*/
|
|
7
|
-
export function buildSampleTree():
|
|
7
|
+
export function buildSampleTree(): InputNode {
|
|
8
8
|
const petSchema = createSchema({
|
|
9
9
|
type: 'object',
|
|
10
10
|
name: 'Pet',
|
|
@@ -29,7 +29,7 @@ export function buildSampleTree(): RootNode {
|
|
|
29
29
|
],
|
|
30
30
|
})
|
|
31
31
|
|
|
32
|
-
return
|
|
32
|
+
return createInput({ schemas: [petSchema], operations: [operation] })
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
/**
|
|
@@ -37,11 +37,11 @@ export function buildSampleTree(): RootNode {
|
|
|
37
37
|
* - six named schemas (`Pet`, `NewPet`, `PetList`, `Error`, `PetOrError`, `FullPet`)
|
|
38
38
|
* - two operations (`listPets`, `createPet`)
|
|
39
39
|
*/
|
|
40
|
-
export function buildFixture():
|
|
40
|
+
export function buildFixture(): InputNode {
|
|
41
41
|
const refPet = createSchema({ type: 'ref', ref: 'Pet' })
|
|
42
42
|
const refError = createSchema({ type: 'ref', ref: 'Error' })
|
|
43
43
|
|
|
44
|
-
return
|
|
44
|
+
return createInput({
|
|
45
45
|
schemas: [
|
|
46
46
|
createSchema({
|
|
47
47
|
name: 'Pet',
|
package/src/nodes/base.ts
CHANGED
|
@@ -7,7 +7,8 @@
|
|
|
7
7
|
* ```
|
|
8
8
|
*/
|
|
9
9
|
export type NodeKind =
|
|
10
|
-
| '
|
|
10
|
+
| 'Input'
|
|
11
|
+
| 'Output'
|
|
11
12
|
| 'Operation'
|
|
12
13
|
| 'Schema'
|
|
13
14
|
| 'Property'
|
|
@@ -17,13 +18,21 @@ export type NodeKind =
|
|
|
17
18
|
| 'ParameterGroup'
|
|
18
19
|
| 'FunctionParameters'
|
|
19
20
|
| 'Type'
|
|
21
|
+
| 'ParamsType'
|
|
22
|
+
| 'File'
|
|
23
|
+
| 'Import'
|
|
24
|
+
| 'Export'
|
|
25
|
+
| 'Source'
|
|
26
|
+
| 'Const'
|
|
27
|
+
| 'Function'
|
|
28
|
+
| 'ArrowFunction'
|
|
20
29
|
|
|
21
30
|
/**
|
|
22
31
|
* Base shape shared by all AST nodes.
|
|
23
32
|
*
|
|
24
33
|
* @example
|
|
25
34
|
* ```ts
|
|
26
|
-
* const base: BaseNode = { kind: '
|
|
35
|
+
* const base: BaseNode = { kind: 'Input' }
|
|
27
36
|
* ```
|
|
28
37
|
*/
|
|
29
38
|
export type BaseNode = {
|