@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.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`.
@@ -1085,11 +1208,19 @@ const typeGenerator = (0, _kubb_core.defineGenerator)({
1085
1208
  meta: meta.file.meta,
1086
1209
  banner: resolver.resolveBanner(ctx.meta, {
1087
1210
  output,
1088
- config
1211
+ config,
1212
+ file: {
1213
+ path: meta.file.path,
1214
+ baseName: meta.file.baseName
1215
+ }
1089
1216
  }),
1090
1217
  footer: resolver.resolveFooter(ctx.meta, {
1091
1218
  output,
1092
- config
1219
+ config,
1220
+ file: {
1221
+ path: meta.file.path,
1222
+ baseName: meta.file.baseName
1223
+ }
1093
1224
  }),
1094
1225
  children: [mode === "split" && imports.map((imp) => /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
1095
1226
  root: meta.file.path,
@@ -1267,11 +1398,19 @@ const typeGenerator = (0, _kubb_core.defineGenerator)({
1267
1398
  meta: meta.file.meta,
1268
1399
  banner: resolver.resolveBanner(ctx.meta, {
1269
1400
  output,
1270
- config
1401
+ config,
1402
+ file: {
1403
+ path: meta.file.path,
1404
+ baseName: meta.file.baseName
1405
+ }
1271
1406
  }),
1272
1407
  footer: resolver.resolveFooter(ctx.meta, {
1273
1408
  output,
1274
- config
1409
+ config,
1410
+ file: {
1411
+ path: meta.file.path,
1412
+ baseName: meta.file.baseName
1413
+ }
1275
1414
  }),
1276
1415
  children: [
1277
1416
  paramTypes,
@@ -1309,13 +1448,15 @@ const resolverTs = (0, _kubb_core.defineResolver)(() => {
1309
1448
  name: "default",
1310
1449
  pluginName: "plugin-ts",
1311
1450
  default(name, type) {
1312
- return pascalCase(name, { isFile: type === "file" });
1451
+ const resolved = pascalCase(name, { isFile: type === "file" });
1452
+ return type === "file" ? resolved : ensureValidVarName(resolved);
1313
1453
  },
1314
1454
  resolveTypeName(name) {
1315
- return pascalCase(name);
1455
+ return ensureValidVarName(pascalCase(name));
1316
1456
  },
1317
1457
  resolvePathName(name, type) {
1318
- return pascalCase(name, { isFile: type === "file" });
1458
+ const resolved = pascalCase(name, { isFile: type === "file" });
1459
+ return type === "file" ? resolved : ensureValidVarName(resolved);
1319
1460
  },
1320
1461
  resolveParamName(node, param) {
1321
1462
  return this.resolveTypeName(`${node.operationId} ${param.in} ${param.name}`);