@kubb/plugin-ts 5.0.0-beta.85 → 5.0.0-beta.86

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
@@ -192,7 +192,7 @@ const reservedWords = /* @__PURE__ */ new Set([
192
192
  */
193
193
  function isValidVarName(name) {
194
194
  if (!name || reservedWords.has(name)) return false;
195
- return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name);
195
+ return isIdentifier(name);
196
196
  }
197
197
  /**
198
198
  * Returns `name` when it's a syntactically valid JavaScript variable name,
@@ -214,6 +214,22 @@ function ensureValidVarName(name) {
214
214
  if (!name || isValidVarName(name)) return name;
215
215
  return `_${name}`;
216
216
  }
217
+ /**
218
+ * Returns `true` when `name` is syntactically a valid identifier, ignoring reserved words.
219
+ *
220
+ * Reserved words and globals (`class`, `name`, `Date`, …) are valid as bare object-literal keys
221
+ * even though they are not valid variable names, so use this (not {@link isValidVarName}) when
222
+ * deciding whether an object key needs quoting.
223
+ *
224
+ * @example
225
+ * ```ts
226
+ * isIdentifier('name') // true
227
+ * isIdentifier('x-total')// false
228
+ * ```
229
+ */
230
+ function isIdentifier(name) {
231
+ return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name);
232
+ }
217
233
  //#endregion
218
234
  //#region ../../internals/utils/src/strings.ts
219
235
  /**
@@ -1413,6 +1429,15 @@ const printerTs = ast.createPrinter((options) => {
1413
1429
  //#endregion
1414
1430
  //#region src/generators/typeGenerator.tsx
1415
1431
  /**
1432
+ * Resolves the imported type name for a referenced schema. An enum schema emitted
1433
+ * as a `const` object imports its suffixed key alias (e.g. `StatusKey`); every other
1434
+ * schema imports its plain resolved name.
1435
+ */
1436
+ function resolveImportName({ schemaName, enumOptions, enumSchemaNames, resolver }) {
1437
+ if (ENUM_TYPES_WITH_KEY_SUFFIX.has(enumOptions.type) && enumOptions.typeSuffix && enumSchemaNames.has(schemaName)) return resolver.enum.keyName({ name: schemaName }, enumOptions.typeSuffix);
1438
+ return resolver.name(schemaName);
1439
+ }
1440
+ /**
1416
1441
  * Built-in generator for `@kubb/plugin-ts`. Emits one TypeScript file per
1417
1442
  * schema in the spec plus per-operation request, response, and parameter
1418
1443
  * types. Drop-replace with a custom `Generator<PluginTs>` to change how
@@ -1426,12 +1451,13 @@ const typeGenerator = defineGenerator({
1426
1451
  const { adapter, config, resolver, root } = ctx;
1427
1452
  if (!node.name) return;
1428
1453
  const enumSchemaNames = new Set(ctx.meta.enumNames);
1429
- function resolveImportName(schemaName) {
1430
- if (ENUM_TYPES_WITH_KEY_SUFFIX.has(enumOptions.type) && enumOptions.typeSuffix && enumSchemaNames.has(schemaName)) return resolver.enum.keyName({ name: schemaName }, enumOptions.typeSuffix);
1431
- return resolver.name(schemaName);
1432
- }
1433
1454
  const imports = adapter.getImports(node, (schemaName) => ({
1434
- name: resolveImportName(schemaName),
1455
+ name: resolveImportName({
1456
+ schemaName,
1457
+ enumOptions,
1458
+ enumSchemaNames,
1459
+ resolver
1460
+ }),
1435
1461
  path: resolver.file({
1436
1462
  name: schemaName,
1437
1463
  extname: ".ts",
@@ -1516,14 +1542,15 @@ const typeGenerator = defineGenerator({
1516
1542
  group: group ?? void 0
1517
1543
  }) };
1518
1544
  const enumSchemaNames = new Set(ctx.meta.enumNames);
1519
- function resolveImportName(schemaName) {
1520
- if (ENUM_TYPES_WITH_KEY_SUFFIX.has(enumOptions.type) && enumOptions.typeSuffix && enumSchemaNames.has(schemaName)) return resolver.enum.keyName({ name: schemaName }, enumOptions.typeSuffix);
1521
- return resolver.name(schemaName);
1522
- }
1523
1545
  function renderSchemaType({ schema, name, keysToOmit }) {
1524
1546
  if (!schema) return null;
1525
1547
  const imports = adapter.getImports(schema, (schemaName) => ({
1526
- name: resolveImportName(schemaName),
1548
+ name: resolveImportName({
1549
+ schemaName,
1550
+ enumOptions,
1551
+ enumSchemaNames,
1552
+ resolver
1553
+ }),
1527
1554
  path: resolver.file({
1528
1555
  name: schemaName,
1529
1556
  extname: ".ts",
@@ -1652,7 +1679,12 @@ const typeGenerator = defineGenerator({
1652
1679
  const responseName = resolver.response.response(node);
1653
1680
  const responsesWithSchema = node.responses.filter(hasSchema);
1654
1681
  if (new Set(responsesWithSchema.flatMap((res) => (res.content ?? []).flatMap((entry) => entry.schema ? adapter.getImports(entry.schema, (schemaName) => ({
1655
- name: resolveImportName(schemaName),
1682
+ name: resolveImportName({
1683
+ schemaName,
1684
+ enumOptions,
1685
+ enumSchemaNames,
1686
+ resolver
1687
+ }),
1656
1688
  path: ""
1657
1689
  })).flatMap((imp) => Array.isArray(imp.name) ? imp.name : [imp.name]) : []))).has(responseName)) return null;
1658
1690
  return renderSchemaType({