ripple 0.2.45 → 0.2.47

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.
Files changed (36) hide show
  1. package/package.json +1 -1
  2. package/src/compiler/phases/1-parse/index.js +8 -1
  3. package/src/compiler/phases/2-analyze/index.js +640 -650
  4. package/src/compiler/phases/3-transform/index.js +1877 -1813
  5. package/src/compiler/phases/3-transform/segments.js +2 -2
  6. package/src/compiler/utils.js +600 -523
  7. package/src/jsx-runtime.js +12 -12
  8. package/src/runtime/array.js +611 -609
  9. package/src/runtime/index.js +29 -17
  10. package/src/runtime/internal/client/array.js +128 -128
  11. package/src/runtime/internal/client/blocks.js +206 -207
  12. package/src/runtime/internal/client/constants.js +2 -2
  13. package/src/runtime/internal/client/context.js +40 -40
  14. package/src/runtime/internal/client/events.js +191 -191
  15. package/src/runtime/internal/client/for.js +355 -355
  16. package/src/runtime/internal/client/if.js +25 -25
  17. package/src/runtime/internal/client/index.js +57 -52
  18. package/src/runtime/internal/client/operations.js +32 -32
  19. package/src/runtime/internal/client/portal.js +20 -20
  20. package/src/runtime/internal/client/render.js +132 -132
  21. package/src/runtime/internal/client/runtime.js +858 -824
  22. package/src/runtime/internal/client/template.js +36 -36
  23. package/src/runtime/internal/client/try.js +113 -113
  24. package/src/runtime/internal/client/types.d.ts +10 -10
  25. package/src/runtime/internal/client/utils.js +5 -5
  26. package/src/runtime/map.js +139 -139
  27. package/src/runtime/set.js +130 -130
  28. package/src/utils/ast.js +189 -189
  29. package/src/utils/builders.js +244 -244
  30. package/src/utils/sanitize_template_string.js +1 -1
  31. package/tests/__snapshots__/composite.test.ripple.snap +1 -1
  32. package/tests/accessors-props.test.ripple +9 -9
  33. package/tests/basic.test.ripple +4 -4
  34. package/tests/boundaries.test.ripple +17 -17
  35. package/tests/composite.test.ripple +43 -72
  36. package/types/index.d.ts +6 -2
@@ -7,270 +7,270 @@ import { active_block, untrack } from './runtime';
7
7
  import { array_from, is_array } from './utils';
8
8
 
9
9
  function create_item(anchor, value, render_fn) {
10
- var b = branch(() => {
11
- render_fn(anchor, value);
12
- });
13
- return b;
10
+ var b = branch(() => {
11
+ render_fn(anchor, value);
12
+ });
13
+ return b;
14
14
  }
15
15
 
16
16
  function move(block, anchor) {
17
- var node = block.s.start;
18
- var end = block.s.end;
19
-
20
- if (node === end) {
21
- anchor.before(node);
22
- return;
23
- }
24
- while (node !== end) {
25
- var next_node = /** @type {TemplateNode} */ (get_next_sibling(node));
26
- anchor.before(node);
27
- node = next_node;
28
- }
17
+ var node = block.s.start;
18
+ var end = block.s.end;
19
+
20
+ if (node === end) {
21
+ anchor.before(node);
22
+ return;
23
+ }
24
+ while (node !== end) {
25
+ var next_node = /** @type {TemplateNode} */ (get_next_sibling(node));
26
+ anchor.before(node);
27
+ node = next_node;
28
+ }
29
29
  }
30
30
 
31
31
  export function for_block(node, get_collection, render_fn, flags) {
32
- var is_controlled = (flags & IS_CONTROLLED) !== 0;
33
- var anchor = node;
34
-
35
- if (is_controlled) {
36
- anchor = node.appendChild(create_text());
37
- }
38
-
39
- render(() => {
40
- var block = active_block;
41
- var collection = get_collection();
42
- var array = is_array(collection)
43
- ? collection
44
- : collection == null
45
- ? []
46
- : array_from(collection);
47
-
48
- if (array[TRACKED_OBJECT] !== undefined) {
49
- array = get_all_elements(collection);
50
- collection.$length;
51
- }
52
-
53
- untrack(() => reconcile(anchor, block, array, render_fn, is_controlled));
54
- }, FOR_BLOCK);
32
+ var is_controlled = (flags & IS_CONTROLLED) !== 0;
33
+ var anchor = node;
34
+
35
+ if (is_controlled) {
36
+ anchor = node.appendChild(create_text());
37
+ }
38
+
39
+ render(() => {
40
+ var block = active_block;
41
+ var collection = get_collection();
42
+ var array = is_array(collection)
43
+ ? collection
44
+ : collection == null
45
+ ? []
46
+ : array_from(collection);
47
+
48
+ if (array[TRACKED_OBJECT] !== undefined) {
49
+ array = get_all_elements(collection);
50
+ collection.$length;
51
+ }
52
+
53
+ untrack(() => reconcile(anchor, block, array, render_fn, is_controlled));
54
+ }, FOR_BLOCK);
55
55
  }
56
56
 
57
57
  function reconcile_fast_clear(anchor, block, array) {
58
- var state = block.s;
59
- var parent_node = anchor.parentNode;
60
- parent_node.textContent = '';
61
- destroy_block_children(block);
62
- parent_node.append(anchor);
63
- state.array = array;
64
- state.blocks = [];
58
+ var state = block.s;
59
+ var parent_node = anchor.parentNode;
60
+ parent_node.textContent = '';
61
+ destroy_block_children(block);
62
+ parent_node.append(anchor);
63
+ state.array = array;
64
+ state.blocks = [];
65
65
  }
66
66
 
67
67
  function reconcile(anchor, block, b, render_fn, is_controlled) {
68
- var state = block.s;
69
-
70
- if (state === null) {
71
- state = block.s = {
72
- array: [],
73
- blocks: [],
74
- };
75
- }
76
-
77
- var a = state.array;
78
- var a_length = a.length;
79
- var b_length = b.length;
80
- var j = 0;
81
-
82
- // Fast-path for clear
83
- if (is_controlled && b_length === 0) {
84
- if (a_length > 0) {
85
- reconcile_fast_clear(anchor, block, b);
86
- }
87
- return;
88
- }
89
- var b_blocks = Array(b_length);
90
-
91
- // Fast-path for create
92
- if (a_length === 0) {
93
- for (; j < b_length; j++) {
94
- b_blocks[j] = create_item(anchor, b[j], render_fn);
95
- }
96
- state.array = b;
97
- state.blocks = b_blocks;
98
- return;
99
- }
100
-
101
- var a_blocks = state.blocks;
102
- var a_val = a[j];
103
- var b_val = b[j];
104
- var a_end = a_length - 1;
105
- var b_end = b_length - 1;
106
-
107
- outer: {
108
- while (a_val === b_val) {
109
- a[j] = b_val;
110
- b_blocks[j] = a_blocks[j];
111
- ++j;
112
- if (j > a_end || j > b_end) {
113
- break outer;
114
- }
115
- a_val = a[j];
116
- b_val = b[j];
117
- }
118
-
119
- a_val = a[a_end];
120
- b_val = b[b_end];
121
-
122
- while (a_val === b_val) {
123
- a[a_end] = b_val;
124
- b_blocks[b_end] = a_blocks[a_end];
125
- a_end--;
126
- b_end--;
127
- if (j > a_end || j > b_end) {
128
- break outer;
129
- }
130
- a_val = a[a_end];
131
- b_val = b[b_end];
132
- }
133
- }
134
-
135
- if (j > a_end) {
136
- if (j <= b_end) {
137
- while (j <= b_end) {
138
- b_val = b[j];
139
- var target = j >= a_length ? anchor : a_blocks[j].s.start;
140
- b_blocks[j++] = create_item(target, b_val, render_fn);
141
- }
142
- }
143
- } else if (j > b_end) {
144
- while (j <= a_end) {
145
- destroy_block(a_blocks[j++]);
146
- }
147
- } else {
148
- var a_start = j;
149
- var b_start = j;
150
- var a_left = a_end - j + 1;
151
- var b_left = b_end - j + 1;
152
- var fast_path_removal = is_controlled && a_left === a_length;
153
- var sources = new Int32Array(b_left + 1);
154
- var moved = false;
155
- var pos = 0;
156
- var patched = 0;
157
- var i = 0;
158
-
159
- // When sizes are small, just loop them through
160
- if (b_length < 4 || (a_left | b_left) < 32) {
161
- for (i = a_start; i <= a_end; ++i) {
162
- a_val = a[i];
163
- if (patched < b_left) {
164
- for (j = b_start; j <= b_end; j++) {
165
- b_val = b[j];
166
- if (a_val === b_val) {
167
- sources[j - b_start] = i + 1;
168
- if (fast_path_removal) {
169
- fast_path_removal = false;
170
- // while (a_start < i) {
171
- // debugger
172
- // destroy_block(a_blocks[a_start++]);
173
- // }
174
- }
175
- if (pos > j) {
176
- moved = true;
177
- } else {
178
- pos = j;
179
- }
180
- b_blocks[j] = a_blocks[i];
181
- ++patched;
182
- break;
183
- }
184
- }
185
- if (!fast_path_removal && j > b_end) {
186
- destroy_block(a_blocks[i]);
187
- }
188
- } else if (!fast_path_removal) {
189
- destroy_block(a_blocks[i]);
190
- }
191
- }
192
- } else {
193
- var map = new Map();
194
-
195
- for (i = b_start; i <= b_end; ++i) {
196
- map.set(b[i], i);
197
- }
198
-
199
- for (i = a_start; i <= a_end; ++i) {
200
- a_val = a[i];
201
-
202
- if (patched < b_left) {
203
- j = map.get(a_val);
204
-
205
- if (j !== undefined) {
206
- if (fast_path_removal) {
207
- fast_path_removal = false;
208
- // while (i > aStart) {
209
- // remove(a[aStart++], dom, animations);
210
- // }
211
- }
212
- sources[j - b_start] = i + 1;
213
- if (pos > j) {
214
- moved = true;
215
- } else {
216
- pos = j;
217
- }
218
- b_val = b[j];
219
- b_blocks[j] = a_blocks[i];
220
- ++patched;
221
- } else if (!fast_path_removal) {
222
- destroy_block(a_blocks[i]);
223
- }
224
- } else if (!fast_path_removal) {
225
- destroy_block(a_blocks[i]);
226
- }
227
- }
228
- }
229
- }
230
-
231
- if (fast_path_removal) {
232
- reconcile_fast_clear(anchor, block, []);
233
- reconcile(anchor, block, b, render_fn, is_controlled);
234
- return;
235
- } else if (moved) {
236
- var next_pos = 0;
237
- var seq = lis_algorithm(sources);
238
- j = seq.length - 1;
239
-
240
- for (i = b_left - 1; i >= 0; i--) {
241
- if (sources[i] === 0) {
242
- pos = i + b_start;
243
- b_val = b[pos];
244
- next_pos = pos + 1;
245
-
246
- var target = next_pos < b_length ? b_blocks[next_pos].s.start : anchor;
247
- b_blocks[pos] = create_item(target, b_val, render_fn);
248
- } else if (j < 0 || i !== seq[j]) {
249
- pos = i + b_start;
250
- b_val = b[pos];
251
- next_pos = pos + 1;
252
-
253
- var target = next_pos < b_length ? b_blocks[next_pos].s.start : anchor;
254
- move(b_blocks[pos], target);
255
- } else {
256
- j--;
257
- }
258
- }
259
- } else if (patched !== b_left) {
260
- for (i = b_left - 1; i >= 0; i--) {
261
- if (sources[i] === 0) {
262
- pos = i + b_start;
263
- b_val = b[pos];
264
- next_pos = pos + 1;
265
-
266
- var target = next_pos < b_length ? b_blocks[next_pos].s.start : anchor;
267
- b_blocks[pos] = create_item(target, b_val, render_fn);
268
- }
269
- }
270
- }
271
-
272
- state.array = b;
273
- state.blocks = b_blocks;
68
+ var state = block.s;
69
+
70
+ if (state === null) {
71
+ state = block.s = {
72
+ array: [],
73
+ blocks: [],
74
+ };
75
+ }
76
+
77
+ var a = state.array;
78
+ var a_length = a.length;
79
+ var b_length = b.length;
80
+ var j = 0;
81
+
82
+ // Fast-path for clear
83
+ if (is_controlled && b_length === 0) {
84
+ if (a_length > 0) {
85
+ reconcile_fast_clear(anchor, block, b);
86
+ }
87
+ return;
88
+ }
89
+ var b_blocks = Array(b_length);
90
+
91
+ // Fast-path for create
92
+ if (a_length === 0) {
93
+ for (; j < b_length; j++) {
94
+ b_blocks[j] = create_item(anchor, b[j], render_fn);
95
+ }
96
+ state.array = b;
97
+ state.blocks = b_blocks;
98
+ return;
99
+ }
100
+
101
+ var a_blocks = state.blocks;
102
+ var a_val = a[j];
103
+ var b_val = b[j];
104
+ var a_end = a_length - 1;
105
+ var b_end = b_length - 1;
106
+
107
+ outer: {
108
+ while (a_val === b_val) {
109
+ a[j] = b_val;
110
+ b_blocks[j] = a_blocks[j];
111
+ ++j;
112
+ if (j > a_end || j > b_end) {
113
+ break outer;
114
+ }
115
+ a_val = a[j];
116
+ b_val = b[j];
117
+ }
118
+
119
+ a_val = a[a_end];
120
+ b_val = b[b_end];
121
+
122
+ while (a_val === b_val) {
123
+ a[a_end] = b_val;
124
+ b_blocks[b_end] = a_blocks[a_end];
125
+ a_end--;
126
+ b_end--;
127
+ if (j > a_end || j > b_end) {
128
+ break outer;
129
+ }
130
+ a_val = a[a_end];
131
+ b_val = b[b_end];
132
+ }
133
+ }
134
+
135
+ if (j > a_end) {
136
+ if (j <= b_end) {
137
+ while (j <= b_end) {
138
+ b_val = b[j];
139
+ var target = j >= a_length ? anchor : a_blocks[j].s.start;
140
+ b_blocks[j++] = create_item(target, b_val, render_fn);
141
+ }
142
+ }
143
+ } else if (j > b_end) {
144
+ while (j <= a_end) {
145
+ destroy_block(a_blocks[j++]);
146
+ }
147
+ } else {
148
+ var a_start = j;
149
+ var b_start = j;
150
+ var a_left = a_end - j + 1;
151
+ var b_left = b_end - j + 1;
152
+ var fast_path_removal = is_controlled && a_left === a_length;
153
+ var sources = new Int32Array(b_left + 1);
154
+ var moved = false;
155
+ var pos = 0;
156
+ var patched = 0;
157
+ var i = 0;
158
+
159
+ // When sizes are small, just loop them through
160
+ if (b_length < 4 || (a_left | b_left) < 32) {
161
+ for (i = a_start; i <= a_end; ++i) {
162
+ a_val = a[i];
163
+ if (patched < b_left) {
164
+ for (j = b_start; j <= b_end; j++) {
165
+ b_val = b[j];
166
+ if (a_val === b_val) {
167
+ sources[j - b_start] = i + 1;
168
+ if (fast_path_removal) {
169
+ fast_path_removal = false;
170
+ // while (a_start < i) {
171
+ // debugger
172
+ // destroy_block(a_blocks[a_start++]);
173
+ // }
174
+ }
175
+ if (pos > j) {
176
+ moved = true;
177
+ } else {
178
+ pos = j;
179
+ }
180
+ b_blocks[j] = a_blocks[i];
181
+ ++patched;
182
+ break;
183
+ }
184
+ }
185
+ if (!fast_path_removal && j > b_end) {
186
+ destroy_block(a_blocks[i]);
187
+ }
188
+ } else if (!fast_path_removal) {
189
+ destroy_block(a_blocks[i]);
190
+ }
191
+ }
192
+ } else {
193
+ var map = new Map();
194
+
195
+ for (i = b_start; i <= b_end; ++i) {
196
+ map.set(b[i], i);
197
+ }
198
+
199
+ for (i = a_start; i <= a_end; ++i) {
200
+ a_val = a[i];
201
+
202
+ if (patched < b_left) {
203
+ j = map.get(a_val);
204
+
205
+ if (j !== undefined) {
206
+ if (fast_path_removal) {
207
+ fast_path_removal = false;
208
+ // while (i > aStart) {
209
+ // remove(a[aStart++], dom, animations);
210
+ // }
211
+ }
212
+ sources[j - b_start] = i + 1;
213
+ if (pos > j) {
214
+ moved = true;
215
+ } else {
216
+ pos = j;
217
+ }
218
+ b_val = b[j];
219
+ b_blocks[j] = a_blocks[i];
220
+ ++patched;
221
+ } else if (!fast_path_removal) {
222
+ destroy_block(a_blocks[i]);
223
+ }
224
+ } else if (!fast_path_removal) {
225
+ destroy_block(a_blocks[i]);
226
+ }
227
+ }
228
+ }
229
+ }
230
+
231
+ if (fast_path_removal) {
232
+ reconcile_fast_clear(anchor, block, []);
233
+ reconcile(anchor, block, b, render_fn, is_controlled);
234
+ return;
235
+ } else if (moved) {
236
+ var next_pos = 0;
237
+ var seq = lis_algorithm(sources);
238
+ j = seq.length - 1;
239
+
240
+ for (i = b_left - 1; i >= 0; i--) {
241
+ if (sources[i] === 0) {
242
+ pos = i + b_start;
243
+ b_val = b[pos];
244
+ next_pos = pos + 1;
245
+
246
+ var target = next_pos < b_length ? b_blocks[next_pos].s.start : anchor;
247
+ b_blocks[pos] = create_item(target, b_val, render_fn);
248
+ } else if (j < 0 || i !== seq[j]) {
249
+ pos = i + b_start;
250
+ b_val = b[pos];
251
+ next_pos = pos + 1;
252
+
253
+ var target = next_pos < b_length ? b_blocks[next_pos].s.start : anchor;
254
+ move(b_blocks[pos], target);
255
+ } else {
256
+ j--;
257
+ }
258
+ }
259
+ } else if (patched !== b_left) {
260
+ for (i = b_left - 1; i >= 0; i--) {
261
+ if (sources[i] === 0) {
262
+ pos = i + b_start;
263
+ b_val = b[pos];
264
+ next_pos = pos + 1;
265
+
266
+ var target = next_pos < b_length ? b_blocks[next_pos].s.start : anchor;
267
+ b_blocks[pos] = create_item(target, b_val, render_fn);
268
+ }
269
+ }
270
+ }
271
+
272
+ state.array = b;
273
+ state.blocks = b_blocks;
274
274
  }
275
275
 
276
276
  let result;
@@ -278,110 +278,110 @@ let p;
278
278
  let maxLen = 0;
279
279
  // https://en.wikipedia.org/wiki/Longest_increasing_subsequence
280
280
  function lis_algorithm(arr) {
281
- let arrI = 0;
282
- let i = 0;
283
- let j = 0;
284
- let k = 0;
285
- let u = 0;
286
- let v = 0;
287
- let c = 0;
288
- var len = arr.length;
289
-
290
- if (len > maxLen) {
291
- maxLen = len;
292
- result = new Int32Array(len);
293
- p = new Int32Array(len);
294
- }
295
-
296
- for (; i < len; ++i) {
297
- arrI = arr[i];
298
-
299
- if (arrI !== 0) {
300
- j = result[k];
301
- if (arr[j] < arrI) {
302
- p[i] = j;
303
- result[++k] = i;
304
- continue;
305
- }
306
-
307
- u = 0;
308
- v = k;
309
-
310
- while (u < v) {
311
- c = (u + v) >> 1;
312
- if (arr[result[c]] < arrI) {
313
- u = c + 1;
314
- } else {
315
- v = c;
316
- }
317
- }
318
-
319
- if (arrI < arr[result[u]]) {
320
- if (u > 0) {
321
- p[i] = result[u - 1];
322
- }
323
- result[u] = i;
324
- }
325
- }
326
- }
327
-
328
- u = k + 1;
329
- var seq = new Int32Array(u);
330
- v = result[u - 1];
331
-
332
- while (u-- > 0) {
333
- seq[u] = v;
334
- v = p[v];
335
- result[u] = 0;
336
- }
337
-
338
- return seq;
281
+ let arrI = 0;
282
+ let i = 0;
283
+ let j = 0;
284
+ let k = 0;
285
+ let u = 0;
286
+ let v = 0;
287
+ let c = 0;
288
+ var len = arr.length;
289
+
290
+ if (len > maxLen) {
291
+ maxLen = len;
292
+ result = new Int32Array(len);
293
+ p = new Int32Array(len);
294
+ }
295
+
296
+ for (; i < len; ++i) {
297
+ arrI = arr[i];
298
+
299
+ if (arrI !== 0) {
300
+ j = result[k];
301
+ if (arr[j] < arrI) {
302
+ p[i] = j;
303
+ result[++k] = i;
304
+ continue;
305
+ }
306
+
307
+ u = 0;
308
+ v = k;
309
+
310
+ while (u < v) {
311
+ c = (u + v) >> 1;
312
+ if (arr[result[c]] < arrI) {
313
+ u = c + 1;
314
+ } else {
315
+ v = c;
316
+ }
317
+ }
318
+
319
+ if (arrI < arr[result[u]]) {
320
+ if (u > 0) {
321
+ p[i] = result[u - 1];
322
+ }
323
+ result[u] = i;
324
+ }
325
+ }
326
+ }
327
+
328
+ u = k + 1;
329
+ var seq = new Int32Array(u);
330
+ v = result[u - 1];
331
+
332
+ while (u-- > 0) {
333
+ seq[u] = v;
334
+ v = p[v];
335
+ result[u] = 0;
336
+ }
337
+
338
+ return seq;
339
339
  }
340
340
 
341
341
  export function keyed(collection, key_fn) {
342
- var block = active_block;
343
- if (block === null || (block.f & FOR_BLOCK) === 0) {
344
- throw new Error('keyed() must be used inside a for block');
345
- }
346
- var state = block.s;
347
-
348
- if (state === null) {
349
- return collection;
350
- }
351
-
352
- var a_array = state.array;
353
- var a_keys = a_array.map(key_fn);
354
- var a = new Map();
355
-
356
- for (let i = 0; i < a_keys.length; i++) {
357
- a.set(a_keys[i], i);
358
- }
359
-
360
- if (a.size !== a_keys.length) {
361
- throw new Error('Duplicate keys are not allowed');
362
- }
363
-
364
- var b_array = is_array(collection)
365
- ? collection
366
- : collection == null
367
- ? []
368
- : array_from(collection);
369
- var b_keys = b_array.map(key_fn);
370
-
371
- // We only need to do this in DEV
372
- var b = new Set(b_keys);
373
- if (b.size !== b_keys.length) {
374
- throw new Error('Duplicate keys are not allowed');
375
- }
376
-
377
- for (let i = 0; i < b_keys.length; i++) {
378
- var b_val = b_keys[i];
379
- var index = a.get(b_val);
380
-
381
- if (index !== undefined) {
382
- b_array[i] = a_array[index];
383
- }
384
- }
385
-
386
- return b_array;
342
+ var block = active_block;
343
+ if (block === null || (block.f & FOR_BLOCK) === 0) {
344
+ throw new Error('keyed() must be used inside a for block');
345
+ }
346
+ var state = block.s;
347
+
348
+ if (state === null) {
349
+ return collection;
350
+ }
351
+
352
+ var a_array = state.array;
353
+ var a_keys = a_array.map(key_fn);
354
+ var a = new Map();
355
+
356
+ for (let i = 0; i < a_keys.length; i++) {
357
+ a.set(a_keys[i], i);
358
+ }
359
+
360
+ if (a.size !== a_keys.length) {
361
+ throw new Error('Duplicate keys are not allowed');
362
+ }
363
+
364
+ var b_array = is_array(collection)
365
+ ? collection
366
+ : collection == null
367
+ ? []
368
+ : array_from(collection);
369
+ var b_keys = b_array.map(key_fn);
370
+
371
+ // We only need to do this in DEV
372
+ var b = new Set(b_keys);
373
+ if (b.size !== b_keys.length) {
374
+ throw new Error('Duplicate keys are not allowed');
375
+ }
376
+
377
+ for (let i = 0; i < b_keys.length; i++) {
378
+ var b_val = b_keys[i];
379
+ var index = a.get(b_val);
380
+
381
+ if (index !== undefined) {
382
+ b_array[i] = a_array[index];
383
+ }
384
+ }
385
+
386
+ return b_array;
387
387
  }