@portel/photon-core 2.5.3 → 2.6.0

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/src/compiler.ts CHANGED
@@ -21,52 +21,94 @@ import * as crypto from 'crypto';
21
21
  * @returns Absolute path to the compiled .mjs file
22
22
  */
23
23
  /**
24
- * Transform array/map/set literals to constructor calls when using reactive imports
24
+ * Transform arrays to reactive collections for PhotonMCP classes
25
25
  *
26
- * When a file imports { Array } from '@portel/photon-core', transforms:
27
- * items: Array<Task> = []; → items: Array<Task> = new Array();
28
- * items = []; → items = new Array();
29
- * data: Map<K,V> = new Map(); → (unchanged, already correct)
26
+ * ZERO-EFFORT REACTIVITY: If a class extends PhotonMCP and has array properties,
27
+ * this transform automatically:
28
+ * 1. Injects `import { Array as ReactiveArray } from '@portel/photon-core'`
29
+ * 2. Transforms `= []` to `= new ReactiveArray()` for class properties
30
30
  *
31
- * Only transforms class properties (not local variables like const x = []).
32
- * This enables zero-effort reactivity where developers just import and use.
31
+ * Result: Developers write normal code, arrays are automatically reactive.
32
+ *
33
+ * ```typescript
34
+ * // Developer writes this (normal TypeScript):
35
+ * export default class TodoList extends PhotonMCP {
36
+ * items: Task[] = [];
37
+ * async add(text: string) { this.items.push({...}); }
38
+ * }
39
+ *
40
+ * // Compiler transforms to:
41
+ * import { Array as ReactiveArray } from '@portel/photon-core';
42
+ * export default class TodoList extends PhotonMCP {
43
+ * items = new ReactiveArray();
44
+ * async add(text: string) { this.items.push({...}); } // Auto-emits!
45
+ * }
46
+ * ```
33
47
  */
34
48
  function transformReactiveCollections(source: string): string {
35
- // Check which reactive types are imported
36
- const importMatch = source.match(
37
- /import\s*\{([^}]+)\}\s*from\s*['"]@portel\/photon-core['"]/
38
- );
39
- if (!importMatch) return source;
49
+ // Check if this is a PhotonMCP class (extends PhotonMCP)
50
+ const isPhotonMCP = /class\s+\w+\s+extends\s+PhotonMCP\b/.test(source);
51
+ if (!isPhotonMCP) return source;
40
52
 
41
- const imports = importMatch[1].split(',').map(s => s.trim());
53
+ // Check if there are array properties with = [] that need transformation
54
+ // Look for patterns like: `items: Type[] = []` or `items = []` (class properties)
55
+ const hasArrayLiterals =
56
+ /\w+\s*:\s*[^=\n]+\[\]\s*=\s*\[\s*\]/.test(source) || // typed: Type[] = []
57
+ /^\s+\w+\s*=\s*\[\s*\](?=\s*[;\n])/m.test(source); // simple: prop = []
58
+
59
+ if (!hasArrayLiterals) return source;
42
60
 
43
61
  let transformed = source;
44
62
 
45
- // Transform [] to new Array() if Array is imported
46
- if (imports.includes('Array')) {
47
- // Match class property declarations with type annotation = []
48
- // Handles: items: Array<T> = []; | items: Type[] = [];
49
- // The (?::\s*...) ensures there's a type annotation (class property pattern)
50
- transformed = transformed.replace(
51
- /(\w+)\s*:\s*(?:Array<[^>]+>|[^=\n]+\[\])\s*=\s*\[\s*\]/g,
52
- '$1 = new Array()'
63
+ // Check if Array is already imported from photon-core
64
+ const hasArrayImport = /import\s*\{[^}]*\bArray\b[^}]*\}\s*from\s*['"]@portel\/photon-core['"]/.test(source);
65
+
66
+ if (!hasArrayImport) {
67
+ // Inject ReactiveArray import (using alias to avoid shadowing issues)
68
+ // Find the photon-core import and add ReactiveArray to it, or add new import
69
+ const photonCoreImport = source.match(
70
+ /import\s*\{([^}]+)\}\s*from\s*['"]@portel\/photon-core['"]/
53
71
  );
54
72
 
55
- // Match class property without type annotation but NOT local variables
56
- // Class properties: ` items = [];` (indented, no const/let/var)
57
- // Skip: `const x = []`, `let x = []`, `var x = []`
58
- transformed = transformed.replace(
59
- /^(\s+)(\w+)\s*=\s*\[\s*\](?=\s*[;\n])/gm,
60
- (match, indent, propName) => {
61
- // Check if previous non-empty line contains const/let/var - if so, skip
62
- // This is a heuristic but works for common patterns
63
- return `${indent}${propName} = new Array()`;
73
+ if (photonCoreImport) {
74
+ // Add to existing import
75
+ const existingImports = photonCoreImport[1];
76
+ transformed = transformed.replace(
77
+ photonCoreImport[0],
78
+ `import { ${existingImports}, Array as ReactiveArray } from '@portel/photon-core'`
79
+ );
80
+ } else {
81
+ // Add new import at the top (after any existing imports)
82
+ const lastImportMatch = source.match(/^import\s+.+$/gm);
83
+ if (lastImportMatch) {
84
+ const lastImport = lastImportMatch[lastImportMatch.length - 1];
85
+ transformed = transformed.replace(
86
+ lastImport,
87
+ `${lastImport}\nimport { Array as ReactiveArray } from '@portel/photon-core';`
88
+ );
89
+ } else {
90
+ // No imports, add at top
91
+ transformed = `import { Array as ReactiveArray } from '@portel/photon-core';\n${transformed}`;
64
92
  }
65
- );
93
+ }
66
94
  }
67
95
 
68
- // Map and Set already require new, so no transform needed
69
- // (you can't write = {} for Map or Set literals)
96
+ // Determine the Array constructor name to use
97
+ const arrayConstructor = hasArrayImport ? 'Array' : 'ReactiveArray';
98
+
99
+ // Transform class property declarations with type annotation = []
100
+ // Handles: items: Task[] = []; | items: Array<T> = [];
101
+ transformed = transformed.replace(
102
+ /(\w+)\s*:\s*(?:Array<[^>]+>|[^=\n]+\[\])\s*=\s*\[\s*\]/g,
103
+ `$1 = new ${arrayConstructor}()`
104
+ );
105
+
106
+ // Transform class property without type annotation (indented, at start of line)
107
+ // Skip local variables (const/let/var)
108
+ transformed = transformed.replace(
109
+ /^(\s+)(\w+)\s*=\s*\[\s*\](?=\s*[;\n])/gm,
110
+ `$1$2 = new ${arrayConstructor}()`
111
+ );
70
112
 
71
113
  return transformed;
72
114
  }
package/src/index.ts CHANGED
@@ -453,6 +453,11 @@ export {
453
453
  ReactiveMap,
454
454
  ReactiveSet,
455
455
  type Emitter,
456
+ // Level 2+: Rich queryable collection with rendering hints
457
+ Collection,
458
+ type RenderHint,
459
+ type RenderFormat,
460
+ type CompareOp,
456
461
  } from './collections/index.js';
457
462
 
458
463
  // ===== PURPOSE-DRIVEN UI TYPES =====