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

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 CHANGED
@@ -171,6 +171,129 @@ function stringify(value) {
171
171
  return JSON.stringify(trimQuotes(value.toString()));
172
172
  }
173
173
  //#endregion
174
+ //#region ../../internals/utils/src/reserved.ts
175
+ /**
176
+ * JavaScript and Java reserved words.
177
+ * @link https://github.com/jonschlinkert/reserved/blob/master/index.js
178
+ */
179
+ const reservedWords = new Set([
180
+ "abstract",
181
+ "arguments",
182
+ "boolean",
183
+ "break",
184
+ "byte",
185
+ "case",
186
+ "catch",
187
+ "char",
188
+ "class",
189
+ "const",
190
+ "continue",
191
+ "debugger",
192
+ "default",
193
+ "delete",
194
+ "do",
195
+ "double",
196
+ "else",
197
+ "enum",
198
+ "eval",
199
+ "export",
200
+ "extends",
201
+ "false",
202
+ "final",
203
+ "finally",
204
+ "float",
205
+ "for",
206
+ "function",
207
+ "goto",
208
+ "if",
209
+ "implements",
210
+ "import",
211
+ "in",
212
+ "instanceof",
213
+ "int",
214
+ "interface",
215
+ "let",
216
+ "long",
217
+ "native",
218
+ "new",
219
+ "null",
220
+ "package",
221
+ "private",
222
+ "protected",
223
+ "public",
224
+ "return",
225
+ "short",
226
+ "static",
227
+ "super",
228
+ "switch",
229
+ "synchronized",
230
+ "this",
231
+ "throw",
232
+ "throws",
233
+ "transient",
234
+ "true",
235
+ "try",
236
+ "typeof",
237
+ "var",
238
+ "void",
239
+ "volatile",
240
+ "while",
241
+ "with",
242
+ "yield",
243
+ "Array",
244
+ "Date",
245
+ "hasOwnProperty",
246
+ "Infinity",
247
+ "isFinite",
248
+ "isNaN",
249
+ "isPrototypeOf",
250
+ "length",
251
+ "Math",
252
+ "name",
253
+ "NaN",
254
+ "Number",
255
+ "Object",
256
+ "prototype",
257
+ "String",
258
+ "toString",
259
+ "undefined",
260
+ "valueOf"
261
+ ]);
262
+ /**
263
+ * Returns `true` when `name` is a syntactically valid JavaScript variable name.
264
+ *
265
+ * @example
266
+ * ```ts
267
+ * isValidVarName('status') // true
268
+ * isValidVarName('class') // false (reserved word)
269
+ * isValidVarName('42foo') // false (starts with digit)
270
+ * ```
271
+ */
272
+ function isValidVarName(name) {
273
+ if (!name || reservedWords.has(name)) return false;
274
+ return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name);
275
+ }
276
+ /**
277
+ * Returns `name` when it's a syntactically valid JavaScript variable name,
278
+ * otherwise prefixes it with `_` so the result is a valid identifier.
279
+ *
280
+ * Useful for sanitizing OpenAPI schema names or operation IDs that start with
281
+ * a digit (e.g. `409`, `504AccountCancel`) before using them as exported
282
+ * variable, type, or function names.
283
+ *
284
+ * @example
285
+ * ```ts
286
+ * ensureValidVarName('409') // '_409'
287
+ * ensureValidVarName('504AccountCancel') // '_504AccountCancel'
288
+ * ensureValidVarName('Pet') // 'Pet'
289
+ * ensureValidVarName('class') // '_class'
290
+ * ```
291
+ */
292
+ function ensureValidVarName(name) {
293
+ if (!name || isValidVarName(name)) return name;
294
+ return `_${name}`;
295
+ }
296
+ //#endregion
174
297
  //#region src/constants.ts
175
298
  /**
176
299
  * `optionalType` values that cause a property's type to include `| undefined`.
@@ -1309,13 +1432,15 @@ const resolverTs = (0, _kubb_core.defineResolver)(() => {
1309
1432
  name: "default",
1310
1433
  pluginName: "plugin-ts",
1311
1434
  default(name, type) {
1312
- return pascalCase(name, { isFile: type === "file" });
1435
+ const resolved = pascalCase(name, { isFile: type === "file" });
1436
+ return type === "file" ? resolved : ensureValidVarName(resolved);
1313
1437
  },
1314
1438
  resolveTypeName(name) {
1315
- return pascalCase(name);
1439
+ return ensureValidVarName(pascalCase(name));
1316
1440
  },
1317
1441
  resolvePathName(name, type) {
1318
- return pascalCase(name, { isFile: type === "file" });
1442
+ const resolved = pascalCase(name, { isFile: type === "file" });
1443
+ return type === "file" ? resolved : ensureValidVarName(resolved);
1319
1444
  },
1320
1445
  resolveParamName(node, param) {
1321
1446
  return this.resolveTypeName(`${node.operationId} ${param.in} ${param.name}`);