ripple 0.2.182 → 0.2.184
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/package.json +4 -2
- package/src/compiler/errors.js +3 -1
- package/src/compiler/index.d.ts +2 -1
- package/src/compiler/phases/1-parse/index.js +525 -311
- package/src/compiler/phases/1-parse/style.js +3 -1
- package/src/compiler/phases/2-analyze/css-analyze.js +116 -97
- package/src/compiler/phases/2-analyze/index.js +81 -51
- package/src/compiler/phases/2-analyze/prune.js +200 -58
- package/src/compiler/phases/2-analyze/validation.js +9 -7
- package/src/compiler/phases/3-transform/client/index.js +871 -394
- package/src/compiler/phases/3-transform/segments.js +99 -53
- package/src/compiler/phases/3-transform/server/index.js +278 -121
- package/src/compiler/scope.js +51 -104
- package/src/compiler/types/index.d.ts +834 -197
- package/src/compiler/types/parse.d.ts +1668 -0
- package/src/compiler/utils.js +62 -74
- package/src/utils/ast.js +247 -192
- package/src/utils/builders.js +309 -247
- package/src/utils/sanitize_template_string.js +2 -2
package/src/compiler/utils.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
/** @import {
|
|
2
|
-
/** @import
|
|
1
|
+
/** @import { CommonContext, NameSpace, ScopeInterface } from '#compiler' */
|
|
2
|
+
/** @import * as AST from 'estree' */
|
|
3
|
+
|
|
3
4
|
import { build_assignment_value, extract_paths } from '../utils/ast.js';
|
|
4
5
|
import * as b from '../utils/builders.js';
|
|
5
6
|
import { is_capture_event, is_non_delegated, normalize_event_name } from '../utils/events.js';
|
|
@@ -172,8 +173,8 @@ export function is_dom_property(name) {
|
|
|
172
173
|
/**
|
|
173
174
|
* Determines if an event handler can be delegated
|
|
174
175
|
* @param {string} event_name
|
|
175
|
-
* @param {
|
|
176
|
-
* @param {
|
|
176
|
+
* @param {AST.Node} handler
|
|
177
|
+
* @param {CommonContext} context
|
|
177
178
|
* @returns {boolean}
|
|
178
179
|
*/
|
|
179
180
|
export function is_delegated_event(event_name, handler, context) {
|
|
@@ -184,7 +185,7 @@ export function is_delegated_event(event_name, handler, context) {
|
|
|
184
185
|
is_non_delegated(normalize_event_name(event_name)) ||
|
|
185
186
|
(handler.type !== 'FunctionExpression' &&
|
|
186
187
|
handler.type !== 'ArrowFunctionExpression' &&
|
|
187
|
-
!is_declared_function_within_component(/** @type {Identifier}*/ (handler), context))
|
|
188
|
+
!is_declared_function_within_component(/** @type {AST.Identifier}*/ (handler), context))
|
|
188
189
|
) {
|
|
189
190
|
return false;
|
|
190
191
|
}
|
|
@@ -193,7 +194,7 @@ export function is_delegated_event(event_name, handler, context) {
|
|
|
193
194
|
|
|
194
195
|
/**
|
|
195
196
|
* Returns true if context is inside a top-level await
|
|
196
|
-
* @param {
|
|
197
|
+
* @param {CommonContext} context
|
|
197
198
|
* @returns {boolean}
|
|
198
199
|
*/
|
|
199
200
|
export function is_top_level_await(context) {
|
|
@@ -205,7 +206,7 @@ export function is_top_level_await(context) {
|
|
|
205
206
|
const context_node = context.path[i];
|
|
206
207
|
const type = context_node.type;
|
|
207
208
|
|
|
208
|
-
if (
|
|
209
|
+
if (context_node.type === 'Component') {
|
|
209
210
|
return true;
|
|
210
211
|
}
|
|
211
212
|
|
|
@@ -222,7 +223,7 @@ export function is_top_level_await(context) {
|
|
|
222
223
|
|
|
223
224
|
/**
|
|
224
225
|
* Returns true if context is inside a Component node
|
|
225
|
-
* @param {
|
|
226
|
+
* @param {CommonContext} context
|
|
226
227
|
* @param {boolean} [includes_functions=false]
|
|
227
228
|
* @returns {boolean}
|
|
228
229
|
*/
|
|
@@ -239,7 +240,7 @@ export function is_inside_component(context, includes_functions = false) {
|
|
|
239
240
|
) {
|
|
240
241
|
return false;
|
|
241
242
|
}
|
|
242
|
-
if (
|
|
243
|
+
if (context_node.type === 'Component') {
|
|
243
244
|
return true;
|
|
244
245
|
}
|
|
245
246
|
}
|
|
@@ -248,7 +249,7 @@ export function is_inside_component(context, includes_functions = false) {
|
|
|
248
249
|
|
|
249
250
|
/**
|
|
250
251
|
* Returns true if context is inside a component-level function
|
|
251
|
-
* @param {
|
|
252
|
+
* @param {CommonContext} context
|
|
252
253
|
* @returns {boolean}
|
|
253
254
|
*/
|
|
254
255
|
export function is_component_level_function(context) {
|
|
@@ -256,7 +257,10 @@ export function is_component_level_function(context) {
|
|
|
256
257
|
const context_node = context.path[i];
|
|
257
258
|
const type = context_node.type;
|
|
258
259
|
|
|
259
|
-
if (
|
|
260
|
+
if (
|
|
261
|
+
type === 'BlockStatement' &&
|
|
262
|
+
context_node.body.find((n) => /** @type {AST.Node} */ (n).type === 'Component')
|
|
263
|
+
) {
|
|
260
264
|
return true;
|
|
261
265
|
}
|
|
262
266
|
|
|
@@ -273,8 +277,8 @@ export function is_component_level_function(context) {
|
|
|
273
277
|
|
|
274
278
|
/**
|
|
275
279
|
* Returns true if callee is a Ripple track call
|
|
276
|
-
* @param {Expression | Super} callee
|
|
277
|
-
* @param {
|
|
280
|
+
* @param {AST.Expression | AST.Super} callee
|
|
281
|
+
* @param {CommonContext} context
|
|
278
282
|
* @returns {boolean}
|
|
279
283
|
*/
|
|
280
284
|
export function is_ripple_track_call(callee, context) {
|
|
@@ -294,7 +298,7 @@ export function is_ripple_track_call(callee, context) {
|
|
|
294
298
|
|
|
295
299
|
/**
|
|
296
300
|
* Returns true if context is inside a call expression
|
|
297
|
-
* @param {
|
|
301
|
+
* @param {CommonContext} context
|
|
298
302
|
* @returns {boolean}
|
|
299
303
|
*/
|
|
300
304
|
export function is_inside_call_expression(context) {
|
|
@@ -322,7 +326,7 @@ export function is_inside_call_expression(context) {
|
|
|
322
326
|
|
|
323
327
|
/**
|
|
324
328
|
* Returns true if node is a static value (Literal, ArrayExpression, etc)
|
|
325
|
-
* @param {Node} node
|
|
329
|
+
* @param {AST.Node} node
|
|
326
330
|
* @returns {boolean}
|
|
327
331
|
*/
|
|
328
332
|
export function is_value_static(node) {
|
|
@@ -344,8 +348,8 @@ export function is_value_static(node) {
|
|
|
344
348
|
|
|
345
349
|
/**
|
|
346
350
|
* Returns true if callee is a Ripple import
|
|
347
|
-
* @param {Expression} callee
|
|
348
|
-
* @param {
|
|
351
|
+
* @param {AST.Expression} callee
|
|
352
|
+
* @param {CommonContext} context
|
|
349
353
|
* @returns {boolean}
|
|
350
354
|
*/
|
|
351
355
|
export function is_ripple_import(callee, context) {
|
|
@@ -380,16 +384,16 @@ export function is_ripple_import(callee, context) {
|
|
|
380
384
|
|
|
381
385
|
/**
|
|
382
386
|
* Returns true if node is a function declared within a component
|
|
383
|
-
* @param {
|
|
384
|
-
* @param {
|
|
387
|
+
* @param {AST.Node} node
|
|
388
|
+
* @param {CommonContext} context
|
|
385
389
|
* @returns {boolean}
|
|
386
390
|
*/
|
|
387
391
|
export function is_declared_function_within_component(node, context) {
|
|
388
|
-
const component = context.path?.find(
|
|
392
|
+
const component = context.path?.find((n) => n.type === 'Component');
|
|
389
393
|
|
|
390
394
|
if (node.type === 'Identifier' && component) {
|
|
391
395
|
const binding = context.state.scope.get(node.name);
|
|
392
|
-
const component_scope = context.state.scopes
|
|
396
|
+
const component_scope = context.state.scopes?.get(component);
|
|
393
397
|
|
|
394
398
|
if (binding !== null && component_scope !== null) {
|
|
395
399
|
if (
|
|
@@ -400,6 +404,7 @@ export function is_declared_function_within_component(node, context) {
|
|
|
400
404
|
) {
|
|
401
405
|
return false;
|
|
402
406
|
}
|
|
407
|
+
/** @type {ScopeInterface | null} */
|
|
403
408
|
let scope = binding.scope;
|
|
404
409
|
|
|
405
410
|
while (scope !== null) {
|
|
@@ -415,10 +420,10 @@ export function is_declared_function_within_component(node, context) {
|
|
|
415
420
|
}
|
|
416
421
|
/**
|
|
417
422
|
* Visits and transforms an assignment expression
|
|
418
|
-
* @param {AssignmentExpression} node
|
|
419
|
-
* @param {
|
|
423
|
+
* @param {AST.AssignmentExpression} node
|
|
424
|
+
* @param {CommonContext} context
|
|
420
425
|
* @param {Function} build_assignment
|
|
421
|
-
* @returns {Expression | AssignmentExpression | null}
|
|
426
|
+
* @returns {AST.Expression | AST.AssignmentExpression | null}
|
|
422
427
|
*/
|
|
423
428
|
export function visit_assignment_expression(node, context, build_assignment) {
|
|
424
429
|
if (
|
|
@@ -426,7 +431,7 @@ export function visit_assignment_expression(node, context, build_assignment) {
|
|
|
426
431
|
node.left.type === 'ObjectPattern' ||
|
|
427
432
|
node.left.type === 'RestElement'
|
|
428
433
|
) {
|
|
429
|
-
const value = /** @type {Expression} */ (context.visit(node.right));
|
|
434
|
+
const value = /** @type {AST.Expression} */ (context.visit(node.right));
|
|
430
435
|
const should_cache = value.type !== 'Identifier';
|
|
431
436
|
const rhs = should_cache ? b.id('$$value') : value;
|
|
432
437
|
|
|
@@ -442,8 +447,8 @@ export function visit_assignment_expression(node, context, build_assignment) {
|
|
|
442
447
|
assignment ??
|
|
443
448
|
b.assignment(
|
|
444
449
|
'=',
|
|
445
|
-
/** @type {Pattern} */ (context.visit(path.node)),
|
|
446
|
-
/** @type {Expression} */ (context.visit(value)),
|
|
450
|
+
/** @type {AST.Pattern} */ (context.visit(path.node)),
|
|
451
|
+
/** @type {AST.Expression} */ (context.visit(value)),
|
|
447
452
|
)
|
|
448
453
|
);
|
|
449
454
|
});
|
|
@@ -453,7 +458,7 @@ export function visit_assignment_expression(node, context, build_assignment) {
|
|
|
453
458
|
return null;
|
|
454
459
|
}
|
|
455
460
|
|
|
456
|
-
const is_standalone = context.path.at(-1)
|
|
461
|
+
const is_standalone = context.path.at(-1)?.type.endsWith('Statement');
|
|
457
462
|
const sequence = b.sequence(assignments);
|
|
458
463
|
|
|
459
464
|
if (!is_standalone) {
|
|
@@ -486,11 +491,11 @@ export function visit_assignment_expression(node, context, build_assignment) {
|
|
|
486
491
|
|
|
487
492
|
/**
|
|
488
493
|
* Builds an assignment node, possibly transforming for reactivity
|
|
489
|
-
* @param {AssignmentOperator} operator
|
|
490
|
-
* @param {Pattern
|
|
491
|
-
* @param {Expression} right
|
|
492
|
-
* @param {
|
|
493
|
-
* @returns {Expression|null}
|
|
494
|
+
* @param {AST.AssignmentOperator} operator
|
|
495
|
+
* @param {AST.Pattern} left
|
|
496
|
+
* @param {AST.Expression} right
|
|
497
|
+
* @param {CommonContext} context
|
|
498
|
+
* @returns {AST.Expression | null}
|
|
494
499
|
*/
|
|
495
500
|
export function build_assignment(operator, left, right, context) {
|
|
496
501
|
let object = left;
|
|
@@ -511,34 +516,16 @@ export function build_assignment(operator, left, right, context) {
|
|
|
511
516
|
|
|
512
517
|
// reassignment
|
|
513
518
|
if (object === left || (left.type === 'MemberExpression' && left.computed && operator === '=')) {
|
|
514
|
-
const assign_fn = transform?.assign
|
|
519
|
+
const assign_fn = transform?.assign;
|
|
515
520
|
if (assign_fn) {
|
|
516
|
-
let value = /** @type {Expression} */ (
|
|
521
|
+
let value = /** @type {AST.Expression} */ (
|
|
517
522
|
context.visit(build_assignment_value(operator, left, right))
|
|
518
523
|
);
|
|
519
524
|
|
|
520
|
-
return assign_fn(
|
|
521
|
-
object,
|
|
522
|
-
value,
|
|
523
|
-
left.type === 'MemberExpression' && left.computed
|
|
524
|
-
? context.visit(left.property)
|
|
525
|
-
: undefined,
|
|
526
|
-
);
|
|
525
|
+
return assign_fn(object, value);
|
|
527
526
|
}
|
|
528
527
|
}
|
|
529
528
|
|
|
530
|
-
// mutation
|
|
531
|
-
if (transform?.mutate) {
|
|
532
|
-
return transform.mutate(
|
|
533
|
-
object,
|
|
534
|
-
b.assignment(
|
|
535
|
-
operator,
|
|
536
|
-
/** @type {Pattern} */ (context.visit(left)),
|
|
537
|
-
/** @type {Expression} */ (context.visit(right)),
|
|
538
|
-
),
|
|
539
|
-
);
|
|
540
|
-
}
|
|
541
|
-
|
|
542
529
|
return null;
|
|
543
530
|
}
|
|
544
531
|
|
|
@@ -547,7 +534,7 @@ const CONTENT_REGEX = /[&<]/g;
|
|
|
547
534
|
|
|
548
535
|
/**
|
|
549
536
|
* Escapes HTML special characters in a string
|
|
550
|
-
* @param {string} value
|
|
537
|
+
* @param {string | number | bigint | boolean | RegExp | null | undefined} value
|
|
551
538
|
* @param {boolean} [is_attr=false]
|
|
552
539
|
* @returns {string}
|
|
553
540
|
*/
|
|
@@ -586,26 +573,27 @@ export function hash(str) {
|
|
|
586
573
|
|
|
587
574
|
/**
|
|
588
575
|
* Returns true if node is a DOM element (not a component)
|
|
589
|
-
* @param {
|
|
576
|
+
* @param {AST.Node} node
|
|
590
577
|
* @returns {boolean}
|
|
591
578
|
*/
|
|
592
579
|
export function is_element_dom_element(node) {
|
|
580
|
+
const id = /** @type {AST.Element} */ (node).id;
|
|
593
581
|
return (
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
!
|
|
582
|
+
id.type === 'Identifier' &&
|
|
583
|
+
id.name[0].toLowerCase() === id.name[0] &&
|
|
584
|
+
id.name !== 'children' &&
|
|
585
|
+
!id.tracked
|
|
598
586
|
);
|
|
599
587
|
}
|
|
600
588
|
|
|
601
589
|
/**
|
|
602
590
|
* Normalizes children nodes (merges adjacent text, removes empty)
|
|
603
|
-
* @param {
|
|
604
|
-
* @param {
|
|
605
|
-
* @returns {
|
|
591
|
+
* @param {AST.Node[]} children
|
|
592
|
+
* @param {CommonContext} context
|
|
593
|
+
* @returns {AST.Node[]}
|
|
606
594
|
*/
|
|
607
595
|
export function normalize_children(children, context) {
|
|
608
|
-
/** @type {
|
|
596
|
+
/** @type {AST.Node[]} */
|
|
609
597
|
const normalized = [];
|
|
610
598
|
|
|
611
599
|
for (const node of children) {
|
|
@@ -636,9 +624,9 @@ export function normalize_children(children, context) {
|
|
|
636
624
|
}
|
|
637
625
|
|
|
638
626
|
/**
|
|
639
|
-
* @param {
|
|
640
|
-
* @param {
|
|
641
|
-
* @param {
|
|
627
|
+
* @param {AST.Node} node
|
|
628
|
+
* @param {AST.Node[]} normalized
|
|
629
|
+
* @param {CommonContext} context
|
|
642
630
|
*/
|
|
643
631
|
function normalize_child(node, normalized, context) {
|
|
644
632
|
if (node.type === 'EmptyStatement') {
|
|
@@ -657,7 +645,7 @@ function normalize_child(node, normalized, context) {
|
|
|
657
645
|
}
|
|
658
646
|
|
|
659
647
|
/**
|
|
660
|
-
* @param {
|
|
648
|
+
* @param {CommonContext} context
|
|
661
649
|
*/
|
|
662
650
|
export function get_parent_block_node(context) {
|
|
663
651
|
const path = context.path;
|
|
@@ -686,9 +674,9 @@ export function get_parent_block_node(context) {
|
|
|
686
674
|
|
|
687
675
|
/**
|
|
688
676
|
* Builds a getter for a tracked identifier
|
|
689
|
-
* @param {Identifier} node
|
|
690
|
-
* @param {
|
|
691
|
-
* @returns {Expression | Identifier}
|
|
677
|
+
* @param {AST.Identifier} node
|
|
678
|
+
* @param {CommonContext} context
|
|
679
|
+
* @returns {AST.Expression | AST.Identifier}
|
|
692
680
|
*/
|
|
693
681
|
export function build_getter(node, context) {
|
|
694
682
|
const state = context.state;
|
|
@@ -704,7 +692,7 @@ export function build_getter(node, context) {
|
|
|
704
692
|
const read_fn = transform?.read;
|
|
705
693
|
|
|
706
694
|
if (read_fn) {
|
|
707
|
-
return read_fn(node
|
|
695
|
+
return read_fn(node);
|
|
708
696
|
}
|
|
709
697
|
}
|
|
710
698
|
}
|
|
@@ -715,8 +703,8 @@ export function build_getter(node, context) {
|
|
|
715
703
|
/**
|
|
716
704
|
* Determines the namespace for child elements
|
|
717
705
|
* @param {string} element_name
|
|
718
|
-
* @param {
|
|
719
|
-
* @returns {
|
|
706
|
+
* @param {NameSpace} current_namespace
|
|
707
|
+
* @returns {NameSpace}
|
|
720
708
|
*/
|
|
721
709
|
export function determine_namespace_for_children(element_name, current_namespace) {
|
|
722
710
|
if (element_name === 'foreignObject') {
|