@esportsplus/reactivity 0.42.0 → 0.43.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.
@@ -31,43 +31,56 @@ function applyIntents(code: string, sourceFile: ts.SourceFile, intents: Replacem
31
31
  const PRELUDE = "import { reactive, ReactiveArray } from '@esportsplus/reactivity';\n";
32
32
 
33
33
 
34
- function parse(code: string): { bindings: Bindings; code: string; isReactiveCall: (node: ts.Node) => node is ts.CallExpression; sourceFile: ts.SourceFile } {
34
+ async function parse(code: string): Promise<{ bindings: Bindings; code: string; isReactiveCall: (node: ts.Node) => node is ts.CallExpression; sourceFile: ts.SourceFile }> {
35
35
  code = PRELUDE + code;
36
36
 
37
- let { checker, program, sourceFile } = languageService.scratch(process.cwd() + '/test.ts', code),
38
- bindings = scope.create(checker, program, sourceFile);
37
+ let { checker, program, sourceFile } = await languageService.scratch(process.cwd() + '/test.ts', code),
38
+ bindings = await scope.create(checker, program, sourceFile),
39
+ candidates: ts.Node[] = [],
40
+ visit = (node: ts.Node): void => {
41
+ if (ts.isCallExpression(node)) {
42
+ candidates.push(node);
43
+ }
44
+
45
+ node.forEachChild(visit);
46
+ };
47
+
48
+ visit(sourceFile);
49
+
50
+ let reactive = await Promise.all(candidates.map((node) => scope.isReactiveCall(bindings, node))),
51
+ calls = new Set(candidates.filter((_, i) => reactive[i]));
39
52
 
40
- return { bindings, code, isReactiveCall: (node): node is ts.CallExpression => scope.isReactiveCall(bindings, node), sourceFile };
53
+ return { bindings, code, isReactiveCall: (node): node is ts.CallExpression => calls.has(node), sourceFile };
41
54
  }
42
55
 
43
56
  // Full pipeline over a module importing reactive(), composed by the coordinator: what a real consumer compiles
44
- function transformModule(code: string): string {
57
+ function transformModule(code: string): Promise<string> {
45
58
  return transformSource(`import { reactive } from '@esportsplus/reactivity';\n` + code);
46
59
  }
47
60
 
48
- function transformSource(code: string): string {
49
- let { checker, program, sourceFile } = languageService.scratch(process.cwd() + '/module.ts', code);
61
+ async function transformSource(code: string): Promise<string> {
62
+ let { checker, program, sourceFile } = await languageService.scratch(process.cwd() + '/module.ts', code);
50
63
 
51
- return coordinator.transform([pipeline], code, sourceFile, { checker, program }, process.cwd(), new Map()).code;
64
+ return (await coordinator.transform([pipeline], code, sourceFile, { checker, program }, process.cwd(), new Map())).code;
52
65
  }
53
66
 
54
- function transformPrimitives(source: string): { bindings: Bindings; failures: string[]; output: string } {
55
- let { bindings, code, isReactiveCall, sourceFile } = parse(source),
56
- { failures, replacements } = primitives(sourceFile, bindings, isReactiveCall);
67
+ async function transformPrimitives(source: string): Promise<{ bindings: Bindings; failures: string[]; output: string }> {
68
+ let { bindings, code, isReactiveCall, sourceFile } = await parse(source),
69
+ { failures, replacements } = await primitives(sourceFile, bindings, isReactiveCall);
57
70
 
58
71
  return { bindings, failures: failures.map(f => f.message), output: applyIntents(code, sourceFile, replacements) };
59
72
  }
60
73
 
61
- function transformArray(source: string): { bindings: Bindings; output: string } {
62
- let { bindings, code, isReactiveCall, sourceFile } = parse(source),
63
- { replacements } = array(sourceFile, bindings, isReactiveCall);
74
+ async function transformArray(source: string): Promise<{ bindings: Bindings; output: string }> {
75
+ let { bindings, code, isReactiveCall, sourceFile } = await parse(source),
76
+ { replacements } = await array(sourceFile, bindings, isReactiveCall);
64
77
 
65
78
  return { bindings, output: applyIntents(code, sourceFile, replacements) };
66
79
  }
67
80
 
68
- function transformObject(source: string): { bindings: Bindings; output: string; prepend: string[] } {
69
- let { bindings, code, isReactiveCall, sourceFile } = parse(source),
70
- result = object(sourceFile, bindings, isReactiveCall);
81
+ async function transformObject(source: string): Promise<{ bindings: Bindings; output: string; prepend: string[] }> {
82
+ let { bindings, code, isReactiveCall, sourceFile } = await parse(source),
83
+ result = await object(sourceFile, bindings, isReactiveCall);
71
84
 
72
85
  return {
73
86
  bindings,
@@ -77,7 +90,7 @@ function transformObject(source: string): { bindings: Bindings; output: string;
77
90
  }
78
91
 
79
92
  // Kind of the binding the variable `name` declares
80
- function typeOf(bindings: Bindings, name: string): number | null {
93
+ async function typeOf(bindings: Bindings, name: string): Promise<number | null> {
81
94
  let found: ts.Identifier | undefined,
82
95
  visit = (node: ts.Node): void => {
83
96
  if (!found && ts.isVariableDeclaration(node) && ts.isIdentifier(node.name) && node.name.text === name) {
@@ -89,158 +102,158 @@ function typeOf(bindings: Bindings, name: string): number | null {
89
102
 
90
103
  visit(bindings.sourceFile);
91
104
 
92
- return found ? scope.kind(bindings, scope.origin(bindings, found)) : null;
105
+ return found ? scope.kind(bindings, await scope.origin(bindings, found)) : null;
93
106
  }
94
107
 
95
108
 
96
109
  describe('primitives transform', () => {
97
- it('transforms reactive(0) to signal', () => {
98
- let { output } = transformPrimitives('let x = reactive(0);');
110
+ it('transforms reactive(0) to signal', async () => {
111
+ let { output } = await transformPrimitives('let x = reactive(0);');
99
112
 
100
113
  expect(output).toContain(`${NAMESPACE}.signal(0)`);
101
114
  });
102
115
 
103
- it('transforms reactive(() => expr) to computed', () => {
104
- let { output } = transformPrimitives('let x = reactive(0); let d = reactive(() => x * 2);');
116
+ it('transforms reactive(() => expr) to computed', async () => {
117
+ let { output } = await transformPrimitives('let x = reactive(0); let d = reactive(() => x * 2);');
105
118
 
106
119
  expect(output).toContain(`${NAMESPACE}.computed(() =>`);
107
120
  });
108
121
 
109
- it('transforms reads to namespace read', () => {
110
- let { output } = transformPrimitives('let x = reactive(0); console.log(x);');
122
+ it('transforms reads to namespace read', async () => {
123
+ let { output } = await transformPrimitives('let x = reactive(0); console.log(x);');
111
124
 
112
125
  expect(output).toContain(`${NAMESPACE}.read(x)`);
113
126
  });
114
127
 
115
- it('transforms simple assignment to write', () => {
116
- let { output } = transformPrimitives('let x = reactive(0); x = 5;');
128
+ it('transforms simple assignment to write', async () => {
129
+ let { output } = await transformPrimitives('let x = reactive(0); x = 5;');
117
130
 
118
131
  expect(output).toContain(`${NAMESPACE}.write(x, 5)`);
119
132
  });
120
133
 
121
- it('transforms compound assignment += to write', () => {
122
- let { output } = transformPrimitives('let x = reactive(0); x += 5;');
134
+ it('transforms compound assignment += to write', async () => {
135
+ let { output } = await transformPrimitives('let x = reactive(0); x += 5;');
123
136
 
124
137
  expect(output).toContain(`${NAMESPACE}.write(x, x.value + 5)`);
125
138
  });
126
139
 
127
- it('transforms postfix x++ in statement to write', () => {
128
- let { output } = transformPrimitives('let x = reactive(0); x++;');
140
+ it('transforms postfix x++ in statement to write', async () => {
141
+ let { output } = await transformPrimitives('let x = reactive(0); x++;');
129
142
 
130
143
  expect(output).toContain(`${NAMESPACE}.write(x, x.value + 1)`);
131
144
  });
132
145
 
133
- it('transforms prefix ++x in expression', () => {
134
- let { output } = transformPrimitives('let x = reactive(0); let y = ++x;');
146
+ it('transforms prefix ++x in expression', async () => {
147
+ let { output } = await transformPrimitives('let x = reactive(0); let y = ++x;');
135
148
 
136
149
  expect(output).toContain(`(${NAMESPACE}.write(x, x.value + 1), x.value)`);
137
150
  });
138
151
 
139
- it('transforms postfix x++ in expression with temp variable', () => {
140
- let { output } = transformPrimitives('let x = reactive(0); let y = x++;');
152
+ it('transforms postfix x++ in expression with temp variable', async () => {
153
+ let { output } = await transformPrimitives('let x = reactive(0); let y = x++;');
141
154
 
142
155
  expect(output).toContain(`((_t0) => (${NAMESPACE}.write(x, _t0 + 1), _t0))(x.value)`);
143
156
  });
144
157
 
145
- it('transforms reads in nested functions within scope', () => {
146
- let { output } = transformPrimitives('let x = reactive(0); function fn() { return x; }');
158
+ it('transforms reads in nested functions within scope', async () => {
159
+ let { output } = await transformPrimitives('let x = reactive(0); function fn() { return x; }');
147
160
 
148
161
  // The x inside fn IS within the reactive binding scope, so it gets transformed
149
162
  expect(output).toContain(`${NAMESPACE}.read(x)`);
150
163
  });
151
164
 
152
- it('builds a non-literal array or object argument with the runtime reactive()', () => {
153
- let { output } = transformPrimitives('declare function someCall(): number[];\nlet x = reactive(someCall());');
165
+ it('builds a non-literal array or object argument with the runtime reactive()', async () => {
166
+ let { output } = await transformPrimitives('declare function someCall(): number[];\nlet x = reactive(someCall());');
154
167
 
155
168
  expect(output).toContain(`${NAMESPACE}.reactive(someCall())`);
156
169
  });
157
170
 
158
- it('classifies a non-literal primitive argument as a signal by its type', () => {
159
- let { output } = transformPrimitives('declare let n: number;\nlet x = reactive(n);\nlet y = x + 1;');
171
+ it('classifies a non-literal primitive argument as a signal by its type', async () => {
172
+ let { output } = await transformPrimitives('declare let n: number;\nlet x = reactive(n);\nlet y = x + 1;');
160
173
 
161
174
  expect(output).toContain(`let x = ${NAMESPACE}.signal(n)`);
162
175
  expect(output).toContain(`let y = ${NAMESPACE}.read(x) + 1`);
163
176
  });
164
177
 
165
- it('classifies a function-typed argument as a computed', () => {
166
- let { output } = transformPrimitives('declare function total(): number;\nlet x = reactive(total);');
178
+ it('classifies a function-typed argument as a computed', async () => {
179
+ let { output } = await transformPrimitives('declare function total(): number;\nlet x = reactive(total);');
167
180
 
168
181
  expect(output).toContain(`let x = ${NAMESPACE}.computed(total)`);
169
182
  });
170
183
 
171
- it('rejects an argument whose kind is not provable', () => {
172
- let { failures } = transformPrimitives('declare let value: any;\nlet x = reactive(value);');
184
+ it('rejects an argument whose kind is not provable', async () => {
185
+ let { failures } = await transformPrimitives('declare let value: any;\nlet x = reactive(value);');
173
186
 
174
187
  expect(failures).toHaveLength(1);
175
188
  expect(failures[0]).toContain('provable');
176
189
  });
177
190
 
178
- it('rejects a signal that does not initialize a variable', () => {
179
- let { failures } = transformPrimitives('declare function take(value: unknown): void;\ntake(reactive(0));\nlet o = { count: reactive(1) };');
191
+ it('rejects a signal that does not initialize a variable', async () => {
192
+ let { failures } = await transformPrimitives('declare function take(value: unknown): void;\ntake(reactive(0));\nlet o = { count: reactive(1) };');
180
193
 
181
194
  expect(failures).toHaveLength(2);
182
195
  expect(failures[0]).toContain('must initialize a variable');
183
196
  });
184
197
 
185
- it('rejects a write to a computed', () => {
186
- let { failures } = transformPrimitives('let d = reactive(() => 1);\nd = 2;');
198
+ it('rejects a write to a computed', async () => {
199
+ let { failures } = await transformPrimitives('let d = reactive(() => 1);\nd = 2;');
187
200
 
188
201
  expect(failures).toEqual(['a reactive computed is read-only']);
189
202
  });
190
203
 
191
- it('rejects a destructuring assignment into a reactive binding', () => {
192
- let { failures } = transformPrimitives('let x = reactive(0);\n[x] = [1];');
204
+ it('rejects a destructuring assignment into a reactive binding', async () => {
205
+ let { failures } = await transformPrimitives('let x = reactive(0);\n[x] = [1];');
193
206
 
194
207
  expect(failures[0]).toContain('destructuring');
195
208
  });
196
209
 
197
- it('reads a binding assigned with reactive() after its declaration', () => {
198
- let { output } = transformPrimitives('let x: number;\nx = reactive(0);\nlet y = x + 1;');
210
+ it('reads a binding assigned with reactive() after its declaration', async () => {
211
+ let { output } = await transformPrimitives('let x: number;\nx = reactive(0);\nlet y = x + 1;');
199
212
 
200
213
  expect(output).toContain(`let y = ${NAMESPACE}.read(x) + 1`);
201
214
  });
202
215
 
203
- it('reads through typeof', () => {
204
- let { output } = transformPrimitives("let x = reactive(0);\nlet t = typeof x;");
216
+ it('reads through typeof', async () => {
217
+ let { output } = await transformPrimitives("let x = reactive(0);\nlet t = typeof x;");
205
218
 
206
219
  expect(output).toContain(`let t = typeof ${NAMESPACE}.read(x)`);
207
220
  });
208
221
 
209
- it('tracks bindings for signal type', () => {
210
- let { bindings } = transformPrimitives('let x = reactive(0);');
222
+ it('tracks bindings for signal type', async () => {
223
+ let { bindings } = await transformPrimitives('let x = reactive(0);');
211
224
 
212
- expect(typeOf(bindings, 'x')).toBe(TYPES.Signal);
225
+ expect(await typeOf(bindings, 'x')).toBe(TYPES.Signal);
213
226
  });
214
227
 
215
- it('tracks bindings for computed type', () => {
216
- let { bindings } = transformPrimitives('let x = reactive(0); let d = reactive(() => x * 2);');
228
+ it('tracks bindings for computed type', async () => {
229
+ let { bindings } = await transformPrimitives('let x = reactive(0); let d = reactive(() => x * 2);');
217
230
 
218
- expect(typeOf(bindings, 'd')).toBe(TYPES.Computed);
231
+ expect(await typeOf(bindings, 'd')).toBe(TYPES.Computed);
219
232
  });
220
233
 
221
- it('leaves a same-named plain variable in a sibling function untouched', () => {
222
- let { output } = transformPrimitives('function a() { let x = reactive(0); return x; }\nfunction b() { let x = 1; return x; }');
234
+ it('leaves a same-named plain variable in a sibling function untouched', async () => {
235
+ let { output } = await transformPrimitives('function a() { let x = reactive(0); return x; }\nfunction b() { let x = 1; return x; }');
223
236
 
224
237
  expect(output).toContain(`function a() { let x = ${NAMESPACE}.signal(0); return ${NAMESPACE}.read(x); }`);
225
238
  expect(output).toContain('function b() { let x = 1; return x; }');
226
239
  });
227
240
 
228
- it('resolves a shadowing inner binding to the innermost declaration', () => {
229
- let { output } = transformPrimitives('let x = reactive(0); function f() { let x = reactive(() => 1); x = 2; }');
241
+ it('resolves a shadowing inner binding to the innermost declaration', async () => {
242
+ let { output } = await transformPrimitives('let x = reactive(0); function f() { let x = reactive(() => 1); x = 2; }');
230
243
 
231
244
  expect(output).toContain(`let x = ${NAMESPACE}.signal(0)`);
232
245
  expect(output).toContain(`let x = ${NAMESPACE}.computed(() => 1)`);
233
246
  expect(output).not.toContain(`${NAMESPACE}.write(x, 2)`);
234
247
  });
235
248
 
236
- it('transforms prefix --x in statement', () => {
237
- let { output } = transformPrimitives('let x = reactive(0); --x;');
249
+ it('transforms prefix --x in statement', async () => {
250
+ let { output } = await transformPrimitives('let x = reactive(0); --x;');
238
251
 
239
252
  expect(output).toContain(`${NAMESPACE}.write(x, x.value - 1)`);
240
253
  });
241
254
 
242
- it('transforms compound assignment -= to write', () => {
243
- let { output } = transformPrimitives('let x = reactive(0); x -= 3;');
255
+ it('transforms compound assignment -= to write', async () => {
256
+ let { output } = await transformPrimitives('let x = reactive(0); x -= 3;');
244
257
 
245
258
  expect(output).toContain(`${NAMESPACE}.write(x, x.value - 3)`);
246
259
  });
@@ -248,8 +261,8 @@ describe('primitives transform', () => {
248
261
 
249
262
 
250
263
  describe('object transform', () => {
251
- it('transforms reactive object with signal field', () => {
252
- let { output, prepend } = transformObject('let obj = reactive({ count: 0 });');
264
+ it('transforms reactive object with signal field', async () => {
265
+ let { output, prepend } = await transformObject('let obj = reactive({ count: 0 });');
253
266
 
254
267
  expect(prepend.length).toBe(1);
255
268
  expect(prepend[0]).toContain(`extends ${NAMESPACE}.ReactiveObject`);
@@ -259,24 +272,24 @@ describe('object transform', () => {
259
272
  expect(output).not.toContain('reactive(');
260
273
  });
261
274
 
262
- it('transforms reactive object with array field', () => {
263
- let { prepend } = transformObject('let obj = reactive({ items: [1, 2, 3] });');
275
+ it('transforms reactive object with array field', async () => {
276
+ let { prepend } = await transformObject('let obj = reactive({ items: [1, 2, 3] });');
264
277
 
265
278
  expect(prepend.length).toBe(1);
266
279
  expect(prepend[0]).toContain(`${NAMESPACE}.REACTIVE_ARRAY`);
267
280
  expect(prepend[0]).toContain('get items()');
268
281
  });
269
282
 
270
- it('transforms reactive object with computed field', () => {
271
- let { prepend } = transformObject('let obj = reactive({ doubled: () => 2 });');
283
+ it('transforms reactive object with computed field', async () => {
284
+ let { prepend } = await transformObject('let obj = reactive({ doubled: () => 2 });');
272
285
 
273
286
  expect(prepend.length).toBe(1);
274
287
  expect(prepend[0]).toContain(`${NAMESPACE}.COMPUTED`);
275
288
  expect(prepend[0]).toContain(`${NAMESPACE}.read(this.#doubled)`);
276
289
  });
277
290
 
278
- it('transforms reactive object with mixed properties', () => {
279
- let { output, prepend } = transformObject(
291
+ it('transforms reactive object with mixed properties', async () => {
292
+ let { output, prepend } = await transformObject(
280
293
  'let obj = reactive({ count: 0, items: [1], doubled: () => 2 });'
281
294
  );
282
295
 
@@ -287,27 +300,27 @@ describe('object transform', () => {
287
300
  expect(output).toContain('new ');
288
301
  });
289
302
 
290
- it('does not transform object with spread assignment', () => {
291
- let { output, prepend } = transformObject('let obj = reactive({ ...base, count: 0 });');
303
+ it('does not transform object with spread assignment', async () => {
304
+ let { output, prepend } = await transformObject('let obj = reactive({ ...base, count: 0 });');
292
305
 
293
306
  expect(prepend.length).toBe(0);
294
307
  expect(output).toContain('reactive(');
295
308
  });
296
309
 
297
- it('preserves type parameter', () => {
298
- let { output } = transformObject('let obj = reactive<MyType>({ count: 0 });');
310
+ it('preserves type parameter', async () => {
311
+ let { output } = await transformObject('let obj = reactive<MyType>({ count: 0 });');
299
312
 
300
313
  expect(output).toContain('<MyType>');
301
314
  });
302
315
 
303
- it('passes every array property to the constructor, static elements included', () => {
304
- let { output } = transformObject('let obj = reactive({ n: 1, items: [1, 2] });');
316
+ it('passes every array property to the constructor, static elements included', async () => {
317
+ let { output } = await transformObject('let obj = reactive({ n: 1, items: [1, 2] });');
305
318
 
306
319
  expect(output).toMatch(/new ReactiveObject_\w+\(\[1, 2\]\)/);
307
320
  });
308
321
 
309
- it('makes a non-literal array property a reactive array', () => {
310
- let { prepend } = transformObject('declare let list: number[];\nlet obj = reactive({ items: list });');
322
+ it('makes a non-literal array property a reactive array', async () => {
323
+ let { prepend } = await transformObject('declare let list: number[];\nlet obj = reactive({ items: list });');
311
324
 
312
325
  expect(prepend[0]).toContain(`${NAMESPACE}.REACTIVE_ARRAY`);
313
326
  });
@@ -315,106 +328,106 @@ describe('object transform', () => {
315
328
 
316
329
 
317
330
  describe('array transform', () => {
318
- it('transforms reactive([1,2,3]) to ReactiveArray', () => {
319
- let { output } = transformArray('let arr = reactive([1, 2, 3]);');
331
+ it('transforms reactive([1,2,3]) to ReactiveArray', async () => {
332
+ let { output } = await transformArray('let arr = reactive([1, 2, 3]);');
320
333
 
321
334
  expect(output).toContain(`new ${NAMESPACE}.ReactiveArray`);
322
335
  expect(output).toContain('([1, 2, 3])');
323
336
  });
324
337
 
325
- it('transforms reactive([] as Type[]) to typed ReactiveArray', () => {
326
- let { output } = transformArray('let arr = reactive([] as number[]);');
338
+ it('transforms reactive([] as Type[]) to typed ReactiveArray', async () => {
339
+ let { output } = await transformArray('let arr = reactive([] as number[]);');
327
340
 
328
341
  expect(output).toContain(`new ${NAMESPACE}.ReactiveArray<number>()`);
329
342
  });
330
343
 
331
- it('transforms arr.length read to arr.$length', () => {
332
- let { output } = transformArray('let arr = reactive([1]); let x = arr.length;');
344
+ it('transforms arr.length read to arr.$length', async () => {
345
+ let { output } = await transformArray('let arr = reactive([1]); let x = arr.length;');
333
346
 
334
347
  expect(output).toContain('arr.$length');
335
348
  });
336
349
 
337
- it('transforms arr.length = n to arr.$length = n', () => {
338
- let { output } = transformArray('let arr = reactive([1]); arr.length = 5;');
350
+ it('transforms arr.length = n to arr.$length = n', async () => {
351
+ let { output } = await transformArray('let arr = reactive([1]); arr.length = 5;');
339
352
 
340
353
  expect(output).toContain('arr.$length = 5');
341
354
  });
342
355
 
343
- it('transforms arr.length += n to arr.$length = arr.length + n', () => {
344
- let { output } = transformArray('let arr = reactive([1]); arr.length += 3;');
356
+ it('transforms arr.length += n to arr.$length = arr.length + n', async () => {
357
+ let { output } = await transformArray('let arr = reactive([1]); arr.length += 3;');
345
358
 
346
359
  expect(output).toContain('arr.$length = arr.length + 3');
347
360
  });
348
361
 
349
- it('transforms arr.length++ and --arr.length to $length writes', () => {
350
- expect(transformArray('let arr = reactive([1]); arr.length++;').output).toContain('arr.$length = arr.length + 1');
351
- expect(transformArray('let arr = reactive([1]); --arr.length;').output).toContain('arr.$length = arr.length - 1');
362
+ it('transforms arr.length++ and --arr.length to $length writes', async () => {
363
+ expect((await transformArray('let arr = reactive([1]); arr.length++;')).output).toContain('arr.$length = arr.length + 1');
364
+ expect((await transformArray('let arr = reactive([1]); --arr.length;')).output).toContain('arr.$length = arr.length - 1');
352
365
  });
353
366
 
354
- it('transforms non-mutating unary operators on arr.length as reads', () => {
367
+ it('transforms non-mutating unary operators on arr.length as reads', async () => {
355
368
  for (let op of ['!', '-', '+', '~']) {
356
- let { output } = transformArray(`let arr = reactive([1]); let x = ${op}arr.length;`);
369
+ let { output } = await transformArray(`let arr = reactive([1]); let x = ${op}arr.length;`);
357
370
 
358
371
  expect(output).toContain(`${op}arr.$length`);
359
372
  expect(output).not.toContain('arr.$length =');
360
373
  }
361
374
  });
362
375
 
363
- it('transforms every compound operator on arr.length with its own token', () => {
376
+ it('transforms every compound operator on arr.length with its own token', async () => {
364
377
  for (let op of ['<<', '>>', '>>>', '??', '||', '&&', '-', '*', '/', '%', '**', '&', '|', '^']) {
365
- let { output } = transformArray(`let arr = reactive([1]); arr.length ${op}= 2;`);
378
+ let { output } = await transformArray(`let arr = reactive([1]); arr.length ${op}= 2;`);
366
379
 
367
380
  expect(output).toContain(`arr.$length = arr.length ${op} 2`);
368
381
  }
369
382
  });
370
383
 
371
- it('transforms arr[i] = value to arr.$set(i, value)', () => {
372
- let { output } = transformArray('let arr = reactive([1]); arr[0] = 42;');
384
+ it('transforms arr[i] = value to arr.$set(i, value)', async () => {
385
+ let { output } = await transformArray('let arr = reactive([1]); arr[0] = 42;');
373
386
 
374
387
  expect(output).toContain('arr.$set(');
375
388
  expect(output).toContain('42');
376
389
  });
377
390
 
378
- it('tracks reactive array binding from reactive call', () => {
379
- let { bindings } = transformArray('let arr = reactive([1, 2, 3]);');
391
+ it('tracks reactive array binding from reactive call', async () => {
392
+ let { bindings } = await transformArray('let arr = reactive([1, 2, 3]);');
380
393
 
381
- expect(typeOf(bindings, 'arr')).toBe(TYPES.Array);
394
+ expect(await typeOf(bindings, 'arr')).toBe(TYPES.Array);
382
395
  });
383
396
 
384
- it('compiles an alias of a reactive array by its type', () => {
385
- let { output } = transformArray('let a = reactive([1]); let b = a; let n = b.length;');
397
+ it('compiles an alias of a reactive array by its type', async () => {
398
+ let { output } = await transformArray('let a = reactive([1]); let b = a; let n = b.length;');
386
399
 
387
400
  expect(output).toContain('let n = b.$length');
388
401
  });
389
402
 
390
- it('compiles a ReactiveArray-typed parameter by its type', () => {
391
- let { output } = transformArray('function fn(arr: ReactiveArray<number>) { arr[0] = 1; return arr.length; }');
403
+ it('compiles a ReactiveArray-typed parameter by its type', async () => {
404
+ let { output } = await transformArray('function fn(arr: ReactiveArray<number>) { arr[0] = 1; return arr.length; }');
392
405
 
393
406
  expect(output).toContain('arr.$set(0, 1)');
394
407
  expect(output).toContain('return arr.$length');
395
408
  });
396
409
 
397
- it('compiles an array property of a reactive object by its type', () => {
398
- let { output } = transformArray('let obj = reactive({ items: [1] }); let n = obj.items.length;');
410
+ it('compiles an array property of a reactive object by its type', async () => {
411
+ let { output } = await transformArray('let obj = reactive({ items: [1] }); let n = obj.items.length;');
399
412
 
400
413
  expect(output).toContain('let n = obj.items.$length');
401
414
  });
402
415
 
403
- it('routes compound and increment element writes through $set', () => {
404
- let { output } = transformArray('let arr = reactive([1]); arr[0] += 2; arr[0]++;');
416
+ it('routes compound and increment element writes through $set', async () => {
417
+ let { output } = await transformArray('let arr = reactive([1]); arr[0] += 2; arr[0]++;');
405
418
 
406
419
  expect(output).toContain('((_a, _k) => _a.$set(_k, _a[_k] + (2)))(arr, 0)');
407
420
  expect(output).toContain('((_a, _k, _v) => (_a.$set(_k, _v + 1), _v))(arr, 0, arr[0])');
408
421
  });
409
422
 
410
- it('leaves a plain array untouched', () => {
411
- let { output } = transformArray('let arr = [1]; arr[0] = 2; let n = arr.length;');
423
+ it('leaves a plain array untouched', async () => {
424
+ let { output } = await transformArray('let arr = [1]; arr[0] = 2; let n = arr.length;');
412
425
 
413
426
  expect(output).toContain('arr[0] = 2; let n = arr.length;');
414
427
  });
415
428
 
416
- it('transforms empty array', () => {
417
- let { output } = transformArray('let arr = reactive([] as string[]);');
429
+ it('transforms empty array', async () => {
430
+ let { output } = await transformArray('let arr = reactive([] as string[]);');
418
431
 
419
432
  expect(output).toContain(`new ${NAMESPACE}.ReactiveArray<string>()`);
420
433
  });
@@ -422,120 +435,120 @@ describe('array transform', () => {
422
435
 
423
436
 
424
437
  describe('binding resolution (full pipeline)', () => {
425
- it('leaves a local array that shadows a reactive array untouched', () => {
426
- let output = transformModule('let rows = reactive([] as number[]);\nfunction build(n: number) { let rows: number[] = []; rows[0] = n; return rows.length; }');
438
+ it('leaves a local array that shadows a reactive array untouched', async () => {
439
+ let output = await transformModule('let rows = reactive([] as number[]);\nfunction build(n: number) { let rows: number[] = []; rows[0] = n; return rows.length; }');
427
440
 
428
441
  expect(output).toContain('let rows: number[] = []; rows[0] = n; return rows.length;');
429
442
  });
430
443
 
431
- it('leaves a parameter that shadows a reactive array untouched', () => {
432
- let output = transformModule('let rows = reactive([1]);\nconst f = (rows: number[]) => { rows[0] = 2; };');
444
+ it('leaves a parameter that shadows a reactive array untouched', async () => {
445
+ let output = await transformModule('let rows = reactive([1]);\nconst f = (rows: number[]) => { rows[0] = 2; };');
433
446
 
434
447
  expect(output).toContain('const f = (rows: number[]) => { rows[0] = 2; };');
435
448
  });
436
449
 
437
- it('leaves a local that shadows a signal untouched', () => {
438
- let output = transformModule('let count = reactive(0);\nfunction f() { let count = 5; count++; return count; }');
450
+ it('leaves a local that shadows a signal untouched', async () => {
451
+ let output = await transformModule('let count = reactive(0);\nfunction f() { let count = 5; count++; return count; }');
439
452
 
440
453
  expect(output).toContain('function f() { let count = 5; count++; return count; }');
441
454
  });
442
455
 
443
- it('leaves a parameter that shadows a signal untouched', () => {
444
- let output = transformModule('let count = reactive(0);\nconst f = (count: number) => count + 1;');
456
+ it('leaves a parameter that shadows a signal untouched', async () => {
457
+ let output = await transformModule('let count = reactive(0);\nconst f = (count: number) => count + 1;');
445
458
 
446
459
  expect(output).toContain('const f = (count: number) => count + 1;');
447
460
  });
448
461
 
449
- it('never rewrites property names, type members or destructured parameters', () => {
450
- let output = transformModule('let count = reactive(0);\nlet o = { count: 1 };\nlet p = o.count;\nfunction f({ count }: { count: number }) { return count; }');
462
+ it('never rewrites property names, type members or destructured parameters', async () => {
463
+ let output = await transformModule('let count = reactive(0);\nlet o = { count: 1 };\nlet p = o.count;\nfunction f({ count }: { count: number }) { return count; }');
451
464
 
452
465
  expect(output).toContain('let o = { count: 1 };');
453
466
  expect(output).toContain('let p = o.count;');
454
467
  expect(output).toContain('function f({ count }: { count: number }) { return count; }');
455
468
  });
456
469
 
457
- it('expands a shorthand property into a read', () => {
458
- let output = transformModule('let count = reactive(0);\nlet o = { count };');
470
+ it('expands a shorthand property into a read', async () => {
471
+ let output = await transformModule('let count = reactive(0);\nlet o = { count };');
459
472
 
460
473
  expect(output).toContain(`let o = { count: ${NAMESPACE}.read(count) };`);
461
474
  });
462
475
 
463
- it('rewrites a use that precedes the declaration in source order', () => {
464
- let output = transformModule('function inc() { count++; }\nlet count = reactive(0);');
476
+ it('rewrites a use that precedes the declaration in source order', async () => {
477
+ let output = await transformModule('function inc() { count++; }\nlet count = reactive(0);');
465
478
 
466
479
  expect(output).toContain(`function inc() { ${NAMESPACE}.write(count, count.value + 1); }`);
467
480
  });
468
481
 
469
- it('exports the signal itself rather than splicing a read into the export list', () => {
470
- let output = transformModule('let count = reactive(0);\nexport { count };');
482
+ it('exports the signal itself rather than splicing a read into the export list', async () => {
483
+ let output = await transformModule('let count = reactive(0);\nexport { count };');
471
484
 
472
485
  expect(output).toContain('export { count };');
473
486
  });
474
487
 
475
- it('transforms a namespace-qualified reactive() call', () => {
476
- let output = transformModule("import * as r from '@esportsplus/reactivity';\nlet count = r.reactive(0);\nlet next = count + 1;");
488
+ it('transforms a namespace-qualified reactive() call', async () => {
489
+ let output = await transformModule("import * as r from '@esportsplus/reactivity';\nlet count = r.reactive(0);\nlet next = count + 1;");
477
490
 
478
491
  expect(output).toContain(`let count = ${NAMESPACE}.signal(0);`);
479
492
  expect(output).toContain(`let next = ${NAMESPACE}.read(count) + 1;`);
480
493
  });
481
494
 
482
- it('transforms calls through an aliased import', () => {
483
- let output = transformSource("import { reactive as r } from '@esportsplus/reactivity';\nlet count = r(0);\nlet next = count + 1;");
495
+ it('transforms calls through an aliased import', async () => {
496
+ let output = await transformSource("import { reactive as r } from '@esportsplus/reactivity';\nlet count = r(0);\nlet next = count + 1;");
484
497
 
485
498
  expect(output).toContain(`let count = ${NAMESPACE}.signal(0);`);
486
499
  expect(output).toContain(`let next = ${NAMESPACE}.read(count) + 1;`);
487
500
  });
488
501
 
489
- it('transforms a reactive array created through an aliased import', () => {
490
- let output = transformSource("import { reactive as r } from '@esportsplus/reactivity';\nlet list = r([1, 2]);\nlet size = list.length;");
502
+ it('transforms a reactive array created through an aliased import', async () => {
503
+ let output = await transformSource("import { reactive as r } from '@esportsplus/reactivity';\nlet list = r([1, 2]);\nlet size = list.length;");
491
504
 
492
505
  expect(output).not.toContain('r([1, 2])');
493
506
  expect(output).toContain('let size = list.$length');
494
507
  });
495
508
 
496
- it('leaves a parameter that shadows an alias untouched', () => {
509
+ it('leaves a parameter that shadows an alias untouched', async () => {
497
510
  let code = "import { reactive as r } from '@esportsplus/reactivity';\nfunction f(r: (n: number) => number) { return r(1); }";
498
511
 
499
- expect(transformSource(code)).toBe(code);
512
+ expect(await transformSource(code)).toBe(code);
500
513
  });
501
514
 
502
- it('leaves another export imported under the name reactive untouched', () => {
515
+ it('leaves another export imported under the name reactive untouched', async () => {
503
516
  let code = "import { root as reactive } from '@esportsplus/reactivity';\nlet value = reactive(() => 1);";
504
517
 
505
- expect(transformSource(code)).toBe(code);
518
+ expect(await transformSource(code)).toBe(code);
506
519
  });
507
520
  });
508
521
 
509
522
 
510
523
  describe('index transform', () => {
511
524
  // A module can reach reactive() or read a binding under any name, so no name prefilter may skip it
512
- it('has no text patterns', () => {
525
+ it('has no text patterns', async () => {
513
526
  expect('patterns' in pipeline).toBe(false);
514
527
  });
515
528
 
516
- it('names itself in coordinator errors', () => {
529
+ it('names itself in coordinator errors', async () => {
517
530
  expect(pipeline.name).toBe(PACKAGE_NAME);
518
531
  });
519
532
 
520
- it('has transform function', () => {
533
+ it('has transform function', async () => {
521
534
  expect(typeof pipeline.transform).toBe('function');
522
535
  });
523
536
 
524
- it('skips a module without an import before resolving anything', () => {
537
+ it('skips a module without an import before resolving anything', async () => {
525
538
  let code = 'let reactive = (n: number) => n;\nexport let x = reactive(1);',
526
- { program, sourceFile } = languageService.scratch(process.cwd() + '/plain.ts', code),
539
+ { program, sourceFile } = await languageService.scratch(process.cwd() + '/plain.ts', code),
527
540
  checker = new Proxy({}, {
528
541
  get: () => {
529
542
  throw new Error('checker queried');
530
543
  }
531
544
  }) as ts.Checker;
532
545
 
533
- expect(pipeline.transform({ checker, code, program, shared: new Map(), sourceFile })).toEqual({});
546
+ expect(await pipeline.transform({ checker, code, program, shared: new Map(), sourceFile })).toEqual({});
534
547
  });
535
548
 
536
- it('classifies every reactive() argument from one batched type request', () => {
549
+ it('classifies every reactive() argument from one batched type request', async () => {
537
550
  let code = "import { reactive } from '@esportsplus/reactivity';\ndeclare let n: number;\ndeclare function total(): number;\nlet a = reactive(n);\nlet b = reactive(total);\nlet c = a + b + a + b;",
538
- { checker, program, sourceFile } = languageService.scratch(process.cwd() + '/batched.ts', code),
551
+ { checker, program, sourceFile } = await languageService.scratch(process.cwd() + '/batched.ts', code),
539
552
  single = 0,
540
553
  counted = new Proxy(checker, {
541
554
  get(target, key) {
@@ -554,7 +567,7 @@ describe('index transform', () => {
554
567
  };
555
568
  }
556
569
  }),
557
- output = coordinator.transform([pipeline], code, sourceFile, { checker: counted, program }, process.cwd(), new Map()).code;
570
+ output = (await coordinator.transform([pipeline], code, sourceFile, { checker: counted, program }, process.cwd(), new Map())).code;
558
571
 
559
572
  expect(output).toContain(`let a = ${NAMESPACE}.signal(n);`);
560
573
  expect(output).toContain(`let b = ${NAMESPACE}.computed(total);`);
@@ -563,11 +576,11 @@ describe('index transform', () => {
563
576
 
564
577
 
565
578
  describe('plugins', () => {
566
- it('tsc plugin is defined', () => {
579
+ it('tsc plugin is defined', async () => {
567
580
  expect(tscPlugin).toBeDefined();
568
581
  });
569
582
 
570
- it('vite plugin is defined', () => {
583
+ it('vite plugin is defined', async () => {
571
584
  expect(vitePlugin).toBeDefined();
572
585
  });
573
586
  });