@kubb/plugin-ts 5.0.0-beta.25 → 5.0.0-beta.28

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.js CHANGED
@@ -141,6 +141,129 @@ function stringify(value) {
141
141
  return JSON.stringify(trimQuotes(value.toString()));
142
142
  }
143
143
  //#endregion
144
+ //#region ../../internals/utils/src/reserved.ts
145
+ /**
146
+ * JavaScript and Java reserved words.
147
+ * @link https://github.com/jonschlinkert/reserved/blob/master/index.js
148
+ */
149
+ const reservedWords = new Set([
150
+ "abstract",
151
+ "arguments",
152
+ "boolean",
153
+ "break",
154
+ "byte",
155
+ "case",
156
+ "catch",
157
+ "char",
158
+ "class",
159
+ "const",
160
+ "continue",
161
+ "debugger",
162
+ "default",
163
+ "delete",
164
+ "do",
165
+ "double",
166
+ "else",
167
+ "enum",
168
+ "eval",
169
+ "export",
170
+ "extends",
171
+ "false",
172
+ "final",
173
+ "finally",
174
+ "float",
175
+ "for",
176
+ "function",
177
+ "goto",
178
+ "if",
179
+ "implements",
180
+ "import",
181
+ "in",
182
+ "instanceof",
183
+ "int",
184
+ "interface",
185
+ "let",
186
+ "long",
187
+ "native",
188
+ "new",
189
+ "null",
190
+ "package",
191
+ "private",
192
+ "protected",
193
+ "public",
194
+ "return",
195
+ "short",
196
+ "static",
197
+ "super",
198
+ "switch",
199
+ "synchronized",
200
+ "this",
201
+ "throw",
202
+ "throws",
203
+ "transient",
204
+ "true",
205
+ "try",
206
+ "typeof",
207
+ "var",
208
+ "void",
209
+ "volatile",
210
+ "while",
211
+ "with",
212
+ "yield",
213
+ "Array",
214
+ "Date",
215
+ "hasOwnProperty",
216
+ "Infinity",
217
+ "isFinite",
218
+ "isNaN",
219
+ "isPrototypeOf",
220
+ "length",
221
+ "Math",
222
+ "name",
223
+ "NaN",
224
+ "Number",
225
+ "Object",
226
+ "prototype",
227
+ "String",
228
+ "toString",
229
+ "undefined",
230
+ "valueOf"
231
+ ]);
232
+ /**
233
+ * Returns `true` when `name` is a syntactically valid JavaScript variable name.
234
+ *
235
+ * @example
236
+ * ```ts
237
+ * isValidVarName('status') // true
238
+ * isValidVarName('class') // false (reserved word)
239
+ * isValidVarName('42foo') // false (starts with digit)
240
+ * ```
241
+ */
242
+ function isValidVarName(name) {
243
+ if (!name || reservedWords.has(name)) return false;
244
+ return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name);
245
+ }
246
+ /**
247
+ * Returns `name` when it's a syntactically valid JavaScript variable name,
248
+ * otherwise prefixes it with `_` so the result is a valid identifier.
249
+ *
250
+ * Useful for sanitizing OpenAPI schema names or operation IDs that start with
251
+ * a digit (e.g. `409`, `504AccountCancel`) before using them as exported
252
+ * variable, type, or function names.
253
+ *
254
+ * @example
255
+ * ```ts
256
+ * ensureValidVarName('409') // '_409'
257
+ * ensureValidVarName('504AccountCancel') // '_504AccountCancel'
258
+ * ensureValidVarName('Pet') // 'Pet'
259
+ * ensureValidVarName('class') // '_class'
260
+ * ```
261
+ */
262
+ function ensureValidVarName(name) {
263
+ if (!name || isValidVarName(name)) return name;
264
+ return `_${name}`;
265
+ }
266
+ //#endregion
144
267
  //#region src/constants.ts
145
268
  /**
146
269
  * `optionalType` values that cause a property's type to include `| undefined`.
@@ -1055,11 +1178,19 @@ const typeGenerator = defineGenerator({
1055
1178
  meta: meta.file.meta,
1056
1179
  banner: resolver.resolveBanner(ctx.meta, {
1057
1180
  output,
1058
- config
1181
+ config,
1182
+ file: {
1183
+ path: meta.file.path,
1184
+ baseName: meta.file.baseName
1185
+ }
1059
1186
  }),
1060
1187
  footer: resolver.resolveFooter(ctx.meta, {
1061
1188
  output,
1062
- config
1189
+ config,
1190
+ file: {
1191
+ path: meta.file.path,
1192
+ baseName: meta.file.baseName
1193
+ }
1063
1194
  }),
1064
1195
  children: [mode === "split" && imports.map((imp) => /* @__PURE__ */ jsx(File.Import, {
1065
1196
  root: meta.file.path,
@@ -1237,11 +1368,19 @@ const typeGenerator = defineGenerator({
1237
1368
  meta: meta.file.meta,
1238
1369
  banner: resolver.resolveBanner(ctx.meta, {
1239
1370
  output,
1240
- config
1371
+ config,
1372
+ file: {
1373
+ path: meta.file.path,
1374
+ baseName: meta.file.baseName
1375
+ }
1241
1376
  }),
1242
1377
  footer: resolver.resolveFooter(ctx.meta, {
1243
1378
  output,
1244
- config
1379
+ config,
1380
+ file: {
1381
+ path: meta.file.path,
1382
+ baseName: meta.file.baseName
1383
+ }
1245
1384
  }),
1246
1385
  children: [
1247
1386
  paramTypes,
@@ -1279,13 +1418,15 @@ const resolverTs = defineResolver(() => {
1279
1418
  name: "default",
1280
1419
  pluginName: "plugin-ts",
1281
1420
  default(name, type) {
1282
- return pascalCase(name, { isFile: type === "file" });
1421
+ const resolved = pascalCase(name, { isFile: type === "file" });
1422
+ return type === "file" ? resolved : ensureValidVarName(resolved);
1283
1423
  },
1284
1424
  resolveTypeName(name) {
1285
- return pascalCase(name);
1425
+ return ensureValidVarName(pascalCase(name));
1286
1426
  },
1287
1427
  resolvePathName(name, type) {
1288
- return pascalCase(name, { isFile: type === "file" });
1428
+ const resolved = pascalCase(name, { isFile: type === "file" });
1429
+ return type === "file" ? resolved : ensureValidVarName(resolved);
1289
1430
  },
1290
1431
  resolveParamName(node, param) {
1291
1432
  return this.resolveTypeName(`${node.operationId} ${param.in} ${param.name}`);