svelte-origin 1.0.0-next.23 → 1.0.0-next.24

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.

Potentially problematic release.


This version of svelte-origin might be problematic. Click here for more details.

@@ -0,0 +1,87 @@
1
+ /**
2
+ * Reserved keyword handling for generated code.
3
+ *
4
+ * JavaScript/TypeScript reserved keywords cannot be used as bare identifiers
5
+ * in destructuring patterns. When a prop name is a reserved keyword (like 'class'),
6
+ * we need to use a renamed internal variable.
7
+ *
8
+ * Example:
9
+ * ```ts
10
+ * // This is INVALID:
11
+ * let { class = '' } = $props()
12
+ *
13
+ * // This is VALID:
14
+ * let { class: ___class = '' } = $props()
15
+ * ```
16
+ */
17
+ /**
18
+ * Check if a string is a reserved keyword that needs renaming
19
+ */
20
+ export declare function isReservedKeyword(name: string): boolean;
21
+ /**
22
+ * Get the internal variable name for a prop.
23
+ *
24
+ * For reserved keywords and collision prevention, we prefix with `___`.
25
+ * This ensures the generated code is valid JavaScript.
26
+ *
27
+ * @param propName The original prop name
28
+ * @param forcePrefix If true, always add prefix (for collision prevention)
29
+ * @returns The internal variable name to use
30
+ *
31
+ * @example
32
+ * getInternalVarName('class') // '___class'
33
+ * getInternalVarName('value') // '___value'
34
+ * getInternalVarName('count', true) // '___count'
35
+ */
36
+ export declare function getInternalVarName(propName: string, forcePrefix?: boolean): string;
37
+ /**
38
+ * Generate the prop destructuring part for a single prop.
39
+ * Handles reserved keywords by using property renaming.
40
+ *
41
+ * @param propName The original prop name
42
+ * @param defaultValue The default value expression (already formatted)
43
+ * @returns The destructuring part for this prop
44
+ *
45
+ * @example
46
+ * // Reserved keyword
47
+ * generatePropDestructure('class', "''")
48
+ * // Returns: "class: ___class = ''"
49
+ *
50
+ * // Normal prop
51
+ * generatePropDestructure('value', '$bindable()')
52
+ * // Returns: "value: ___value = $bindable()"
53
+ */
54
+ export declare function generatePropDestructure(propName: string, defaultValue: string): string;
55
+ /**
56
+ * Generate the prop destructuring part for a prop without a default value.
57
+ *
58
+ * @param propName The original prop name
59
+ * @returns The destructuring part for this prop
60
+ *
61
+ * @example
62
+ * generatePropDestructureNoDefault('class')
63
+ * // Returns: "class: ___class"
64
+ */
65
+ export declare function generatePropDestructureNoDefault(propName: string): string;
66
+ /**
67
+ * Generate a getter for the reactive attrs wrapper.
68
+ *
69
+ * @param propName The original prop name
70
+ * @returns The getter definition
71
+ *
72
+ * @example
73
+ * generateGetter('class')
74
+ * // Returns: "get class() { return ___class }"
75
+ */
76
+ export declare function generateGetter(propName: string): string;
77
+ /**
78
+ * Generate a setter for the reactive attrs wrapper.
79
+ *
80
+ * @param propName The original prop name
81
+ * @returns The setter definition
82
+ *
83
+ * @example
84
+ * generateSetter('class')
85
+ * // Returns: "set class(v) { ___class = v }"
86
+ */
87
+ export declare function generateSetter(propName: string): string;
@@ -64,12 +64,16 @@ export declare function parseAttrsSchemaFromSource(source: string, exportName: s
64
64
  * to the factory type. This approach is required for svelte-check to properly
65
65
  * infer component props from the origin factory definition.
66
66
  *
67
+ * All props use internal variable names (prefixed with ___) to:
68
+ * 1. Avoid reserved keyword conflicts (e.g., 'class' -> 'class: ___class')
69
+ * 2. Prevent accidental naming collisions with user code
70
+ *
67
71
  * @example
68
72
  * generatePropsDestructuring([
69
- * { key: 'label', defaultValue: "'Counter'", bindable: false, hasDefault: true },
73
+ * { key: 'class', defaultValue: "''", bindable: false, hasDefault: true },
70
74
  * { key: 'count', defaultValue: '0', bindable: true, hasDefault: true }
71
75
  * ], 'Counter')
72
- * // Returns: "let { label = 'Counter', count = $bindable(0) }: $attrs.Of<typeof Counter> = $props()"
76
+ * // Returns: "let { class: ___class = '', count: ___count = $bindable(0) }: $attrs.Of<typeof Counter> = $props()"
73
77
  */
74
78
  export declare function generatePropsDestructuring(attrs: ParsedAttrInfo[], factoryName: string): string;
75
79
  /**
@@ -77,12 +81,15 @@ export declare function generatePropsDestructuring(attrs: ParsedAttrInfo[], fact
77
81
  * For bindable attrs, we create getters AND setters to maintain two-way binding.
78
82
  * For non-bindable attrs, we only create getters.
79
83
  *
84
+ * Getters/setters reference internal variable names (___key) that were
85
+ * destructured from $props().
86
+ *
80
87
  * @example
81
88
  * generateFactoryCallFromAttrs('Counter', [
82
- * { key: 'label', defaultValue: "'Counter'", bindable: false, hasDefault: true },
89
+ * { key: 'class', defaultValue: "''", bindable: false, hasDefault: true },
83
90
  * { key: 'count', defaultValue: '0', bindable: true, hasDefault: true }
84
91
  * ])
85
- * // Returns: "Counter({ get label() { return label }, get count() { return count }, set count(v) { count = v } })"
92
+ * // Returns: "Counter({ get class() { return ___class }, get count() { return ___count }, set count(v) { ___count = v } })"
86
93
  */
87
94
  export declare function generateFactoryCallFromAttrs(factoryName: string, attrs: ParsedAttrInfo[]): string;
88
95
  /**
package/dist/vite-dts.js CHANGED
@@ -200,6 +200,79 @@ function findMatchingBracket(source, openIndex, openChar = "(", closeChar = ")")
200
200
  return -1;
201
201
  }
202
202
 
203
+ // src/transform/reserved-keywords.ts
204
+ var RESERVED_KEYWORDS = new Set([
205
+ "break",
206
+ "case",
207
+ "catch",
208
+ "continue",
209
+ "debugger",
210
+ "default",
211
+ "delete",
212
+ "do",
213
+ "else",
214
+ "finally",
215
+ "for",
216
+ "function",
217
+ "if",
218
+ "in",
219
+ "instanceof",
220
+ "new",
221
+ "return",
222
+ "switch",
223
+ "this",
224
+ "throw",
225
+ "try",
226
+ "typeof",
227
+ "var",
228
+ "void",
229
+ "while",
230
+ "with",
231
+ "class",
232
+ "const",
233
+ "enum",
234
+ "export",
235
+ "extends",
236
+ "import",
237
+ "super",
238
+ "implements",
239
+ "interface",
240
+ "let",
241
+ "package",
242
+ "private",
243
+ "protected",
244
+ "public",
245
+ "static",
246
+ "yield",
247
+ "await",
248
+ "async",
249
+ "true",
250
+ "false",
251
+ "null",
252
+ "undefined",
253
+ "arguments",
254
+ "eval"
255
+ ]);
256
+ function getInternalVarName(propName, forcePrefix = true) {
257
+ return `___${propName}`;
258
+ }
259
+ function generatePropDestructure(propName, defaultValue) {
260
+ const internalName = getInternalVarName(propName);
261
+ return `${propName}: ${internalName} = ${defaultValue}`;
262
+ }
263
+ function generatePropDestructureNoDefault(propName) {
264
+ const internalName = getInternalVarName(propName);
265
+ return `${propName}: ${internalName}`;
266
+ }
267
+ function generateGetter(propName) {
268
+ const internalName = getInternalVarName(propName);
269
+ return `get ${propName}() { return ${internalName} }`;
270
+ }
271
+ function generateSetter(propName) {
272
+ const internalName = getInternalVarName(propName);
273
+ return `set ${propName}(v) { ${internalName} = v }`;
274
+ }
275
+
203
276
  // src/transform/schema.ts
204
277
  function parseOriginSchemaFromSource(source, exportName) {
205
278
  const sourceResult = parseSourceOrigin(source, exportName);
@@ -538,14 +611,14 @@ function generatePropsDestructuring(attrs, factoryName) {
538
611
  for (const attr of attrs) {
539
612
  if (attr.bindable) {
540
613
  if (attr.hasDefault) {
541
- parts.push(`${attr.key} = $bindable(${attr.defaultValue})`);
614
+ parts.push(generatePropDestructure(attr.key, `$bindable(${attr.defaultValue})`));
542
615
  } else {
543
- parts.push(`${attr.key} = $bindable()`);
616
+ parts.push(generatePropDestructure(attr.key, "$bindable()"));
544
617
  }
545
618
  } else if (attr.hasDefault) {
546
- parts.push(`${attr.key} = ${attr.defaultValue}`);
619
+ parts.push(generatePropDestructure(attr.key, attr.defaultValue));
547
620
  } else {
548
- parts.push(attr.key);
621
+ parts.push(generatePropDestructureNoDefault(attr.key));
549
622
  }
550
623
  }
551
624
  return `let { ${parts.join(", ")} }: $attrs.Of<typeof ${factoryName}> = $props()`;
@@ -553,9 +626,9 @@ function generatePropsDestructuring(attrs, factoryName) {
553
626
  function generateFactoryCallFromAttrs(factoryName, attrs) {
554
627
  const parts = [];
555
628
  for (const attr of attrs) {
556
- parts.push(`get ${attr.key}() { return ${attr.key} }`);
629
+ parts.push(generateGetter(attr.key));
557
630
  if (attr.bindable) {
558
- parts.push(`set ${attr.key}(v) { ${attr.key} = v }`);
631
+ parts.push(generateSetter(attr.key));
559
632
  }
560
633
  }
561
634
  return `${factoryName}({ ${parts.join(", ")} })`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte-origin",
3
- "version": "1.0.0-next.23",
3
+ "version": "1.0.0-next.24",
4
4
  "description": "Compiler-assisted state and prop ergonomics for Svelte 5",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",