pulse-js-framework 1.7.10 → 1.7.12

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/README.md CHANGED
@@ -1,9 +1,10 @@
1
1
  # Pulse Framework
2
2
 
3
3
  [![CI](https://github.com/vincenthirtz/pulse-js-framework/actions/workflows/ci.yml/badge.svg)](https://github.com/vincenthirtz/pulse-js-framework/actions/workflows/ci.yml)
4
+ [![codecov](https://codecov.io/gh/vincenthirtz/pulse-js-framework/graph/badge.svg)](https://codecov.io/gh/vincenthirtz/pulse-js-framework)
4
5
  [![Netlify Status](https://api.netlify.com/api/v1/badges/2597dac2-228a-4d3e-bea8-4e7ef8ac5c53/deploy-status)](https://app.netlify.com/projects/pulse-js/deploys)
5
6
 
6
- A declarative DOM framework with CSS selector-based structure and reactive pulsations.
7
+ No build. No dependencies. Just JavaScript.
7
8
 
8
9
  ## Features
9
10
 
@@ -249,6 +250,7 @@ const count: Pulse<number> = pulse(0);
249
250
  | [HMR Demo](examples/hmr) | Hot module replacement |
250
251
  | [Router Demo](examples/router) | SPA routing |
251
252
  | [Store Demo](examples/store) | State with undo/redo |
253
+ | [Electron App](examples/electron) | Desktop notes app |
252
254
 
253
255
  ## Documentation
254
256
 
package/compiler/lexer.js CHANGED
@@ -33,6 +33,7 @@ export const TokenType = {
33
33
 
34
34
  // Directives
35
35
  AT: 'AT', // @
36
+ DIRECTIVE_MOD: 'DIRECTIVE_MOD', // .modifier after @directive (e.g., @click.prevent)
36
37
  PAGE: 'PAGE',
37
38
  ROUTE: 'ROUTE',
38
39
  IF: 'IF',
@@ -499,10 +500,31 @@ export class Lexer {
499
500
  continue;
500
501
  }
501
502
 
502
- // At-sign for directives
503
+ // At-sign for directives with optional modifiers
503
504
  if (char === '@') {
504
505
  this.advance();
505
506
  this.tokens.push(new Token(TokenType.AT, '@', startLine, startColumn));
507
+
508
+ // After @, read directive name (if identifier follows)
509
+ if (/[a-zA-Z]/.test(this.current())) {
510
+ // Read the directive name
511
+ const nameToken = this.readIdentifier();
512
+ this.tokens.push(nameToken);
513
+
514
+ // Read modifiers: .prevent, .stop, .enter, etc.
515
+ while (!this.isEOF() && this.current() === '.' && /[a-zA-Z]/.test(this.peek())) {
516
+ this.advance(); // skip '.'
517
+ const modStartLine = this.line;
518
+ const modStartColumn = this.column;
519
+ let modName = '';
520
+ while (!this.isEOF() && /[a-zA-Z0-9]/.test(this.current())) {
521
+ modName += this.advance();
522
+ }
523
+ if (modName) {
524
+ this.tokens.push(new Token(TokenType.DIRECTIVE_MOD, modName, modStartLine, modStartColumn));
525
+ }
526
+ }
527
+ }
506
528
  continue;
507
529
  }
508
530
 
@@ -27,6 +27,7 @@ export const NodeType = {
27
27
  IfDirective: 'IfDirective',
28
28
  EachDirective: 'EachDirective',
29
29
  EventDirective: 'EventDirective',
30
+ ModelDirective: 'ModelDirective',
30
31
 
31
32
  // Accessibility directives
32
33
  A11yDirective: 'A11yDirective',
@@ -769,6 +770,12 @@ export class Parser {
769
770
 
770
771
  const name = this.expect(TokenType.IDENT).value;
771
772
 
773
+ // Collect modifiers (.prevent, .stop, .enter, .lazy, etc.)
774
+ const modifiers = [];
775
+ while (this.is(TokenType.DIRECTIVE_MOD)) {
776
+ modifiers.push(this.advance().value);
777
+ }
778
+
772
779
  if (name === 'if') {
773
780
  return this.parseIfDirective();
774
781
  }
@@ -790,8 +797,13 @@ export class Parser {
790
797
  return this.parseSrOnlyDirective();
791
798
  }
792
799
 
800
+ // @model directive for two-way binding
801
+ if (name === 'model') {
802
+ return this.parseModelDirective(modifiers);
803
+ }
804
+
793
805
  // Event directive like @click
794
- return this.parseEventDirective(name);
806
+ return this.parseEventDirective(name, modifiers);
795
807
  }
796
808
 
797
809
  /**
@@ -801,6 +813,12 @@ export class Parser {
801
813
  this.expect(TokenType.AT);
802
814
  const name = this.expect(TokenType.IDENT).value;
803
815
 
816
+ // Collect modifiers (.prevent, .stop, .enter, .lazy, etc.)
817
+ const modifiers = [];
818
+ while (this.is(TokenType.DIRECTIVE_MOD)) {
819
+ modifiers.push(this.advance().value);
820
+ }
821
+
804
822
  // Check for a11y directives
805
823
  if (name === 'a11y') {
806
824
  return this.parseA11yDirective();
@@ -815,16 +833,22 @@ export class Parser {
815
833
  return this.parseSrOnlyDirective();
816
834
  }
817
835
 
836
+ // @model directive for two-way binding
837
+ if (name === 'model') {
838
+ return this.parseModelDirective(modifiers);
839
+ }
840
+
818
841
  // Event directive (click, submit, etc.)
819
842
  this.expect(TokenType.LPAREN);
820
843
  const expression = this.parseExpression();
821
844
  this.expect(TokenType.RPAREN);
822
845
 
823
- return new ASTNode(NodeType.EventDirective, { event: name, handler: expression });
846
+ return new ASTNode(NodeType.EventDirective, { event: name, handler: expression, modifiers });
824
847
  }
825
848
 
826
849
  /**
827
- * Parse @if directive
850
+ * Parse @if directive with @else-if/@else chains
851
+ * Syntax: @if (cond) { } @else-if (cond) { } @else { }
828
852
  */
829
853
  parseIfDirective() {
830
854
  this.expect(TokenType.LPAREN);
@@ -838,23 +862,76 @@ export class Parser {
838
862
  }
839
863
  this.expect(TokenType.RBRACE);
840
864
 
865
+ const elseIfBranches = [];
841
866
  let alternate = null;
842
- if (this.is(TokenType.AT) && this.peek()?.value === 'else') {
843
- this.advance(); // @
844
- this.advance(); // else
845
- this.expect(TokenType.LBRACE);
846
- alternate = [];
847
- while (!this.is(TokenType.RBRACE) && !this.is(TokenType.EOF)) {
848
- alternate.push(this.parseViewChild());
867
+
868
+ // Parse @else-if and @else chains
869
+ while (this.is(TokenType.AT)) {
870
+ const nextToken = this.peek();
871
+
872
+ // Check for @else or @else-if
873
+ if (nextToken?.value === 'else') {
874
+ this.advance(); // @
875
+ this.advance(); // else
876
+
877
+ // Check if followed by @if or -if (making @else @if or @else-if)
878
+ if (this.is(TokenType.AT) && (this.peek()?.type === TokenType.IF || this.peek()?.value === 'if')) {
879
+ // @else @if pattern
880
+ this.advance(); // @
881
+ this.advance(); // if
882
+
883
+ this.expect(TokenType.LPAREN);
884
+ const elseIfCondition = this.parseExpression();
885
+ this.expect(TokenType.RPAREN);
886
+
887
+ this.expect(TokenType.LBRACE);
888
+ const elseIfConsequent = [];
889
+ while (!this.is(TokenType.RBRACE) && !this.is(TokenType.EOF)) {
890
+ elseIfConsequent.push(this.parseViewChild());
891
+ }
892
+ this.expect(TokenType.RBRACE);
893
+
894
+ elseIfBranches.push({ condition: elseIfCondition, consequent: elseIfConsequent });
895
+ }
896
+ // Check for -if pattern (@else-if as hyphenated)
897
+ else if (this.is(TokenType.MINUS) && (this.peek()?.type === TokenType.IF || this.peek()?.value === 'if')) {
898
+ this.advance(); // -
899
+ this.advance(); // if
900
+
901
+ this.expect(TokenType.LPAREN);
902
+ const elseIfCondition = this.parseExpression();
903
+ this.expect(TokenType.RPAREN);
904
+
905
+ this.expect(TokenType.LBRACE);
906
+ const elseIfConsequent = [];
907
+ while (!this.is(TokenType.RBRACE) && !this.is(TokenType.EOF)) {
908
+ elseIfConsequent.push(this.parseViewChild());
909
+ }
910
+ this.expect(TokenType.RBRACE);
911
+
912
+ elseIfBranches.push({ condition: elseIfCondition, consequent: elseIfConsequent });
913
+ }
914
+ // Plain @else
915
+ else {
916
+ this.expect(TokenType.LBRACE);
917
+ alternate = [];
918
+ while (!this.is(TokenType.RBRACE) && !this.is(TokenType.EOF)) {
919
+ alternate.push(this.parseViewChild());
920
+ }
921
+ this.expect(TokenType.RBRACE);
922
+ break; // @else terminates the chain
923
+ }
924
+ } else {
925
+ break; // Not an @else variant
849
926
  }
850
- this.expect(TokenType.RBRACE);
851
927
  }
852
928
 
853
- return new ASTNode(NodeType.IfDirective, { condition, consequent, alternate });
929
+ return new ASTNode(NodeType.IfDirective, { condition, consequent, elseIfBranches, alternate });
854
930
  }
855
931
 
856
932
  /**
857
- * Parse @each/@for directive
933
+ * Parse @each/@for directive with optional key function
934
+ * Syntax: @for (item of items) key(item.id) { ... }
858
935
  */
859
936
  parseEachDirective() {
860
937
  this.expect(TokenType.LPAREN);
@@ -870,6 +947,15 @@ export class Parser {
870
947
  const iterable = this.parseExpression();
871
948
  this.expect(TokenType.RPAREN);
872
949
 
950
+ // Parse optional key function: key(item.id)
951
+ let keyExpr = null;
952
+ if (this.is(TokenType.IDENT) && this.current().value === 'key') {
953
+ this.advance(); // consume 'key'
954
+ this.expect(TokenType.LPAREN);
955
+ keyExpr = this.parseExpression();
956
+ this.expect(TokenType.RPAREN);
957
+ }
958
+
873
959
  this.expect(TokenType.LBRACE);
874
960
  const template = [];
875
961
  while (!this.is(TokenType.RBRACE) && !this.is(TokenType.EOF)) {
@@ -877,13 +963,15 @@ export class Parser {
877
963
  }
878
964
  this.expect(TokenType.RBRACE);
879
965
 
880
- return new ASTNode(NodeType.EachDirective, { itemName, iterable, template });
966
+ return new ASTNode(NodeType.EachDirective, { itemName, iterable, template, keyExpr });
881
967
  }
882
968
 
883
969
  /**
884
- * Parse event directive
970
+ * Parse event directive with optional modifiers
971
+ * @param {string} event - Event name (click, keydown, etc.)
972
+ * @param {string[]} modifiers - Array of modifier names (prevent, stop, enter, etc.)
885
973
  */
886
- parseEventDirective(event) {
974
+ parseEventDirective(event, modifiers = []) {
887
975
  this.expect(TokenType.LPAREN);
888
976
  const handler = this.parseExpression();
889
977
  this.expect(TokenType.RPAREN);
@@ -897,7 +985,20 @@ export class Parser {
897
985
  this.expect(TokenType.RBRACE);
898
986
  }
899
987
 
900
- return new ASTNode(NodeType.EventDirective, { event, handler, children });
988
+ return new ASTNode(NodeType.EventDirective, { event, handler, children, modifiers });
989
+ }
990
+
991
+ /**
992
+ * Parse @model directive for two-way binding
993
+ * @model(name) or @model.lazy(name) or @model.lazy.trim(name)
994
+ * @param {string[]} modifiers - Array of modifier names (lazy, trim, number)
995
+ */
996
+ parseModelDirective(modifiers = []) {
997
+ this.expect(TokenType.LPAREN);
998
+ const binding = this.parseExpression();
999
+ this.expect(TokenType.RPAREN);
1000
+
1001
+ return new ASTNode(NodeType.ModelDirective, { binding, modifiers });
901
1002
  }
902
1003
 
903
1004
  /**
@@ -12,6 +12,7 @@
12
12
  export function generateExport(transformer) {
13
13
  const pageName = transformer.ast.page?.name || 'Component';
14
14
  const routePath = transformer.ast.route?.path || null;
15
+ const hasInit = transformer.actionNames.has('init');
15
16
 
16
17
  const lines = ['// Export'];
17
18
  lines.push(`export const ${pageName} = {`);
@@ -21,9 +22,47 @@ export function generateExport(transformer) {
21
22
  lines.push(` route: ${JSON.stringify(routePath)},`);
22
23
  }
23
24
 
25
+ // Mount with reactive re-rendering (preserves focus)
24
26
  lines.push(' mount: (target) => {');
25
- lines.push(' const el = render();');
26
- lines.push(' return mount(target, el);');
27
+ lines.push(' const container = typeof target === "string" ? document.querySelector(target) : target;');
28
+ lines.push(' let currentEl = null;');
29
+ lines.push(' effect(() => {');
30
+ lines.push(' // Save focus state before re-render');
31
+ lines.push(' const activeEl = document.activeElement;');
32
+ lines.push(' const isInput = activeEl && (activeEl.tagName === "INPUT" || activeEl.tagName === "TEXTAREA");');
33
+ lines.push(' const focusInfo = isInput ? {');
34
+ lines.push(' tag: activeEl.tagName.toLowerCase(),');
35
+ lines.push(' type: activeEl.type || "",');
36
+ lines.push(' placeholder: activeEl.placeholder || "",');
37
+ lines.push(' ariaLabel: activeEl.getAttribute("aria-label") || "",');
38
+ lines.push(' start: activeEl.selectionStart,');
39
+ lines.push(' end: activeEl.selectionEnd');
40
+ lines.push(' } : null;');
41
+ lines.push(' const newEl = render();');
42
+ lines.push(' if (currentEl) {');
43
+ lines.push(' container.replaceChild(newEl, currentEl);');
44
+ lines.push(' } else {');
45
+ lines.push(' container.appendChild(newEl);');
46
+ lines.push(' }');
47
+ lines.push(' currentEl = newEl;');
48
+ lines.push(' // Restore focus after re-render');
49
+ lines.push(' if (focusInfo) {');
50
+ lines.push(' let selector = focusInfo.tag;');
51
+ lines.push(' if (focusInfo.ariaLabel) selector += `[aria-label="${focusInfo.ariaLabel}"]`;');
52
+ lines.push(' else if (focusInfo.placeholder) selector += `[placeholder="${focusInfo.placeholder}"]`;');
53
+ lines.push(' const newActive = newEl.querySelector(selector);');
54
+ lines.push(' if (newActive) {');
55
+ lines.push(' newActive.focus();');
56
+ lines.push(' if (typeof focusInfo.start === "number") {');
57
+ lines.push(' try { newActive.setSelectionRange(focusInfo.start, focusInfo.end); } catch(e) {}');
58
+ lines.push(' }');
59
+ lines.push(' }');
60
+ lines.push(' }');
61
+ lines.push(' });');
62
+ if (hasInit) {
63
+ lines.push(' init();');
64
+ }
65
+ lines.push(' return { unmount: () => currentEl?.remove() };');
27
66
  lines.push(' }');
28
67
  lines.push('};');
29
68
  lines.push('');
@@ -177,8 +177,22 @@ export function transformFunctionBody(transformer, tokens) {
177
177
  let lastToken = null;
178
178
  let lastNonSpaceToken = null;
179
179
 
180
+ // Tokens that must follow } directly without semicolon
181
+ const NO_SEMI_BEFORE = new Set(['catch', 'finally', 'else']);
182
+
180
183
  const needsManualSemicolon = (token, nextToken, lastNonSpace) => {
181
184
  if (!token || lastNonSpace?.value === 'new') return false;
185
+ // Don't add semicolon after 'await' - it always needs its expression
186
+ if (lastNonSpace?.value === 'await') return false;
187
+ // For 'return': bare return followed by statement keyword needs semicolon
188
+ if (lastNonSpace?.value === 'return') {
189
+ // If followed by a statement keyword, it's a bare return - needs semicolon
190
+ if (token.type === 'IDENT' && STATEMENT_KEYWORDS.has(token.value)) return true;
191
+ if (STATEMENT_TOKEN_TYPES.has(token.type)) return true;
192
+ return false; // return expression - no semicolon
193
+ }
194
+ // Don't add semicolon before catch/finally/else after }
195
+ if (lastNonSpace?.type === 'RBRACE' && NO_SEMI_BEFORE.has(token.value)) return false;
182
196
  if (STATEMENT_TOKEN_TYPES.has(token.type)) return true;
183
197
  if (token.type !== 'IDENT') return false;
184
198
  if (STATEMENT_KEYWORDS.has(token.value)) return true;
@@ -245,29 +259,158 @@ export function transformFunctionBody(transformer, tokens) {
245
259
  lastNonSpaceToken = token;
246
260
  }
247
261
 
262
+ // Protect string literals from state var replacement
263
+ const stringPlaceholders = [];
264
+ const protectStrings = (str) => {
265
+ // Match strings and template literals, handling escapes
266
+ return str.replace(/(["'`])(?:\\.|(?!\1)[^\\])*\1/g, (match) => {
267
+ const index = stringPlaceholders.length;
268
+ stringPlaceholders.push(match);
269
+ return `__STRING_${index}__`;
270
+ });
271
+ };
272
+ const restoreStrings = (str) => {
273
+ return str.replace(/__STRING_(\d+)__/g, (_, index) => stringPlaceholders[parseInt(index)]);
274
+ };
275
+
276
+ // Protect strings before transformations
277
+ code = protectStrings(code);
278
+
248
279
  // Build patterns for state variable transformation
249
280
  const stateVarPattern = [...stateVars].join('|');
250
281
  const funcPattern = [...actionNames, ...BUILTIN_FUNCTIONS].join('|');
251
282
  const keywordsPattern = [...STATEMENT_KEYWORDS].join('|');
252
283
 
253
284
  // Transform state var assignments: stateVar = value -> stateVar.set(value)
285
+ // Match assignment and find end by tracking balanced brackets
254
286
  for (const stateVar of stateVars) {
255
- const boundaryPattern = `\\s+(?:${stateVarPattern})(?:\\s*=(?!=)|\\s*\\.set\\()|\\s+(?:${funcPattern})\\s*\\(|\\s+(?:${keywordsPattern})\\b|;|$`;
256
- const assignPattern = new RegExp(`\\b${stateVar}\\s*=(?!=)\\s*(.+?)(?=${boundaryPattern})`, 'g');
257
- code = code.replace(assignPattern, (_, value) => `${stateVar}.set(${value.trim()});`);
287
+ const pattern = new RegExp(`\\b${stateVar}\\s*=(?!=)`, 'g');
288
+ let match;
289
+ const replacements = [];
290
+
291
+ while ((match = pattern.exec(code)) !== null) {
292
+ const startIdx = match.index + match[0].length;
293
+
294
+ // Skip whitespace
295
+ let exprStart = startIdx;
296
+ while (exprStart < code.length && /\s/.test(code[exprStart])) exprStart++;
297
+
298
+ // Find end of expression with bracket balancing
299
+ let depth = 0;
300
+ let endIdx = exprStart;
301
+ let inString = false;
302
+ let stringChar = '';
303
+
304
+ for (let i = exprStart; i < code.length; i++) {
305
+ const ch = code[i];
306
+ const prevCh = i > 0 ? code[i-1] : '';
307
+
308
+ // Handle string literals
309
+ if (!inString && (ch === '"' || ch === "'" || ch === '`')) {
310
+ inString = true;
311
+ stringChar = ch;
312
+ endIdx = i + 1;
313
+ continue;
314
+ }
315
+ if (inString) {
316
+ if (ch === stringChar && prevCh !== '\\') {
317
+ inString = false;
318
+ }
319
+ endIdx = i + 1;
320
+ continue;
321
+ }
322
+
323
+ // Track bracket depth
324
+ if (ch === '(' || ch === '[' || ch === '{') {
325
+ depth++;
326
+ endIdx = i + 1;
327
+ continue;
328
+ }
329
+ if (ch === ')' || ch === ']' || ch === '}') {
330
+ if (depth > 0) {
331
+ depth--;
332
+ endIdx = i + 1;
333
+ continue;
334
+ }
335
+ // depth would go negative - this is a boundary (e.g., closing brace of if block)
336
+ break;
337
+ }
338
+
339
+ // At depth 0, check for statement boundaries
340
+ if (depth === 0) {
341
+ // Semicolon ends the expression
342
+ if (ch === ';') {
343
+ break;
344
+ }
345
+ // Check for whitespace followed by keyword/identifier that starts a new statement
346
+ if (/\s/.test(ch)) {
347
+ const rest = code.slice(i);
348
+ const keywordBoundary = new RegExp(`^\\s+(?:(?:${stateVarPattern})\\s*=(?!=)|(?:${keywordsPattern}|await|return)\\b|(?:${funcPattern})\\s*\\()`);
349
+ if (keywordBoundary.test(rest)) {
350
+ break;
351
+ }
352
+ }
353
+ }
354
+
355
+ endIdx = i + 1;
356
+ }
357
+
358
+ const value = code.slice(exprStart, endIdx).trim();
359
+ if (value) {
360
+ replacements.push({
361
+ start: match.index,
362
+ end: endIdx,
363
+ replacement: `${stateVar}.set(${value});`
364
+ });
365
+ }
366
+ }
367
+
368
+ // Apply replacements in reverse order
369
+ for (let i = replacements.length - 1; i >= 0; i--) {
370
+ const r = replacements[i];
371
+ code = code.slice(0, r.start) + r.replacement + code.slice(r.end);
372
+ }
258
373
  }
259
374
 
260
375
  // Clean up any double semicolons
261
376
  code = code.replace(/;+/g, ';');
262
377
  code = code.replace(/; ;/g, ';');
263
378
 
264
- // Replace state var reads
379
+ // Handle post-increment/decrement on state vars: stateVar++ -> ((v) => (stateVar.set(v + 1), v))(stateVar.get())
380
+ for (const stateVar of stateVars) {
381
+ // Post-increment: stateVar++ (returns old value)
382
+ code = code.replace(
383
+ new RegExp(`\\b${stateVar}\\s*\\+\\+`, 'g'),
384
+ `((v) => (${stateVar}.set(v + 1), v))(${stateVar}.get())`
385
+ );
386
+ // Post-decrement: stateVar-- (returns old value)
387
+ code = code.replace(
388
+ new RegExp(`\\b${stateVar}\\s*--`, 'g'),
389
+ `((v) => (${stateVar}.set(v - 1), v))(${stateVar}.get())`
390
+ );
391
+ // Pre-increment: ++stateVar (returns new value)
392
+ code = code.replace(
393
+ new RegExp(`\\+\\+\\s*${stateVar}\\b`, 'g'),
394
+ `(${stateVar}.set(${stateVar}.get() + 1), ${stateVar}.get())`
395
+ );
396
+ // Pre-decrement: --stateVar (returns new value)
397
+ code = code.replace(
398
+ new RegExp(`--\\s*${stateVar}\\b`, 'g'),
399
+ `(${stateVar}.set(${stateVar}.get() - 1), ${stateVar}.get())`
400
+ );
401
+ }
402
+
403
+ // Replace state var reads (not in assignments, not already with .get/.set)
404
+ // Allow spread operators (...stateVar) but block member access (obj.stateVar)
265
405
  for (const stateVar of stateVars) {
266
406
  code = code.replace(
267
- new RegExp(`(?<!\\.\\s*)\\b${stateVar}\\b(?!\\s*=(?!=)|\\s*\\(|\\s*\\.(?:get|set))`, 'g'),
407
+ new RegExp(`(?:(?<=\\.\\.\\.)|(?<!\\.))\\b${stateVar}\\b(?!\\s*=(?!=)|\\s*\\(|\\s*\\.(?:get|set))`, 'g'),
268
408
  `${stateVar}.get()`
269
409
  );
270
410
  }
271
411
 
412
+ // Restore protected strings
413
+ code = restoreStrings(code);
414
+
272
415
  return code.trim();
273
416
  }
@@ -39,6 +39,7 @@ export function generateImports(transformer) {
39
39
  'el',
40
40
  'text',
41
41
  'on',
42
+ 'bind',
42
43
  'list',
43
44
  'when',
44
45
  'mount',