pulse-js-framework 1.7.29 → 1.7.31
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/cli/dev.js +5 -2
- package/cli/index.js +20 -5
- package/compiler/parser.js +6 -1
- package/compiler/sourcemap.js +5 -3
- package/compiler/transformer/constants.js +2 -1
- package/compiler/transformer/expressions.js +117 -16
- package/compiler/transformer/imports.js +5 -0
- package/compiler/transformer/index.js +9 -9
- package/compiler/transformer/state.js +51 -0
- package/compiler/transformer/view.js +118 -20
- package/package.json +13 -2
- package/runtime/async.js +14 -26
- package/runtime/devtools/diagnostics.js +51 -3
- package/runtime/devtools.js +58 -1
- package/runtime/errors.js +159 -0
- package/runtime/graphql.js +83 -113
- package/runtime/http.js +18 -100
- package/runtime/interceptor-manager.js +242 -0
- package/runtime/pulse.js +36 -1
- package/runtime/router.js +80 -15
- package/runtime/utils.js +121 -5
- package/runtime/websocket.js +62 -73
package/cli/dev.js
CHANGED
|
@@ -134,12 +134,15 @@ export async function startDevServer(args) {
|
|
|
134
134
|
});
|
|
135
135
|
res.end(result.code);
|
|
136
136
|
} else {
|
|
137
|
+
const errorDetails = result.errors.map(e => `${e.message} at line ${e.line || '?'}:${e.column || '?'}`).join('\n');
|
|
138
|
+
console.error(`[Pulse] Compilation error in ${filePath}:`, result.errors);
|
|
137
139
|
res.writeHead(500, { 'Content-Type': 'text/plain' });
|
|
138
|
-
res.end(`Compilation error: ${
|
|
140
|
+
res.end(`Compilation error: ${errorDetails}\nFile: ${filePath}`);
|
|
139
141
|
}
|
|
140
142
|
} catch (error) {
|
|
143
|
+
console.error(`[Pulse] Error compiling ${filePath}:`, error);
|
|
141
144
|
res.writeHead(500, { 'Content-Type': 'text/plain' });
|
|
142
|
-
res.end(`Error: ${error.message}`);
|
|
145
|
+
res.end(`Error: ${error.message}\nFile: ${filePath}`);
|
|
143
146
|
}
|
|
144
147
|
return;
|
|
145
148
|
}
|
package/cli/index.js
CHANGED
|
@@ -15,8 +15,14 @@ const __filename = fileURLToPath(import.meta.url);
|
|
|
15
15
|
const __dirname = dirname(__filename);
|
|
16
16
|
|
|
17
17
|
// Version - read dynamically from package.json
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
let VERSION = '0.0.0';
|
|
19
|
+
try {
|
|
20
|
+
const pkgContent = readFileSync(join(__dirname, '..', 'package.json'), 'utf-8');
|
|
21
|
+
const pkg = JSON.parse(pkgContent);
|
|
22
|
+
VERSION = pkg.version || VERSION;
|
|
23
|
+
} catch (err) {
|
|
24
|
+
log.warn(`Could not read package.json: ${err.message}`);
|
|
25
|
+
}
|
|
20
26
|
|
|
21
27
|
// Available example templates
|
|
22
28
|
const TEMPLATES = {
|
|
@@ -683,10 +689,19 @@ async function initProject(args) {
|
|
|
683
689
|
|
|
684
690
|
if (existsSync(pkgPath)) {
|
|
685
691
|
try {
|
|
686
|
-
|
|
687
|
-
|
|
692
|
+
const pkgContent = readFileSync(pkgPath, 'utf-8');
|
|
693
|
+
if (!pkgContent.trim()) {
|
|
694
|
+
log.warn('Existing package.json is empty, creating new one.');
|
|
695
|
+
} else {
|
|
696
|
+
pkg = JSON.parse(pkgContent);
|
|
697
|
+
log.info('Found existing package.json, merging...');
|
|
698
|
+
}
|
|
688
699
|
} catch (e) {
|
|
689
|
-
|
|
700
|
+
if (e instanceof SyntaxError) {
|
|
701
|
+
log.warn(`Invalid JSON in package.json: ${e.message}. Creating new one.`);
|
|
702
|
+
} else {
|
|
703
|
+
log.warn(`Could not read package.json: ${e.message}. Creating new one.`);
|
|
704
|
+
}
|
|
690
705
|
}
|
|
691
706
|
}
|
|
692
707
|
|
package/compiler/parser.js
CHANGED
|
@@ -1597,9 +1597,14 @@ export class Parser {
|
|
|
1597
1597
|
|
|
1598
1598
|
// Special case: . or # after an identifier needs space (descendant selector)
|
|
1599
1599
|
// e.g., ".school .date" - need space between "school" and "."
|
|
1600
|
+
// BUT NOT for "body.dark" where . is directly adjacent to body (no whitespace)
|
|
1601
|
+
// We check if tokens are adjacent by comparing positions
|
|
1602
|
+
const expectedNextCol = lastToken ? (lastToken.column + String(lastToken.value).length) : 0;
|
|
1603
|
+
const tokensAreAdjacent = token.column === expectedNextCol;
|
|
1600
1604
|
const isDescendantSelector = (tokenValue === '.' || tokenValue === '#') &&
|
|
1601
1605
|
lastToken?.type === TokenType.IDENT &&
|
|
1602
|
-
!inAtRule
|
|
1606
|
+
!inAtRule && // Don't add space in @media selectors
|
|
1607
|
+
!tokensAreAdjacent; // Only add space if not directly adjacent
|
|
1603
1608
|
|
|
1604
1609
|
// Special case: hyphenated class/id names like .job-title, .card-3d, max-width
|
|
1605
1610
|
// Check if we're continuing a class/id name - the last part should end with alphanumeric
|
package/compiler/sourcemap.js
CHANGED
|
@@ -210,9 +210,11 @@ export class SourceMapGenerator {
|
|
|
210
210
|
* @returns {string} Comment with base64 encoded source map
|
|
211
211
|
*/
|
|
212
212
|
toComment() {
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
213
|
+
// Use Buffer for base64 encoding (supports UTF-8, unlike btoa which is Latin1-only)
|
|
214
|
+
const json = this.toString();
|
|
215
|
+
const base64 = typeof Buffer !== 'undefined'
|
|
216
|
+
? Buffer.from(json, 'utf-8').toString('base64')
|
|
217
|
+
: btoa(unescape(encodeURIComponent(json))); // Browser fallback for UTF-8
|
|
216
218
|
return `//# sourceMappingURL=data:application/json;charset=utf-8;base64,${base64}`;
|
|
217
219
|
}
|
|
218
220
|
|
|
@@ -33,7 +33,8 @@ export const PUNCT_NO_SPACE_AFTER = [
|
|
|
33
33
|
/** JavaScript statement keywords */
|
|
34
34
|
export const STATEMENT_KEYWORDS = new Set([
|
|
35
35
|
'let', 'const', 'var', 'return', 'if', 'else', 'for', 'while',
|
|
36
|
-
'switch', 'throw', 'try', 'catch', 'finally'
|
|
36
|
+
'switch', 'throw', 'try', 'catch', 'finally', 'break', 'continue',
|
|
37
|
+
'case', 'default'
|
|
37
38
|
]);
|
|
38
39
|
|
|
39
40
|
/** Built-in JavaScript functions and objects */
|
|
@@ -25,9 +25,9 @@ export function transformExpression(transformer, node) {
|
|
|
25
25
|
|
|
26
26
|
switch (node.type) {
|
|
27
27
|
case NodeType.Identifier:
|
|
28
|
-
// Props
|
|
28
|
+
// Props and state are both reactive (wrapped in computed via useProp or pulse)
|
|
29
29
|
if (transformer.propVars.has(node.name)) {
|
|
30
|
-
return node.name
|
|
30
|
+
return `${node.name}.get()`;
|
|
31
31
|
}
|
|
32
32
|
if (transformer.stateVars.has(node.name)) {
|
|
33
33
|
return `${node.name}.get()`;
|
|
@@ -47,12 +47,18 @@ export function transformExpression(transformer, node) {
|
|
|
47
47
|
|
|
48
48
|
case NodeType.MemberExpression: {
|
|
49
49
|
const obj = transformExpression(transformer, node.object);
|
|
50
|
-
// Use optional chaining when accessing properties on
|
|
50
|
+
// Use optional chaining when accessing properties on:
|
|
51
|
+
// 1. Function call results (could return null/undefined)
|
|
52
|
+
// 2. Props (commonly receive null values like notification: null)
|
|
53
|
+
// Note: State vars don't get optional chaining to avoid breaking array/object methods
|
|
51
54
|
const isCallResult = node.object.type === NodeType.CallExpression;
|
|
52
|
-
const
|
|
55
|
+
const isProp = node.object.type === NodeType.Identifier &&
|
|
56
|
+
transformer.propVars.has(node.object.name);
|
|
57
|
+
const useOptionalChaining = isCallResult || isProp;
|
|
58
|
+
const accessor = useOptionalChaining ? '?.' : '.';
|
|
53
59
|
if (node.computed) {
|
|
54
60
|
const prop = transformExpression(transformer, node.property);
|
|
55
|
-
return
|
|
61
|
+
return useOptionalChaining ? `${obj}?.[${prop}]` : `${obj}[${prop}]`;
|
|
56
62
|
}
|
|
57
63
|
return `${obj}${accessor}${node.property}`;
|
|
58
64
|
}
|
|
@@ -156,20 +162,33 @@ export function transformExpression(transformer, node) {
|
|
|
156
162
|
* @returns {string} Transformed expression string
|
|
157
163
|
*/
|
|
158
164
|
export function transformExpressionString(transformer, exprStr) {
|
|
159
|
-
// Simple transformation: wrap state vars with .get()
|
|
160
|
-
//
|
|
165
|
+
// Simple transformation: wrap state and prop vars with .get()
|
|
166
|
+
// Both are now reactive (useProp returns computed for uniform interface)
|
|
161
167
|
let result = exprStr;
|
|
168
|
+
|
|
169
|
+
// Transform state vars
|
|
162
170
|
for (const stateVar of transformer.stateVars) {
|
|
163
|
-
// Skip if this var name is also a prop (props shadow state in render scope)
|
|
164
|
-
if (transformer.propVars.has(stateVar)) {
|
|
165
|
-
continue;
|
|
166
|
-
}
|
|
167
171
|
result = result.replace(
|
|
168
172
|
new RegExp(`\\b${stateVar}\\b`, 'g'),
|
|
169
173
|
`${stateVar}.get()`
|
|
170
174
|
);
|
|
171
175
|
}
|
|
172
176
|
|
|
177
|
+
// Transform prop vars (now also reactive via useProp)
|
|
178
|
+
// Add optional chaining when followed by property access for nullable props
|
|
179
|
+
// Props commonly receive null values (e.g., notification: null)
|
|
180
|
+
for (const propVar of transformer.propVars) {
|
|
181
|
+
result = result.replace(
|
|
182
|
+
new RegExp(`\\b${propVar}\\b(?=\\.)`, 'g'),
|
|
183
|
+
`${propVar}.get()?`
|
|
184
|
+
);
|
|
185
|
+
// Handle standalone prop var (not followed by property access)
|
|
186
|
+
result = result.replace(
|
|
187
|
+
new RegExp(`\\b${propVar}\\b(?!\\.)`, 'g'),
|
|
188
|
+
`${propVar}.get()`
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
|
|
173
192
|
// NOTE: Removed aggressive optional chaining regex that was adding ?.
|
|
174
193
|
// after ALL function calls. This caused false positives like:
|
|
175
194
|
// "User.name" -> "User?.name" in string literals.
|
|
@@ -186,7 +205,7 @@ export function transformExpressionString(transformer, exprStr) {
|
|
|
186
205
|
* @returns {string} JavaScript code
|
|
187
206
|
*/
|
|
188
207
|
export function transformFunctionBody(transformer, tokens) {
|
|
189
|
-
const { stateVars, actionNames } = transformer;
|
|
208
|
+
const { stateVars, propVars, actionNames } = transformer;
|
|
190
209
|
let code = '';
|
|
191
210
|
let lastToken = null;
|
|
192
211
|
let lastNonSpaceToken = null;
|
|
@@ -194,15 +213,17 @@ export function transformFunctionBody(transformer, tokens) {
|
|
|
194
213
|
// Tokens that must follow } directly without semicolon
|
|
195
214
|
const NO_SEMI_BEFORE = new Set(['catch', 'finally', 'else']);
|
|
196
215
|
|
|
197
|
-
const needsManualSemicolon = (token, nextToken, lastNonSpace) => {
|
|
216
|
+
const needsManualSemicolon = (token, nextToken, lastNonSpace, tokenIndex) => {
|
|
198
217
|
if (!token || lastNonSpace?.value === 'new') return false;
|
|
199
218
|
// Don't add semicolon after 'await' - it always needs its expression
|
|
200
219
|
if (lastNonSpace?.value === 'await') return false;
|
|
201
|
-
// For 'return': bare return followed by statement keyword needs semicolon
|
|
220
|
+
// For 'return': bare return followed by statement keyword or state assignment needs semicolon
|
|
202
221
|
if (lastNonSpace?.value === 'return') {
|
|
203
222
|
// If followed by a statement keyword, it's a bare return - needs semicolon
|
|
204
223
|
if (token.type === 'IDENT' && STATEMENT_KEYWORDS.has(token.value)) return true;
|
|
205
224
|
if (STATEMENT_TOKEN_TYPES.has(token.type)) return true;
|
|
225
|
+
// If followed by a state variable assignment, it's a bare return - needs semicolon
|
|
226
|
+
if (token.type === 'IDENT' && stateVars.has(token.value) && nextToken?.type === 'EQ') return true;
|
|
206
227
|
return false; // return expression - no semicolon
|
|
207
228
|
}
|
|
208
229
|
// Don't add semicolon before catch/finally/else after }
|
|
@@ -211,9 +232,25 @@ export function transformFunctionBody(transformer, tokens) {
|
|
|
211
232
|
if (token.type !== 'IDENT') return false;
|
|
212
233
|
if (STATEMENT_KEYWORDS.has(token.value)) return true;
|
|
213
234
|
if (stateVars.has(token.value) && nextToken?.type === 'EQ') return true;
|
|
235
|
+
// Any identifier followed by = after a statement end is an assignment statement
|
|
236
|
+
if (nextToken?.type === 'EQ' && lastNonSpace?.type === 'RPAREN') return true;
|
|
214
237
|
if (nextToken?.type === 'LPAREN' &&
|
|
215
238
|
(BUILTIN_FUNCTIONS.has(token.value) || actionNames.has(token.value))) return true;
|
|
216
239
|
if (nextToken?.type === 'DOT' && BUILTIN_FUNCTIONS.has(token.value)) return true;
|
|
240
|
+
|
|
241
|
+
// Check for property assignment or method call: identifier.property = value OR identifier.method()
|
|
242
|
+
// This is a new statement if token is followed by .property = ... or .method(...)
|
|
243
|
+
if (nextToken?.type === 'DOT') {
|
|
244
|
+
// Look ahead to see if this is an assignment or method call
|
|
245
|
+
for (let j = tokenIndex + 1; j < tokens.length && j < tokenIndex + 10; j++) {
|
|
246
|
+
const t = tokens[j];
|
|
247
|
+
// Assignment: identifier.property = value
|
|
248
|
+
if (t.type === 'EQ' && tokens[j-1]?.type === 'IDENT') return true;
|
|
249
|
+
// Method call: identifier.method()
|
|
250
|
+
if (t.type === 'LPAREN' && tokens[j-1]?.type === 'IDENT') return true;
|
|
251
|
+
if (t.type === 'SEMI') break;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
217
254
|
return false;
|
|
218
255
|
};
|
|
219
256
|
|
|
@@ -242,7 +279,7 @@ export function transformFunctionBody(transformer, tokens) {
|
|
|
242
279
|
}
|
|
243
280
|
|
|
244
281
|
// Add semicolon before statement starters
|
|
245
|
-
if (needsManualSemicolon(token, nextToken, lastNonSpaceToken) &&
|
|
282
|
+
if (needsManualSemicolon(token, nextToken, lastNonSpaceToken, i) &&
|
|
246
283
|
afterStatementEnd(lastNonSpaceToken)) {
|
|
247
284
|
if (!afterIfCondition && lastToken && lastToken.value !== ';' && lastToken.value !== '{') {
|
|
248
285
|
code += '; ';
|
|
@@ -416,10 +453,74 @@ export function transformFunctionBody(transformer, tokens) {
|
|
|
416
453
|
|
|
417
454
|
// Replace state var reads (not in assignments, not already with .get/.set)
|
|
418
455
|
// Allow spread operators (...stateVar) but block member access (obj.stateVar)
|
|
456
|
+
// Skip object literal keys (e.g., { users: value } - don't transform the key 'users')
|
|
419
457
|
for (const stateVar of stateVars) {
|
|
420
458
|
code = code.replace(
|
|
421
459
|
new RegExp(`(?:(?<=\\.\\.\\.)|(?<!\\.))\\b${stateVar}\\b(?!\\s*=(?!=)|\\s*\\(|\\s*\\.(?:get|set))`, 'g'),
|
|
422
|
-
|
|
460
|
+
(match, offset) => {
|
|
461
|
+
// Check if this is an object key by looking at context
|
|
462
|
+
// Pattern: after { or , and before : (with arbitrary content between)
|
|
463
|
+
const after = code.slice(offset + match.length, offset + match.length + 10);
|
|
464
|
+
|
|
465
|
+
// If followed by : (not ::), check if it's an object key
|
|
466
|
+
if (/^\s*:(?!:)/.test(after)) {
|
|
467
|
+
// Look backwards for the nearest { or , that would indicate object context
|
|
468
|
+
// We need to track bracket depth to handle nested structures
|
|
469
|
+
let depth = 0;
|
|
470
|
+
for (let i = offset - 1; i >= 0; i--) {
|
|
471
|
+
const ch = code[i];
|
|
472
|
+
if (ch === ')' || ch === ']') depth++;
|
|
473
|
+
else if (ch === '(' || ch === '[') depth--;
|
|
474
|
+
else if (ch === '}') depth++;
|
|
475
|
+
else if (ch === '{') {
|
|
476
|
+
if (depth === 0) {
|
|
477
|
+
// Found opening brace at same depth - this is an object key
|
|
478
|
+
return match;
|
|
479
|
+
}
|
|
480
|
+
depth--;
|
|
481
|
+
}
|
|
482
|
+
else if (ch === ',' && depth === 0) {
|
|
483
|
+
// Found comma at same depth - this is an object key
|
|
484
|
+
return match;
|
|
485
|
+
}
|
|
486
|
+
// Stop if we hit a semicolon at depth 0 (different statement)
|
|
487
|
+
else if (ch === ';' && depth === 0) break;
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
return `${stateVar}.get()`;
|
|
492
|
+
}
|
|
493
|
+
);
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
// Replace prop var reads (props are reactive via useProp, need .get() like state vars)
|
|
497
|
+
// For props: allow function calls (prop callbacks need .get()() pattern)
|
|
498
|
+
// Skip object keys, allow spreads, block member access
|
|
499
|
+
for (const propVar of propVars) {
|
|
500
|
+
code = code.replace(
|
|
501
|
+
new RegExp(`(?:(?<=\\.\\.\\.)|(?<!\\.))\\b${propVar}\\b(?!\\s*=(?!=)|\\s*\\.(?:get|set))`, 'g'),
|
|
502
|
+
(match, offset) => {
|
|
503
|
+
// Check if this is an object key
|
|
504
|
+
const after = code.slice(offset + match.length, offset + match.length + 10);
|
|
505
|
+
|
|
506
|
+
if (/^\s*:(?!:)/.test(after)) {
|
|
507
|
+
let depth = 0;
|
|
508
|
+
for (let i = offset - 1; i >= 0; i--) {
|
|
509
|
+
const ch = code[i];
|
|
510
|
+
if (ch === ')' || ch === ']') depth++;
|
|
511
|
+
else if (ch === '(' || ch === '[') depth--;
|
|
512
|
+
else if (ch === '}') depth++;
|
|
513
|
+
else if (ch === '{') {
|
|
514
|
+
if (depth === 0) return match;
|
|
515
|
+
depth--;
|
|
516
|
+
}
|
|
517
|
+
else if (ch === ',' && depth === 0) return match;
|
|
518
|
+
else if (ch === ';' && depth === 0) break;
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
return `${propVar}.get()`;
|
|
523
|
+
}
|
|
423
524
|
);
|
|
424
525
|
}
|
|
425
526
|
|
|
@@ -46,6 +46,11 @@ export function generateImports(transformer) {
|
|
|
46
46
|
'model'
|
|
47
47
|
];
|
|
48
48
|
|
|
49
|
+
// Add useProp if component has props (for reactive prop support)
|
|
50
|
+
if (transformer.propVars.size > 0) {
|
|
51
|
+
runtimeImports.push('useProp');
|
|
52
|
+
}
|
|
53
|
+
|
|
49
54
|
lines.push(`import { ${runtimeImports.join(', ')} } from '${options.runtime}';`);
|
|
50
55
|
|
|
51
56
|
// A11y imports (if a11y features are used)
|
|
@@ -178,15 +178,7 @@ export class Transformer {
|
|
|
178
178
|
extractImportedComponents(this, this.ast.imports);
|
|
179
179
|
}
|
|
180
180
|
|
|
181
|
-
//
|
|
182
|
-
if (this.ast.view) {
|
|
183
|
-
this._scanA11yUsage(this.ast.view);
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
// Imports (runtime + user imports)
|
|
187
|
-
parts.push(generateImports(this));
|
|
188
|
-
|
|
189
|
-
// Extract prop variables
|
|
181
|
+
// Extract prop variables (before imports so useProp can be conditionally imported)
|
|
190
182
|
if (this.ast.props) {
|
|
191
183
|
extractPropVars(this, this.ast.props);
|
|
192
184
|
}
|
|
@@ -201,6 +193,14 @@ export class Transformer {
|
|
|
201
193
|
extractActionNames(this, this.ast.actions);
|
|
202
194
|
}
|
|
203
195
|
|
|
196
|
+
// Pre-scan for a11y usage to determine imports
|
|
197
|
+
if (this.ast.view) {
|
|
198
|
+
this._scanA11yUsage(this.ast.view);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// Imports (runtime + user imports) - after extraction so we know what to import
|
|
202
|
+
parts.push(generateImports(this));
|
|
203
|
+
|
|
204
204
|
// Store (must come before router so $store is available to guards)
|
|
205
205
|
if (this.ast.store) {
|
|
206
206
|
parts.push(transformStore(this, this.ast.store, transformValue));
|
|
@@ -93,6 +93,22 @@ export function transformState(transformer, stateBlock) {
|
|
|
93
93
|
return lines.join('\n');
|
|
94
94
|
}
|
|
95
95
|
|
|
96
|
+
/**
|
|
97
|
+
* Check if an action body references any prop variables
|
|
98
|
+
* @param {Array} bodyTokens - Function body tokens
|
|
99
|
+
* @param {Set} propVars - Set of prop variable names
|
|
100
|
+
* @returns {boolean} True if action references props
|
|
101
|
+
*/
|
|
102
|
+
export function actionReferencesProp(bodyTokens, propVars) {
|
|
103
|
+
if (propVars.size === 0) return false;
|
|
104
|
+
for (const token of bodyTokens) {
|
|
105
|
+
if (token.type === 'IDENT' && propVars.has(token.value)) {
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
|
|
96
112
|
/**
|
|
97
113
|
* Transform actions block to function declarations
|
|
98
114
|
* @param {Object} transformer - Transformer instance
|
|
@@ -104,6 +120,11 @@ export function transformActions(transformer, actionsBlock, transformFunctionBod
|
|
|
104
120
|
const lines = ['// Actions'];
|
|
105
121
|
|
|
106
122
|
for (const fn of actionsBlock.functions) {
|
|
123
|
+
// Skip actions that reference props - they'll be generated inside render()
|
|
124
|
+
if (actionReferencesProp(fn.body, transformer.propVars)) {
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
|
|
107
128
|
const asyncKeyword = fn.async ? 'async ' : '';
|
|
108
129
|
const params = fn.params.join(', ');
|
|
109
130
|
const body = transformFunctionBody(transformer, fn.body);
|
|
@@ -116,3 +137,33 @@ export function transformActions(transformer, actionsBlock, transformFunctionBod
|
|
|
116
137
|
|
|
117
138
|
return lines.join('\n');
|
|
118
139
|
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Transform prop-dependent actions (to be placed inside render function)
|
|
143
|
+
* @param {Object} transformer - Transformer instance
|
|
144
|
+
* @param {Object} actionsBlock - Actions block from AST
|
|
145
|
+
* @param {Function} transformFunctionBody - Function to transform body tokens
|
|
146
|
+
* @param {string} indent - Indentation string
|
|
147
|
+
* @returns {string} JavaScript code
|
|
148
|
+
*/
|
|
149
|
+
export function transformPropDependentActions(transformer, actionsBlock, transformFunctionBody, indent = ' ') {
|
|
150
|
+
const lines = [];
|
|
151
|
+
|
|
152
|
+
for (const fn of actionsBlock.functions) {
|
|
153
|
+
// Only include actions that reference props
|
|
154
|
+
if (!actionReferencesProp(fn.body, transformer.propVars)) {
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const asyncKeyword = fn.async ? 'async ' : '';
|
|
159
|
+
const params = fn.params.join(', ');
|
|
160
|
+
const body = transformFunctionBody(transformer, fn.body);
|
|
161
|
+
|
|
162
|
+
lines.push(`${indent}${asyncKeyword}function ${fn.name}(${params}) {`);
|
|
163
|
+
lines.push(`${indent} ${body}`);
|
|
164
|
+
lines.push(`${indent}}`);
|
|
165
|
+
lines.push('');
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return lines.join('\n');
|
|
169
|
+
}
|
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
import { NodeType } from '../parser.js';
|
|
8
|
-
import { transformValue } from './state.js';
|
|
9
|
-
import { transformExpression, transformExpressionString } from './expressions.js';
|
|
8
|
+
import { transformValue, transformPropDependentActions } from './state.js';
|
|
9
|
+
import { transformExpression, transformExpressionString, transformFunctionBody } from './expressions.js';
|
|
10
10
|
|
|
11
11
|
/** View node transformers lookup table */
|
|
12
12
|
export const VIEW_NODE_HANDLERS = {
|
|
@@ -38,14 +38,27 @@ export function transformView(transformer, viewBlock) {
|
|
|
38
38
|
// Generate render function with props parameter
|
|
39
39
|
lines.push('function render({ props = {}, slots = {} } = {}) {');
|
|
40
40
|
|
|
41
|
-
//
|
|
41
|
+
// Extract props using useProp() for reactive prop support
|
|
42
|
+
// useProp checks for a getter (propName$) and returns a computed if present
|
|
42
43
|
if (transformer.propVars.size > 0) {
|
|
43
|
-
const
|
|
44
|
+
for (const name of transformer.propVars) {
|
|
44
45
|
const defaultValue = transformer.propDefaults.get(name);
|
|
45
46
|
const defaultCode = defaultValue ? transformValue(transformer, defaultValue) : 'undefined';
|
|
46
|
-
|
|
47
|
-
}
|
|
48
|
-
|
|
47
|
+
lines.push(` const ${name} = useProp(props, '${name}', ${defaultCode});`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Generate prop-dependent actions inside render (after useProp declarations)
|
|
52
|
+
if (transformer.ast.actions && transformer.propVars.size > 0) {
|
|
53
|
+
const propActions = transformPropDependentActions(
|
|
54
|
+
transformer,
|
|
55
|
+
transformer.ast.actions,
|
|
56
|
+
transformFunctionBody,
|
|
57
|
+
' '
|
|
58
|
+
);
|
|
59
|
+
if (propActions.trim()) {
|
|
60
|
+
lines.push(propActions);
|
|
61
|
+
}
|
|
49
62
|
}
|
|
50
63
|
|
|
51
64
|
lines.push(' return (');
|
|
@@ -416,7 +429,7 @@ function extractDynamicAttributes(selector) {
|
|
|
416
429
|
}
|
|
417
430
|
}
|
|
418
431
|
|
|
419
|
-
//
|
|
432
|
+
// Parse the attribute value (may be static or contain interpolations)
|
|
420
433
|
let attrValue = '';
|
|
421
434
|
if (quoteChar) {
|
|
422
435
|
// Quoted value - read until closing quote
|
|
@@ -437,8 +450,15 @@ function extractDynamicAttributes(selector) {
|
|
|
437
450
|
// Skip closing ]
|
|
438
451
|
if (selector[i] === ']') i++;
|
|
439
452
|
|
|
440
|
-
//
|
|
441
|
-
|
|
453
|
+
// Check if the value contains interpolation {expr}
|
|
454
|
+
// If so, treat as dynamic attribute with template string
|
|
455
|
+
if (attrValue.includes('{') && attrValue.includes('}')) {
|
|
456
|
+
// Convert "text {expr} more" to template string: `text ${expr} more`
|
|
457
|
+
dynamicAttrs.push({ name: attrName, expr: attrValue, isInterpolated: true });
|
|
458
|
+
} else {
|
|
459
|
+
// Pure static attribute
|
|
460
|
+
staticAttrs.push({ name: attrName, value: attrValue });
|
|
461
|
+
}
|
|
442
462
|
continue;
|
|
443
463
|
} else {
|
|
444
464
|
// Boolean attribute (no value) like [disabled]
|
|
@@ -618,7 +638,16 @@ export function transformElement(transformer, node, indent) {
|
|
|
618
638
|
|
|
619
639
|
// Chain dynamic attribute bindings (e.g., [value={searchQuery}])
|
|
620
640
|
for (const attr of dynamicAttrs) {
|
|
621
|
-
|
|
641
|
+
let exprCode;
|
|
642
|
+
if (attr.isInterpolated) {
|
|
643
|
+
// String with interpolation: "display: {show ? 'block' : 'none'}"
|
|
644
|
+
// Convert to template literal: `display: ${show.get() ? 'block' : 'none'}`
|
|
645
|
+
const templateStr = attr.expr.replace(/\{/g, '${');
|
|
646
|
+
exprCode = '`' + transformExpressionString(transformer, templateStr) + '`';
|
|
647
|
+
} else {
|
|
648
|
+
// Pure expression: {searchQuery}
|
|
649
|
+
exprCode = transformExpressionString(transformer, attr.expr);
|
|
650
|
+
}
|
|
622
651
|
result = `bind(${result}, '${attr.name}', () => ${exprCode})`;
|
|
623
652
|
}
|
|
624
653
|
|
|
@@ -642,8 +671,69 @@ export function addScopeToSelector(transformer, selector) {
|
|
|
642
671
|
return `${selector}.${transformer.scopeId}`;
|
|
643
672
|
}
|
|
644
673
|
|
|
674
|
+
/**
|
|
675
|
+
* Check if an expression references any state variables
|
|
676
|
+
* @param {Object} transformer - Transformer instance
|
|
677
|
+
* @param {Object} node - AST node to check
|
|
678
|
+
* @returns {boolean} True if expression uses state variables
|
|
679
|
+
*/
|
|
680
|
+
function expressionUsesState(transformer, node) {
|
|
681
|
+
if (!node) return false;
|
|
682
|
+
|
|
683
|
+
switch (node.type) {
|
|
684
|
+
case NodeType.Identifier:
|
|
685
|
+
return transformer.stateVars.has(node.name);
|
|
686
|
+
|
|
687
|
+
case NodeType.MemberExpression:
|
|
688
|
+
return expressionUsesState(transformer, node.object) ||
|
|
689
|
+
(node.computed && expressionUsesState(transformer, node.property));
|
|
690
|
+
|
|
691
|
+
case NodeType.CallExpression:
|
|
692
|
+
return expressionUsesState(transformer, node.callee) ||
|
|
693
|
+
node.arguments.some(arg => expressionUsesState(transformer, arg));
|
|
694
|
+
|
|
695
|
+
case NodeType.BinaryExpression:
|
|
696
|
+
case NodeType.ConditionalExpression:
|
|
697
|
+
return expressionUsesState(transformer, node.left) ||
|
|
698
|
+
expressionUsesState(transformer, node.right) ||
|
|
699
|
+
(node.test && expressionUsesState(transformer, node.test)) ||
|
|
700
|
+
(node.consequent && expressionUsesState(transformer, node.consequent)) ||
|
|
701
|
+
(node.alternate && expressionUsesState(transformer, node.alternate));
|
|
702
|
+
|
|
703
|
+
case NodeType.UnaryExpression:
|
|
704
|
+
case NodeType.UpdateExpression:
|
|
705
|
+
return expressionUsesState(transformer, node.argument);
|
|
706
|
+
|
|
707
|
+
case NodeType.ArrayLiteral:
|
|
708
|
+
return node.elements?.some(el => expressionUsesState(transformer, el));
|
|
709
|
+
|
|
710
|
+
case NodeType.ObjectLiteral:
|
|
711
|
+
return node.properties?.some(prop =>
|
|
712
|
+
expressionUsesState(transformer, prop.value)
|
|
713
|
+
);
|
|
714
|
+
|
|
715
|
+
case NodeType.ArrowFunction:
|
|
716
|
+
// Arrow functions capture state, but don't need wrapping themselves
|
|
717
|
+
return false;
|
|
718
|
+
|
|
719
|
+
case NodeType.SpreadElement:
|
|
720
|
+
return expressionUsesState(transformer, node.argument);
|
|
721
|
+
|
|
722
|
+
default:
|
|
723
|
+
return false;
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
|
|
645
727
|
/**
|
|
646
728
|
* Transform a component call (imported component)
|
|
729
|
+
*
|
|
730
|
+
* For reactive props (those that reference state variables), we pass both:
|
|
731
|
+
* - The current value: `propName: value`
|
|
732
|
+
* - A getter function: `propName$: () => value`
|
|
733
|
+
*
|
|
734
|
+
* The child component can use `useProp(props, 'propName', default)` to get
|
|
735
|
+
* a reactive computed if a getter exists, or the static value otherwise.
|
|
736
|
+
*
|
|
647
737
|
* @param {Object} transformer - Transformer instance
|
|
648
738
|
* @param {Object} node - Element node (component)
|
|
649
739
|
* @param {number} indent - Indentation level
|
|
@@ -670,17 +760,26 @@ export function transformComponentCall(transformer, node, indent) {
|
|
|
670
760
|
}
|
|
671
761
|
|
|
672
762
|
// Build component call
|
|
673
|
-
let code = `${pad}${componentName}.render({ `;
|
|
674
|
-
|
|
675
763
|
const renderArgs = [];
|
|
676
764
|
|
|
677
765
|
// Add props if any
|
|
678
766
|
if (node.props && node.props.length > 0) {
|
|
679
|
-
const
|
|
767
|
+
const propEntries = [];
|
|
768
|
+
|
|
769
|
+
for (const prop of node.props) {
|
|
680
770
|
const valueCode = transformExpression(transformer, prop.value);
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
771
|
+
const usesState = expressionUsesState(transformer, prop.value);
|
|
772
|
+
|
|
773
|
+
if (usesState) {
|
|
774
|
+
// Reactive prop: pass both value and getter
|
|
775
|
+
// The getter allows child to create a reactive binding via useProp()
|
|
776
|
+
propEntries.push(`${prop.name}$: () => ${valueCode}`);
|
|
777
|
+
}
|
|
778
|
+
// Always pass the current value (for non-useProp access and initial render)
|
|
779
|
+
propEntries.push(`${prop.name}: ${valueCode}`);
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
renderArgs.push(`props: { ${propEntries.join(', ')} }`);
|
|
684
783
|
}
|
|
685
784
|
|
|
686
785
|
// Add slots if any
|
|
@@ -691,9 +790,8 @@ export function transformComponentCall(transformer, node, indent) {
|
|
|
691
790
|
renderArgs.push(`slots: { ${slotCode} }`);
|
|
692
791
|
}
|
|
693
792
|
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
return code;
|
|
793
|
+
const renderCall = `${componentName}.render({ ${renderArgs.join(', ')} })`;
|
|
794
|
+
return `${pad}${renderCall}`;
|
|
697
795
|
}
|
|
698
796
|
|
|
699
797
|
/**
|