@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.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`.
@@ -1279,13 +1402,15 @@ const resolverTs = defineResolver(() => {
1279
1402
  name: "default",
1280
1403
  pluginName: "plugin-ts",
1281
1404
  default(name, type) {
1282
- return pascalCase(name, { isFile: type === "file" });
1405
+ const resolved = pascalCase(name, { isFile: type === "file" });
1406
+ return type === "file" ? resolved : ensureValidVarName(resolved);
1283
1407
  },
1284
1408
  resolveTypeName(name) {
1285
- return pascalCase(name);
1409
+ return ensureValidVarName(pascalCase(name));
1286
1410
  },
1287
1411
  resolvePathName(name, type) {
1288
- return pascalCase(name, { isFile: type === "file" });
1412
+ const resolved = pascalCase(name, { isFile: type === "file" });
1413
+ return type === "file" ? resolved : ensureValidVarName(resolved);
1289
1414
  },
1290
1415
  resolveParamName(node, param) {
1291
1416
  return this.resolveTypeName(`${node.operationId} ${param.in} ${param.name}`);