@kubb/plugin-ts 5.0.0-alpha.9 → 5.0.0-beta.10
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/LICENSE +17 -10
- package/README.md +26 -7
- package/dist/index.cjs +1526 -5
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +558 -27
- package/dist/index.js +1488 -2
- package/dist/index.js.map +1 -0
- package/extension.yaml +632 -0
- package/package.json +43 -65
- package/src/components/{v2/Enum.tsx → Enum.tsx} +33 -17
- package/src/components/Type.tsx +31 -161
- package/src/constants.ts +10 -0
- package/src/factory.ts +295 -39
- package/src/generators/typeGenerator.tsx +248 -420
- package/src/index.ts +9 -3
- package/src/plugin.ts +66 -205
- package/src/printers/functionPrinter.ts +197 -0
- package/src/printers/printerTs.ts +329 -0
- package/src/resolvers/resolverTs.ts +66 -0
- package/src/types.ts +233 -221
- package/src/utils.ts +129 -0
- package/dist/components-CRu8IKY3.js +0 -729
- package/dist/components-CRu8IKY3.js.map +0 -1
- package/dist/components-DeNDKlzf.cjs +0 -982
- package/dist/components-DeNDKlzf.cjs.map +0 -1
- package/dist/components.cjs +0 -3
- package/dist/components.d.ts +0 -36
- package/dist/components.js +0 -2
- package/dist/generators.cjs +0 -4
- package/dist/generators.d.ts +0 -509
- package/dist/generators.js +0 -2
- package/dist/plugin-BZkBwnEA.js +0 -1269
- package/dist/plugin-BZkBwnEA.js.map +0 -1
- package/dist/plugin-Bunz1oGa.cjs +0 -1322
- package/dist/plugin-Bunz1oGa.cjs.map +0 -1
- package/dist/types-mSXmB8WU.d.ts +0 -298
- package/src/components/index.ts +0 -1
- package/src/components/v2/Type.tsx +0 -59
- package/src/generators/index.ts +0 -2
- package/src/generators/v2/typeGenerator.tsx +0 -167
- package/src/generators/v2/utils.ts +0 -140
- package/src/parser.ts +0 -389
- package/src/printer.ts +0 -368
- package/src/resolverTs.ts +0 -77
package/src/index.ts
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export {
|
|
3
|
-
export
|
|
1
|
+
export { Enum } from './components/Enum.tsx'
|
|
2
|
+
export { Type } from './components/Type.tsx'
|
|
3
|
+
export { typeGenerator } from './generators/typeGenerator.tsx'
|
|
4
|
+
export { default, pluginTs, pluginTsName } from './plugin.ts'
|
|
5
|
+
export { functionPrinter } from './printers/functionPrinter.ts'
|
|
6
|
+
export type { PrinterTsFactory, PrinterTsNodes, PrinterTsOptions } from './printers/printerTs.ts'
|
|
7
|
+
export { printerTs } from './printers/printerTs.ts'
|
|
8
|
+
export { resolverTs } from './resolvers/resolverTs.ts'
|
|
9
|
+
export type { PluginTs, ResolverTs } from './types.ts'
|
package/src/plugin.ts
CHANGED
|
@@ -1,15 +1,31 @@
|
|
|
1
|
-
import path from 'node:path'
|
|
2
1
|
import { camelCase } from '@internals/utils'
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import { typeGenerator, typeGeneratorV2 } from './generators'
|
|
7
|
-
import { resolverTs } from './resolverTs.ts'
|
|
2
|
+
import { definePlugin, type Group } from '@kubb/core'
|
|
3
|
+
import { typeGenerator } from './generators/typeGenerator.tsx'
|
|
4
|
+
import { resolverTs } from './resolvers/resolverTs.ts'
|
|
8
5
|
import type { PluginTs } from './types.ts'
|
|
9
6
|
|
|
7
|
+
/**
|
|
8
|
+
* Canonical plugin name for `@kubb/plugin-ts`, used to identify the plugin in driver lookups and warnings.
|
|
9
|
+
*/
|
|
10
10
|
export const pluginTsName = 'plugin-ts' satisfies PluginTs['name']
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
/**
|
|
13
|
+
* The `@kubb/plugin-ts` plugin factory.
|
|
14
|
+
*
|
|
15
|
+
* Generates TypeScript type declarations from an OpenAPI/AST `RootNode`.
|
|
16
|
+
* Walks schemas and operations, delegates rendering to the active generators,
|
|
17
|
+
* and writes barrel files based on `output.barrelType`.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* import pluginTs from '@kubb/plugin-ts'
|
|
22
|
+
*
|
|
23
|
+
* export default defineConfig({
|
|
24
|
+
* plugins: [pluginTs({ output: { path: 'types' }, enumType: 'asConst' })],
|
|
25
|
+
* })
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export const pluginTs = definePlugin<PluginTs>((options) => {
|
|
13
29
|
const {
|
|
14
30
|
output = { path: 'types', barrelType: 'named' },
|
|
15
31
|
group,
|
|
@@ -17,216 +33,61 @@ export const pluginTs = createPlugin<PluginTs>((options) => {
|
|
|
17
33
|
include,
|
|
18
34
|
override = [],
|
|
19
35
|
enumType = 'asConst',
|
|
36
|
+
enumTypeSuffix = 'Key',
|
|
20
37
|
enumKeyCasing = 'none',
|
|
21
|
-
enumSuffix = 'enum',
|
|
22
|
-
dateType = 'string',
|
|
23
|
-
integerType = 'number',
|
|
24
|
-
unknownType = 'any',
|
|
25
38
|
optionalType = 'questionToken',
|
|
26
39
|
arrayType = 'array',
|
|
27
|
-
emptySchemaType = unknownType,
|
|
28
40
|
syntaxType = 'type',
|
|
29
|
-
transformers = {},
|
|
30
41
|
paramsCasing,
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
42
|
+
printer,
|
|
43
|
+
resolver: userResolver,
|
|
44
|
+
transformer: userTransformer,
|
|
45
|
+
generators: userGenerators = [],
|
|
34
46
|
} = options
|
|
35
47
|
|
|
36
|
-
|
|
37
|
-
|
|
48
|
+
const groupConfig = group
|
|
49
|
+
? ({
|
|
50
|
+
...group,
|
|
51
|
+
name: (ctx) => {
|
|
52
|
+
if (group.type === 'path') {
|
|
53
|
+
return `${ctx.group.split('/')[1]}`
|
|
54
|
+
}
|
|
55
|
+
return `${camelCase(ctx.group)}Controller`
|
|
56
|
+
},
|
|
57
|
+
} satisfies Group)
|
|
58
|
+
: undefined
|
|
38
59
|
|
|
39
60
|
return {
|
|
40
61
|
name: pluginTsName,
|
|
41
|
-
options
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
integerType,
|
|
46
|
-
optionalType,
|
|
47
|
-
arrayType,
|
|
48
|
-
enumType,
|
|
49
|
-
enumKeyCasing,
|
|
50
|
-
enumSuffix,
|
|
51
|
-
unknownType,
|
|
52
|
-
emptySchemaType,
|
|
53
|
-
syntaxType,
|
|
54
|
-
group,
|
|
55
|
-
override,
|
|
56
|
-
paramsCasing,
|
|
57
|
-
usedEnumNames,
|
|
58
|
-
},
|
|
59
|
-
pre: [pluginOasName],
|
|
60
|
-
resolvePath(baseName, pathMode, options) {
|
|
61
|
-
const root = path.resolve(this.config.root, this.config.output.path)
|
|
62
|
-
const mode = pathMode ?? getMode(path.resolve(root, output.path))
|
|
63
|
-
|
|
64
|
-
if (mode === 'single') {
|
|
65
|
-
/**
|
|
66
|
-
* when output is a file then we will always append to the same file(output file), see fileManager.addOrAppend
|
|
67
|
-
* Other plugins then need to call addOrAppend instead of just add from the fileManager class
|
|
68
|
-
*/
|
|
69
|
-
return path.resolve(root, output.path)
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
if (group && (options?.group?.path || options?.group?.tag)) {
|
|
73
|
-
const groupName: Group['name'] = group?.name
|
|
74
|
-
? group.name
|
|
75
|
-
: (ctx) => {
|
|
76
|
-
if (group?.type === 'path') {
|
|
77
|
-
return `${ctx.group.split('/')[1]}`
|
|
78
|
-
}
|
|
79
|
-
return `${camelCase(ctx.group)}Controller`
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
return path.resolve(
|
|
83
|
-
root,
|
|
84
|
-
output.path,
|
|
85
|
-
groupName({
|
|
86
|
-
group: group.type === 'path' ? options.group.path! : options.group.tag!,
|
|
87
|
-
}),
|
|
88
|
-
baseName,
|
|
89
|
-
)
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
return path.resolve(root, output.path, baseName)
|
|
93
|
-
},
|
|
94
|
-
resolveName(name, type) {
|
|
95
|
-
const resolvedName = resolverTs.default(name, type)
|
|
96
|
-
|
|
97
|
-
if (type) {
|
|
98
|
-
return transformers?.name?.(resolvedName, type) || resolvedName
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
return resolvedName
|
|
102
|
-
},
|
|
103
|
-
async install() {
|
|
104
|
-
const { config, fabric, plugin, adapter, rootNode, driver, openInStudio } = this
|
|
105
|
-
|
|
106
|
-
const root = path.resolve(config.root, config.output.path)
|
|
107
|
-
const mode = getMode(path.resolve(root, output.path))
|
|
108
|
-
|
|
109
|
-
if (adapter) {
|
|
110
|
-
await openInStudio({ ast: true })
|
|
111
|
-
|
|
112
|
-
await walk(
|
|
113
|
-
rootNode,
|
|
114
|
-
{
|
|
115
|
-
async schema(schemaNode) {
|
|
116
|
-
const writeTasks = generators.map(async (generator) => {
|
|
117
|
-
if (generator.type === 'react' && generator.version === '2') {
|
|
118
|
-
const options = resolverTs.resolveOptions(schemaNode, { options: plugin.options, exclude, include, override })
|
|
119
|
-
|
|
120
|
-
if (options === null) {
|
|
121
|
-
return
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
await buildSchema(schemaNode, {
|
|
125
|
-
options,
|
|
126
|
-
adapter,
|
|
127
|
-
config,
|
|
128
|
-
fabric,
|
|
129
|
-
Component: generator.Schema,
|
|
130
|
-
plugin,
|
|
131
|
-
driver,
|
|
132
|
-
mode,
|
|
133
|
-
version: generator.version,
|
|
134
|
-
})
|
|
135
|
-
}
|
|
136
|
-
})
|
|
137
|
-
|
|
138
|
-
await Promise.all(writeTasks)
|
|
139
|
-
},
|
|
140
|
-
async operation(operationNode) {
|
|
141
|
-
const writeTasks = generators.map(async (generator) => {
|
|
142
|
-
if (generator.type === 'react' && generator.version === '2') {
|
|
143
|
-
const options = resolverTs.resolveOptions(operationNode, { options: plugin.options, exclude, include, override })
|
|
144
|
-
|
|
145
|
-
if (options === null) {
|
|
146
|
-
return
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
await buildOperation(operationNode, {
|
|
150
|
-
options,
|
|
151
|
-
adapter,
|
|
152
|
-
config,
|
|
153
|
-
fabric,
|
|
154
|
-
Component: generator.Operation,
|
|
155
|
-
plugin,
|
|
156
|
-
driver,
|
|
157
|
-
mode,
|
|
158
|
-
version: generator.version,
|
|
159
|
-
})
|
|
160
|
-
}
|
|
161
|
-
})
|
|
162
|
-
|
|
163
|
-
await Promise.all(writeTasks)
|
|
164
|
-
},
|
|
165
|
-
},
|
|
166
|
-
{ depth: 'shallow' },
|
|
167
|
-
)
|
|
168
|
-
|
|
169
|
-
const barrelFiles = await getBarrelFiles(this.fabric.files, {
|
|
170
|
-
type: output.barrelType ?? 'named',
|
|
171
|
-
root,
|
|
62
|
+
options,
|
|
63
|
+
hooks: {
|
|
64
|
+
'kubb:plugin:setup'(ctx) {
|
|
65
|
+
ctx.setOptions({
|
|
172
66
|
output,
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
67
|
+
exclude,
|
|
68
|
+
include,
|
|
69
|
+
override,
|
|
70
|
+
optionalType,
|
|
71
|
+
group: groupConfig,
|
|
72
|
+
arrayType,
|
|
73
|
+
enumType,
|
|
74
|
+
enumTypeSuffix,
|
|
75
|
+
enumKeyCasing,
|
|
76
|
+
syntaxType,
|
|
77
|
+
paramsCasing,
|
|
78
|
+
printer,
|
|
176
79
|
})
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
const schemaGenerator = new SchemaGenerator(this.plugin.options, {
|
|
188
|
-
fabric: this.fabric,
|
|
189
|
-
oas,
|
|
190
|
-
driver: this.driver,
|
|
191
|
-
events: this.events,
|
|
192
|
-
plugin: this.plugin,
|
|
193
|
-
contentType,
|
|
194
|
-
include: undefined,
|
|
195
|
-
override,
|
|
196
|
-
mode,
|
|
197
|
-
output: output.path,
|
|
198
|
-
})
|
|
199
|
-
|
|
200
|
-
const schemaFiles = await schemaGenerator.build(...generators)
|
|
201
|
-
await this.upsertFile(...schemaFiles)
|
|
202
|
-
|
|
203
|
-
const operationGenerator = new OperationGenerator(this.plugin.options, {
|
|
204
|
-
fabric: this.fabric,
|
|
205
|
-
oas,
|
|
206
|
-
driver: this.driver,
|
|
207
|
-
events: this.events,
|
|
208
|
-
plugin: this.plugin,
|
|
209
|
-
contentType,
|
|
210
|
-
exclude,
|
|
211
|
-
include,
|
|
212
|
-
override,
|
|
213
|
-
mode,
|
|
214
|
-
UNSTABLE_NAMING,
|
|
215
|
-
})
|
|
216
|
-
|
|
217
|
-
const operationFiles = await operationGenerator.build(...generators)
|
|
218
|
-
await this.upsertFile(...operationFiles)
|
|
219
|
-
|
|
220
|
-
const barrelFiles = await getBarrelFiles(this.fabric.files, {
|
|
221
|
-
type: output.barrelType ?? 'named',
|
|
222
|
-
root,
|
|
223
|
-
output,
|
|
224
|
-
meta: {
|
|
225
|
-
pluginName: this.plugin.name,
|
|
226
|
-
},
|
|
227
|
-
})
|
|
228
|
-
|
|
229
|
-
await this.upsertFile(...barrelFiles)
|
|
80
|
+
ctx.setResolver(userResolver ? { ...resolverTs, ...userResolver } : resolverTs)
|
|
81
|
+
if (userTransformer) {
|
|
82
|
+
ctx.setTransformer(userTransformer)
|
|
83
|
+
}
|
|
84
|
+
ctx.addGenerator(typeGenerator)
|
|
85
|
+
for (const gen of userGenerators) {
|
|
86
|
+
ctx.addGenerator(gen)
|
|
87
|
+
}
|
|
88
|
+
},
|
|
230
89
|
},
|
|
231
90
|
}
|
|
232
91
|
})
|
|
92
|
+
|
|
93
|
+
export default pluginTs
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { ast } from '@kubb/core'
|
|
2
|
+
import { PARAM_RANK } from '../constants.ts'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Maps each function-printer handler key to its concrete node type.
|
|
6
|
+
*/
|
|
7
|
+
export type FunctionNodeByType = {
|
|
8
|
+
functionParameter: ast.FunctionParameterNode
|
|
9
|
+
parameterGroup: ast.ParameterGroupNode
|
|
10
|
+
functionParameters: ast.FunctionParametersNode
|
|
11
|
+
paramsType: ast.ParamsTypeNode
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const kindToHandlerKey = {
|
|
15
|
+
FunctionParameter: 'functionParameter',
|
|
16
|
+
ParameterGroup: 'parameterGroup',
|
|
17
|
+
FunctionParameters: 'functionParameters',
|
|
18
|
+
ParamsType: 'paramsType',
|
|
19
|
+
} satisfies Record<string, ast.FunctionNodeType>
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Creates a function-parameter printer factory.
|
|
23
|
+
*
|
|
24
|
+
* Uses `createPrinterFactory` and dispatches handlers by `node.kind`
|
|
25
|
+
* (for function nodes) rather than by `node.type` (for schema nodes).
|
|
26
|
+
*/
|
|
27
|
+
export const defineFunctionPrinter = ast.createPrinterFactory<ast.FunctionParamNode, ast.FunctionNodeType, FunctionNodeByType>(
|
|
28
|
+
(node) => kindToHandlerKey[node.kind],
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
export type FunctionPrinterOptions = {
|
|
32
|
+
/**
|
|
33
|
+
* Rendering modes supported by `functionPrinter`.
|
|
34
|
+
*
|
|
35
|
+
* | Mode | Output example | Use case |
|
|
36
|
+
* |---------------|---------------------------------------------|---------------------------------|
|
|
37
|
+
* | `declaration` | `id: string, config: Config = {}` | Function parameter declaration |
|
|
38
|
+
* | `call` | `id, { method, url }` | Function call arguments |
|
|
39
|
+
* | `keys` | `{ id, config }` | Key names only (destructuring) |
|
|
40
|
+
* | `values` | `{ id: id, config: config }` | Key/value object entries |
|
|
41
|
+
*/
|
|
42
|
+
mode: 'declaration' | 'call' | 'keys' | 'values'
|
|
43
|
+
/**
|
|
44
|
+
* Optional transformation applied to every parameter name before printing.
|
|
45
|
+
*/
|
|
46
|
+
transformName?: (name: string) => string
|
|
47
|
+
/**
|
|
48
|
+
* Optional transformation applied to every type string before printing.
|
|
49
|
+
*/
|
|
50
|
+
transformType?: (type: string) => string
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
type DefaultPrinter = ast.PrinterFactoryOptions<'functionParameters', FunctionPrinterOptions, string>
|
|
54
|
+
|
|
55
|
+
function rank(param: ast.FunctionParameterNode | ast.ParameterGroupNode): number {
|
|
56
|
+
if (param.kind === 'ParameterGroup') {
|
|
57
|
+
if (param.default) return PARAM_RANK.withDefault
|
|
58
|
+
const isOptional = param.optional ?? param.properties.every((p) => p.optional || p.default !== undefined)
|
|
59
|
+
return isOptional ? PARAM_RANK.optional : PARAM_RANK.required
|
|
60
|
+
}
|
|
61
|
+
if (param.rest) return PARAM_RANK.rest
|
|
62
|
+
if (param.default) return PARAM_RANK.withDefault
|
|
63
|
+
return param.optional ? PARAM_RANK.optional : PARAM_RANK.required
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function sortParams(params: ReadonlyArray<ast.FunctionParameterNode | ast.ParameterGroupNode>): Array<ast.FunctionParameterNode | ast.ParameterGroupNode> {
|
|
67
|
+
return [...params].sort((a, b) => rank(a) - rank(b))
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function sortChildParams(params: Array<ast.FunctionParameterNode>): Array<ast.FunctionParameterNode> {
|
|
71
|
+
return [...params].sort((a, b) => rank(a) - rank(b))
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Default function-signature printer.
|
|
76
|
+
* Covers the four standard output modes used across Kubb plugins.
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```ts
|
|
80
|
+
* const printer = functionPrinter({ mode: 'declaration' })
|
|
81
|
+
*
|
|
82
|
+
* const sig = createFunctionParameters({
|
|
83
|
+
* params: [
|
|
84
|
+
* createFunctionParameter({ name: 'petId', type: 'string', optional: false }),
|
|
85
|
+
* createFunctionParameter({ name: 'config', type: 'Config', optional: false, default: '{}' }),
|
|
86
|
+
* ],
|
|
87
|
+
* })
|
|
88
|
+
*
|
|
89
|
+
* printer.print(sig) // → "petId: string, config: Config = {}"
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
export const functionPrinter = defineFunctionPrinter<DefaultPrinter>((options) => ({
|
|
93
|
+
name: 'functionParameters',
|
|
94
|
+
options,
|
|
95
|
+
nodes: {
|
|
96
|
+
paramsType(node) {
|
|
97
|
+
if (node.kind !== 'ParamsType') return null
|
|
98
|
+
if (node.variant === 'member') {
|
|
99
|
+
return `${node.base}['${node.key}']`
|
|
100
|
+
}
|
|
101
|
+
if (node.variant === 'struct') {
|
|
102
|
+
const parts = node.properties.map((p) => {
|
|
103
|
+
const typeStr = this.transform(p.type)
|
|
104
|
+
const key = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(p.name) ? p.name : JSON.stringify(p.name)
|
|
105
|
+
return p.optional ? `${key}?: ${typeStr}` : `${key}: ${typeStr}`
|
|
106
|
+
})
|
|
107
|
+
return `{ ${parts.join('; ')} }`
|
|
108
|
+
}
|
|
109
|
+
if (node.variant === 'reference') {
|
|
110
|
+
return node.name
|
|
111
|
+
}
|
|
112
|
+
return null
|
|
113
|
+
},
|
|
114
|
+
functionParameter(node) {
|
|
115
|
+
const { mode, transformName, transformType } = this.options
|
|
116
|
+
const name = transformName ? transformName(node.name) : node.name
|
|
117
|
+
|
|
118
|
+
const rawType = node.type ? this.transform(node.type) : undefined
|
|
119
|
+
const type = rawType != null && transformType ? transformType(rawType) : rawType
|
|
120
|
+
|
|
121
|
+
if (mode === 'keys' || mode === 'values') {
|
|
122
|
+
return node.rest ? `...${name}` : name
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (mode === 'call') {
|
|
126
|
+
return node.rest ? `...${name}` : name
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (node.rest) {
|
|
130
|
+
return type ? `...${name}: ${type}` : `...${name}`
|
|
131
|
+
}
|
|
132
|
+
if (type) {
|
|
133
|
+
if (node.optional) return `${name}?: ${type}`
|
|
134
|
+
return node.default ? `${name}: ${type} = ${node.default}` : `${name}: ${type}`
|
|
135
|
+
}
|
|
136
|
+
return node.default ? `${name} = ${node.default}` : name
|
|
137
|
+
},
|
|
138
|
+
parameterGroup(node) {
|
|
139
|
+
const { mode, transformName, transformType } = this.options
|
|
140
|
+
const sorted = sortChildParams(node.properties)
|
|
141
|
+
const isOptional = node.optional ?? sorted.every((p) => p.optional || p.default !== undefined)
|
|
142
|
+
|
|
143
|
+
if (node.inline) {
|
|
144
|
+
return sorted
|
|
145
|
+
.map((p) => this.transform(p))
|
|
146
|
+
.filter(Boolean)
|
|
147
|
+
.join(', ')
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (mode === 'keys' || mode === 'values') {
|
|
151
|
+
const keys = sorted.map((p) => p.name).join(', ')
|
|
152
|
+
return `{ ${keys} }`
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (mode === 'call') {
|
|
156
|
+
const keys = sorted.map((p) => p.name).join(', ')
|
|
157
|
+
return `{ ${keys} }`
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const names = sorted.map((p) => {
|
|
161
|
+
const n = transformName ? transformName(p.name) : p.name
|
|
162
|
+
|
|
163
|
+
return n
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
const nameStr = names.length ? `{ ${names.join(', ')} }` : undefined
|
|
167
|
+
if (!nameStr) return null
|
|
168
|
+
|
|
169
|
+
let typeAnnotation: string | undefined = node.type ? (this.transform(node.type) ?? undefined) : undefined
|
|
170
|
+
if (!typeAnnotation) {
|
|
171
|
+
const typeParts = sorted
|
|
172
|
+
.filter((p) => p.type)
|
|
173
|
+
.map((p) => {
|
|
174
|
+
const rawT = p.type ? this.transform(p.type) : undefined
|
|
175
|
+
const t = rawT != null && transformType ? transformType(rawT) : rawT
|
|
176
|
+
return p.optional || p.default !== undefined ? `${p.name}?: ${t}` : `${p.name}: ${t}`
|
|
177
|
+
})
|
|
178
|
+
typeAnnotation = typeParts.length ? `{ ${typeParts.join('; ')} }` : undefined
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (typeAnnotation) {
|
|
182
|
+
if (isOptional) return `${nameStr}: ${typeAnnotation} = ${node.default ?? '{}'}`
|
|
183
|
+
return node.default ? `${nameStr}: ${typeAnnotation} = ${node.default}` : `${nameStr}: ${typeAnnotation}`
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
return node.default ? `${nameStr} = ${node.default}` : nameStr
|
|
187
|
+
},
|
|
188
|
+
functionParameters(node) {
|
|
189
|
+
const sorted = sortParams(node.params)
|
|
190
|
+
|
|
191
|
+
return sorted
|
|
192
|
+
.map((p) => this.transform(p))
|
|
193
|
+
.filter(Boolean)
|
|
194
|
+
.join(', ')
|
|
195
|
+
},
|
|
196
|
+
},
|
|
197
|
+
}))
|