@esportsplus/reactivity 0.40.0 → 0.42.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.
Files changed (61) hide show
  1. package/README.md +16 -10
  2. package/bench/compiler.bench.ts +122 -0
  3. package/bench/kairo.bench.ts +1 -1
  4. package/bench/lib/reactive-adapter.ts +1 -1
  5. package/bench/ownership.bench.ts +9 -5
  6. package/bench/reactive/array.bench.ts +7 -2
  7. package/bench/reactive/reactive-object.bench.ts +3 -2
  8. package/bench/system.bench.ts +9 -5
  9. package/build/compiler/array.js +28 -25
  10. package/build/compiler/bindings.d.ts +3 -1
  11. package/build/compiler/bindings.js +36 -18
  12. package/build/compiler/index.d.ts +3 -2
  13. package/build/compiler/index.js +21 -13
  14. package/build/compiler/object.js +30 -21
  15. package/build/compiler/plugins/vite.d.ts +29 -6
  16. package/build/compiler/primitives.js +9 -9
  17. package/build/constants.d.ts +3 -1
  18. package/build/constants.js +3 -1
  19. package/build/reactive/array.js +89 -17
  20. package/build/reactive/index.d.ts +1 -1
  21. package/build/reactive/object.d.ts +3 -3
  22. package/build/reactive/object.js +1 -1
  23. package/build/system.d.ts +4 -3
  24. package/build/system.js +48 -16
  25. package/oxlintrc.json +3 -0
  26. package/package.json +15 -7
  27. package/pnpm-workspace.yaml +4 -7
  28. package/src/compiler/array.ts +30 -27
  29. package/src/compiler/bindings.ts +52 -23
  30. package/src/compiler/index.ts +37 -20
  31. package/src/compiler/object.ts +43 -30
  32. package/src/compiler/primitives.ts +13 -10
  33. package/src/constants.ts +6 -1
  34. package/src/reactive/array.ts +133 -24
  35. package/src/reactive/index.ts +3 -3
  36. package/src/reactive/object.ts +4 -4
  37. package/src/system.ts +68 -23
  38. package/test/async-hardening.test.ts +2 -2
  39. package/test/async-iterable.test.ts +3 -3
  40. package/test/compiler/__snapshots__/output.test.ts.snap +157 -0
  41. package/test/compiler/compiler.test.ts +51 -9
  42. package/test/compiler/composition.test.ts +110 -0
  43. package/test/compiler/coverage.test.ts +22 -22
  44. package/test/compiler/origins.test.ts +156 -0
  45. package/test/compiler/output.test.ts +116 -0
  46. package/test/deep-graphs.test.ts +6 -6
  47. package/test/effects.test.ts +5 -5
  48. package/test/errors.test.ts +4 -4
  49. package/test/invalidate.test.ts +2 -2
  50. package/test/ownership.test.ts +30 -30
  51. package/test/pause.test.ts +267 -0
  52. package/test/reactive/array.test.ts +13 -12
  53. package/test/reactive/reactive.test.ts +6 -6
  54. package/test/signal-selector.test.ts +4 -4
  55. package/test/system.test.ts +13 -13
  56. package/test/untrack.test.ts +10 -10
  57. package/tsconfig.build.json +3 -0
  58. package/tsconfig.json +2 -2
  59. package/vitest.config.ts +2 -2
  60. package/bench/tsconfig.json +0 -16
  61. package/test/tsconfig.json +0 -16
@@ -1,8 +1,8 @@
1
1
  import { describe, expect, it } from 'vitest';
2
2
  import { ts } from '@esportsplus/typescript';
3
- import { languageService } from '@esportsplus/typescript/compiler';
3
+ import { coordinator, languageService } from '@esportsplus/typescript/compiler';
4
4
  import type { ReplacementIntent } from '@esportsplus/typescript/compiler';
5
- import { NAMESPACE, TYPES } from '~/compiler/constants';
5
+ import { NAMESPACE, PACKAGE_NAME, TYPES } from '~/compiler/constants';
6
6
  import type { Bindings } from '~/compiler/types';
7
7
  import array from '~/compiler/array';
8
8
  import scope from '~/compiler/bindings';
@@ -21,7 +21,7 @@ function applyIntents(code: string, sourceFile: ts.SourceFile, intents: Replacem
21
21
  end = intent.node.getEnd(),
22
22
  start = intent.node.getStart(sourceFile);
23
23
 
24
- code = code.slice(0, start) + intent.generate(sourceFile) + code.slice(end);
24
+ code = code.slice(0, start) + intent.generate(sourceFile, node => node.getText(sourceFile)) + code.slice(end);
25
25
  }
26
26
 
27
27
  return code;
@@ -40,16 +40,15 @@ function parse(code: string): { bindings: Bindings; code: string; isReactiveCall
40
40
  return { bindings, code, isReactiveCall: (node): node is ts.CallExpression => scope.isReactiveCall(bindings, node), sourceFile };
41
41
  }
42
42
 
43
- // Full pipeline over a module importing reactive(): what a real consumer compiles
43
+ // Full pipeline over a module importing reactive(), composed by the coordinator: what a real consumer compiles
44
44
  function transformModule(code: string): string {
45
45
  return transformSource(`import { reactive } from '@esportsplus/reactivity';\n` + code);
46
46
  }
47
47
 
48
48
  function transformSource(code: string): string {
49
- let { checker, program, sourceFile } = languageService.scratch(process.cwd() + '/module.ts', code),
50
- result = pipeline.transform({ checker, code, program, sourceFile } as never);
49
+ let { checker, program, sourceFile } = languageService.scratch(process.cwd() + '/module.ts', code);
51
50
 
52
- return applyIntents(code, sourceFile, result.replacements ?? []);
51
+ return coordinator.transform([pipeline], code, sourceFile, { checker, program }, process.cwd(), new Map()).code;
53
52
  }
54
53
 
55
54
  function transformPrimitives(source: string): { bindings: Bindings; failures: string[]; output: string } {
@@ -509,15 +508,58 @@ describe('binding resolution (full pipeline)', () => {
509
508
 
510
509
 
511
510
  describe('index transform', () => {
512
- // A module can reach reactive() or read a binding under any name, so no text prefilter may skip it
511
+ // A module can reach reactive() or read a binding under any name, so no name prefilter may skip it
513
512
  it('has no text patterns', () => {
514
513
  expect('patterns' in pipeline).toBe(false);
515
514
  });
516
515
 
516
+ it('names itself in coordinator errors', () => {
517
+ expect(pipeline.name).toBe(PACKAGE_NAME);
518
+ });
519
+
517
520
  it('has transform function', () => {
518
521
  expect(typeof pipeline.transform).toBe('function');
519
522
  });
520
- });
523
+
524
+ it('skips a module without an import before resolving anything', () => {
525
+ let code = 'let reactive = (n: number) => n;\nexport let x = reactive(1);',
526
+ { program, sourceFile } = languageService.scratch(process.cwd() + '/plain.ts', code),
527
+ checker = new Proxy({}, {
528
+ get: () => {
529
+ throw new Error('checker queried');
530
+ }
531
+ }) as ts.Checker;
532
+
533
+ expect(pipeline.transform({ checker, code, program, shared: new Map(), sourceFile })).toEqual({});
534
+ });
535
+
536
+ it('classifies every reactive() argument from one batched type request', () => {
537
+ 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),
539
+ single = 0,
540
+ counted = new Proxy(checker, {
541
+ get(target, key) {
542
+ let value = Reflect.get(target, key);
543
+
544
+ if (key !== 'getTypeAtLocation' || typeof value !== 'function') {
545
+ return value;
546
+ }
547
+
548
+ return (node: unknown) => {
549
+ if (!Array.isArray(node)) {
550
+ single++;
551
+ }
552
+
553
+ return value.call(target, node);
554
+ };
555
+ }
556
+ }),
557
+ output = coordinator.transform([pipeline], code, sourceFile, { checker: counted, program }, process.cwd(), new Map()).code;
558
+
559
+ expect(output).toContain(`let a = ${NAMESPACE}.signal(n);`);
560
+ expect(output).toContain(`let b = ${NAMESPACE}.computed(total);`);
561
+ expect(single).toBe(0);
562
+ });});
521
563
 
522
564
 
523
565
  describe('plugins', () => {
@@ -0,0 +1,110 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { ts } from '@esportsplus/typescript';
3
+ import { coordinator, languageService } from '@esportsplus/typescript/compiler';
4
+ import type { Plugin, ReplacementIntent } from '@esportsplus/typescript/compiler';
5
+ import { NAMESPACE } from '~/compiler/constants';
6
+
7
+ import pipeline from '~/compiler/index';
8
+
9
+
10
+ // Stands in for template: rewrites every `tag` tagged template, which reactivity's own rewrites may contain
11
+ const FOREIGN: Plugin = {
12
+ name: 'foreign',
13
+ transform: ({ sourceFile }) => {
14
+ let replacements: ReplacementIntent[] = [],
15
+ visit = (node: ts.Node): void => {
16
+ if (ts.isTaggedTemplateExpression(node) && ts.isIdentifier(node.tag) && node.tag.text === 'tag') {
17
+ replacements.push({ generate: () => 'TAGGED', node });
18
+ }
19
+
20
+ node.forEachChild(visit);
21
+ };
22
+
23
+ visit(sourceFile);
24
+
25
+ return { replacements };
26
+ }
27
+ };
28
+
29
+ const PRELUDE = "import { reactive } from '@esportsplus/reactivity';\n";
30
+
31
+
32
+ function compile(code: string, plugins: Plugin[] = [pipeline]): string {
33
+ code = PRELUDE + code;
34
+
35
+ let { checker, program, sourceFile } = languageService.scratch(process.cwd() + '/composed.ts', code);
36
+
37
+ return coordinator.transform(plugins, code, sourceFile, { checker, program }, process.cwd(), new Map()).code;
38
+ }
39
+
40
+
41
+ describe('nested rewrites', () => {
42
+ let output = compile([
43
+ 'let x = reactive(0);',
44
+ 'let y = reactive(1);',
45
+ 'x = y;',
46
+ 'x += y;',
47
+ 'let arr = reactive([y]);',
48
+ 'arr[0] = y;',
49
+ 'arr[1] += y;',
50
+ 'arr[y] = 2;',
51
+ 'let at = arr[y];',
52
+ 'arr.length = y;',
53
+ 'arr = [y];',
54
+ 'let o = reactive({ a: y, items: [y] });',
55
+ 'let s = reactive({ ...{ b: 1 }, c: y });',
56
+ 'let e = { y: { y } };'
57
+ ].join('\n'));
58
+
59
+ it('reads a signal on the right of a write to another', () => {
60
+ expect(output).toContain(`${NAMESPACE}.write(x, ${NAMESPACE}.read(y));`);
61
+ expect(output).toContain(`${NAMESPACE}.write(x, x.value + ${NAMESPACE}.read(y));`);
62
+ });
63
+
64
+ it('reads a signal inside array lowerings', () => {
65
+ expect(output).toContain(`let arr = new ${NAMESPACE}.ReactiveArray([${NAMESPACE}.read(y)]);`);
66
+ expect(output).toContain(`arr.$set(0, ${NAMESPACE}.read(y));`);
67
+ expect(output).toContain(`((_a, _k) => _a.$set(_k, _a[_k] + (${NAMESPACE}.read(y))))(arr, 1);`);
68
+ expect(output).toContain(`arr.$set(${NAMESPACE}.read(y), 2);`);
69
+ expect(output).toContain(`let at = arr[${NAMESPACE}.read(y)];`);
70
+ expect(output).toContain(`arr.$length = ${NAMESPACE}.read(y);`);
71
+ expect(output).toContain(`arr.splice(0, arr.length, ...[${NAMESPACE}.read(y)]);`);
72
+ });
73
+
74
+ it('reads a signal inside object lowerings', () => {
75
+ expect(output).toMatch(new RegExp(`let o = +new ReactiveObject_\\w+\\(${NAMESPACE}\\.read\\(y\\), \\[${NAMESPACE}\\.read\\(y\\)\\]\\);`));
76
+ expect(output).toContain(`let s = ${NAMESPACE}.reactive({ ...{ b: 1 }, c: ${NAMESPACE}.read(y) });`);
77
+ expect(output).toContain(`let e = { y: { y: ${NAMESPACE}.read(y) } };`);
78
+ });
79
+ });
80
+
81
+
82
+ describe('composition with another plugin', () => {
83
+ let code = [
84
+ 'declare function tag(strings: TemplateStringsArray): number;',
85
+ 'let count = reactive(tag`a`);',
86
+ 'count = tag`b`;',
87
+ 'count += tag`c`;',
88
+ 'let o = reactive({ n: tag`d` });',
89
+ 'let s = reactive({ ...{}, n: tag`e` });',
90
+ 'let arr = reactive([tag`f`]);',
91
+ 'arr[0] = tag`g`;',
92
+ 'arr = [tag`h`];'
93
+ ].join('\n');
94
+
95
+ for (let [order, plugins] of [['before', [FOREIGN, pipeline]], ['after', [pipeline, FOREIGN]]] as const) {
96
+ it(`keeps the other plugin's replacements inside every lowering when it runs ${order}`, () => {
97
+ let output = compile(code, [...plugins]);
98
+
99
+ expect(output).not.toContain('tag`');
100
+ expect(output).toContain(`let count = ${NAMESPACE}.signal(TAGGED);`);
101
+ expect(output).toContain(`${NAMESPACE}.write(count, TAGGED);`);
102
+ expect(output).toContain(`${NAMESPACE}.write(count, count.value + TAGGED);`);
103
+ expect(output).toMatch(/let o = +new ReactiveObject_\w+\(TAGGED\);/);
104
+ expect(output).toContain(`let s = ${NAMESPACE}.reactive({ ...{}, n: TAGGED });`);
105
+ expect(output).toContain(`let arr = new ${NAMESPACE}.ReactiveArray([TAGGED]);`);
106
+ expect(output).toContain('arr.$set(0, TAGGED);');
107
+ expect(output).toContain('arr.splice(0, arr.length, ...[TAGGED]);');
108
+ });
109
+ }
110
+ });
@@ -7,18 +7,24 @@ import vite from '~/compiler/plugins/vite';
7
7
 
8
8
  // Every route by which a module reaches `reactive`, or a binding it created, must compile
9
9
  const FILES: Record<string, string> = {
10
- 'state.ts': [
11
- "import { reactive } from '@esportsplus/reactivity';",
12
- 'export let count = reactive(0);',
13
- 'export let list = reactive([1, 2]);',
14
- 'export let store = reactive({ n: 1, items: [1] });',
15
- 'export default reactive(0);'
16
- ].join('\n'),
17
10
  'barrel.ts': [
18
11
  "export { reactive as make } from '@esportsplus/reactivity';",
19
12
  "export * as R from '@esportsplus/reactivity';",
20
13
  "export * from './state';"
21
14
  ].join('\n'),
15
+ 'entries.ts': [
16
+ "import { make, R } from './barrel';",
17
+ "import * as B from './barrel';",
18
+ 'export let a = make(0);',
19
+ 'export let b = R.reactive(0);',
20
+ 'export let c = B.R.reactive(0);',
21
+ "export let d = R['reactive'](0);",
22
+ 'const alias = make;',
23
+ 'export let e = alias(0);',
24
+ 'const { reactive: destructured } = R;',
25
+ 'export let f = destructured(0);',
26
+ 'export let g = () => a + b + c + d + e + f;'
27
+ ].join('\n'),
22
28
  'reads.ts': [
23
29
  "import { count, list, store } from './state';",
24
30
  "import * as S from './state';",
@@ -33,18 +39,12 @@ const FILES: Record<string, string> = {
33
39
  'export let g = () => store.items.length;',
34
40
  "export let h = () => S['count'] + 1;"
35
41
  ].join('\n'),
36
- 'entries.ts': [
37
- "import { make, R } from './barrel';",
38
- "import * as B from './barrel';",
39
- 'export let a = make(0);',
40
- 'export let b = R.reactive(0);',
41
- 'export let c = B.R.reactive(0);',
42
- "export let d = R['reactive'](0);",
43
- 'const alias = make;',
44
- 'export let e = alias(0);',
45
- 'const { reactive: destructured } = R;',
46
- 'export let f = destructured(0);',
47
- 'export let g = () => a + b + c + d + e + f;'
42
+ 'state.ts': [
43
+ "import { reactive } from '@esportsplus/reactivity';",
44
+ 'export let count = reactive(0);',
45
+ 'export let list = reactive([1, 2]);',
46
+ 'export let store = reactive({ n: 1, items: [1] });',
47
+ 'export default reactive(0);'
48
48
  ].join('\n')
49
49
  };
50
50
 
@@ -73,7 +73,7 @@ function project(files: Record<string, string>) {
73
73
  return {
74
74
  directory,
75
75
  plugin,
76
- transform: (name: string) => plugin.transform(files[name], directory + '/' + name)
76
+ transform: (name: string) => plugin.transform.handler(files[name], directory + '/' + name)
77
77
  };
78
78
  }
79
79
 
@@ -133,8 +133,8 @@ describe('compiler coverage', () => {
133
133
 
134
134
  it('fails the build when a signal is destructured out of a module namespace', () => {
135
135
  let files = {
136
- 'state.ts': "import { reactive } from '@esportsplus/reactivity';\nexport let count = reactive(0);",
137
- 'copy.ts': "import * as S from './state';\nconst { count } = S;\nexport let n = count;"
136
+ 'copy.ts': "import * as S from './state';\nconst { count } = S;\nexport let n = count;",
137
+ 'state.ts': "import { reactive } from '@esportsplus/reactivity';\nexport let count = reactive(0);"
138
138
  },
139
139
  { transform } = project(files);
140
140
 
@@ -0,0 +1,156 @@
1
+ import { afterEach, describe, expect, it } from 'vitest';
2
+ import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
3
+ import { join } from 'node:path';
4
+ import { NAMESPACE } from '~/compiler/constants';
5
+
6
+ import vite from '~/compiler/plugins/vite';
7
+
8
+
9
+ // `reactive`, and the bindings it creates, reached under any name: resolution may prune what it
10
+ // sends to the checker, never by name
11
+ const FILES: Record<string, string> = {
12
+ 'app.ts': [
13
+ "export { reactive as signalOf } from '@esportsplus/reactivity';",
14
+ "export { default as fallback } from './state';",
15
+ "export * from './mid';"
16
+ ].join('\n'),
17
+ 'consumer.ts': [
18
+ "import { deepCount, fallback, held, later, signalOf } from './app';",
19
+ "import * as app from './app';",
20
+ 'const { signalOf: unpacked } = app;',
21
+ 'export let a = signalOf(0);',
22
+ 'export let b = app.signalOf(0);',
23
+ "export let c = app['signalOf'](0);",
24
+ 'export let d = unpacked(0);',
25
+ 'export let e = held(0);',
26
+ 'export let f = () => fallback + deepCount + later;',
27
+ 'export let g = () => app.later + app.deepCount;'
28
+ ].join('\n'),
29
+ 'deep.ts': [
30
+ "export * from '@esportsplus/reactivity';",
31
+ "export { count as deepCount } from './state';"
32
+ ].join('\n'),
33
+ 'held.ts': [
34
+ "import { reactive } from '@esportsplus/reactivity';",
35
+ 'export const held = reactive;'
36
+ ].join('\n'),
37
+ 'local.ts': [
38
+ "import { reactive as x } from '@esportsplus/reactivity';",
39
+ 'export function before() { return late; }',
40
+ 'let late: number;',
41
+ 'late = x(0);',
42
+ 'export let after = late + 1;',
43
+ 'export let count = x(1);',
44
+ 'export let next = count + 1;',
45
+ 'export function shadow(x: (n: number) => number) { return x(1); }'
46
+ ].join('\n'),
47
+ 'mid.ts': [
48
+ "export * from './deep';",
49
+ "export { held } from './held';",
50
+ "export { later } from './state';"
51
+ ].join('\n'),
52
+ 'star.ts': [
53
+ "import { reactive } from './mid';",
54
+ 'export let s = reactive(0);',
55
+ 'export let t = s + 1;'
56
+ ].join('\n'),
57
+ 'state.ts': [
58
+ "import { reactive } from '@esportsplus/reactivity';",
59
+ 'export let count = reactive(0);',
60
+ 'export let later: number;',
61
+ 'later = reactive(1);',
62
+ 'export default reactive(2);'
63
+ ].join('\n')
64
+ };
65
+
66
+
67
+ let fixtures: string[] = [];
68
+
69
+
70
+ function compile(name: string, files: Record<string, string> = FILES): string {
71
+ let directory = mkdtempSync(join(process.cwd(), '.fixture-origins-')).replace(/\\/g, '/');
72
+
73
+ fixtures.push(directory);
74
+
75
+ for (let [file, code] of Object.entries(files)) {
76
+ writeFileSync(join(directory, file), code);
77
+ }
78
+
79
+ writeFileSync(join(directory, 'tsconfig.json'), JSON.stringify({
80
+ compilerOptions: { module: 'esnext', moduleResolution: 'bundler', strict: true, target: 'esnext', types: [] },
81
+ files: Object.keys(files).map(file => './' + file)
82
+ }));
83
+
84
+ let plugin = vite({ root: directory });
85
+
86
+ plugin.configResolved({ command: 'build', root: directory });
87
+
88
+ return plugin.transform.handler(files[name], directory + '/' + name)?.code ?? files[name];
89
+ }
90
+
91
+
92
+ afterEach(() => {
93
+ for (let i = 0, n = fixtures.length; i < n; i++) {
94
+ rmSync(fixtures[i], { force: true, recursive: true });
95
+ }
96
+
97
+ fixtures = [];
98
+ });
99
+
100
+
101
+ describe('origins reached under any name', () => {
102
+ it('compiles reactive() imported under a local alias, and leaves a parameter shadowing it', () => {
103
+ let output = compile('local.ts');
104
+
105
+ expect(output).toContain(`export let count = ${NAMESPACE}.signal(1);`);
106
+ expect(output).toContain(`export let next = ${NAMESPACE}.read(count) + 1;`);
107
+ expect(output).toContain('export function shadow(x: (n: number) => number) { return x(1); }');
108
+ });
109
+
110
+ it('reads a let assigned with reactive() later, before and after the assignment', () => {
111
+ let output = compile('local.ts');
112
+
113
+ expect(output).toContain(`export function before() { return ${NAMESPACE}.read(late); }`);
114
+ expect(output).toContain(`late = ${NAMESPACE}.signal(0);`);
115
+ expect(output).toContain(`export let after = ${NAMESPACE}.read(late) + 1;`);
116
+ });
117
+
118
+ it('reads a let assigned with reactive() in another module', () => {
119
+ let output = compile('consumer.ts');
120
+
121
+ expect(output).toContain(`${NAMESPACE}.read(later)`);
122
+ expect(output).toContain(`${NAMESPACE}.read(app.later)`);
123
+ });
124
+
125
+ it('compiles reactive() re-exported renamed through a central module, by name and through its namespace', () => {
126
+ let output = compile('consumer.ts');
127
+
128
+ for (let name of ['a', 'b', 'c', 'd']) {
129
+ expect(output).toContain(`export let ${name} = ${NAMESPACE}.signal(0);`);
130
+ }
131
+ });
132
+
133
+ it('compiles reactive() held by an exported const', () => {
134
+ expect(compile('consumer.ts')).toContain(`export let e = ${NAMESPACE}.signal(0);`);
135
+ });
136
+
137
+ it('reads a default export re-exported renamed, and bindings through export * two deep', () => {
138
+ let output = compile('consumer.ts');
139
+
140
+ expect(output).toContain(`export let f = () => ${NAMESPACE}.read(fallback) + ${NAMESPACE}.read(deepCount) + ${NAMESPACE}.read(later);`);
141
+ expect(output).toContain(`export let g = () => ${NAMESPACE}.read(app.later) + ${NAMESPACE}.read(app.deepCount);`);
142
+ });
143
+
144
+ it('compiles reactive() reached through export * two deep', () => {
145
+ let output = compile('star.ts');
146
+
147
+ expect(output).toContain(`export let s = ${NAMESPACE}.signal(0);`);
148
+ expect(output).toContain(`export let t = ${NAMESPACE}.read(s) + 1;`);
149
+ });
150
+
151
+ it('leaves a module that imports nothing untouched', () => {
152
+ let code = 'let reactive = (n: number) => n;\nexport let x = reactive(1);';
153
+
154
+ expect(compile('plain.ts', { ...FILES, 'plain.ts': code })).toBe(code);
155
+ });
156
+ });
@@ -0,0 +1,116 @@
1
+ import { afterEach, describe, expect, it } from 'vitest';
2
+ import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
3
+ import { join } from 'node:path';
4
+
5
+ import vite from '~/compiler/plugins/vite';
6
+
7
+
8
+ // Whole compiled modules, pinned byte for byte: every lowering form the compiler emits, each one
9
+ // alone in its range so no output depends on how nested rewrites compose
10
+ const FILES: Record<string, string> = {
11
+ 'app.ts': [
12
+ "export { reactive as make } from '@esportsplus/reactivity';",
13
+ "export * from './state';",
14
+ "export { default as total } from './state';"
15
+ ].join('\n'),
16
+ 'consumer.ts': [
17
+ "import { count as renamed, later, make, total } from './app';",
18
+ "import * as app from './app';",
19
+ 'export let a = make(0);',
20
+ 'export let b = () => renamed + total + later;',
21
+ 'export let c = () => app.count + app.list.length;',
22
+ "export let d = () => app['count'];",
23
+ 'export let e = () => { app.list[0] = 1; };'
24
+ ].join('\n'),
25
+ 'forms.ts': [
26
+ "import { reactive } from '@esportsplus/reactivity';",
27
+ 'declare function total(): number;',
28
+ 'declare let list: number[];',
29
+ 'declare let n: number;',
30
+ 'let count = reactive(0);',
31
+ 'let doubled = reactive(() => count * 2);',
32
+ 'let typed = reactive(n);',
33
+ 'let fn = reactive(total);',
34
+ 'let runtime = reactive(list);',
35
+ 'let obj = reactive({ a: 1, b: \'x\', items: [1, 2], sum: () => 3, neg: -1, source: list, flag: true });',
36
+ 'let shorthand = reactive({ list });',
37
+ 'let hinted = reactive<{ a: number; items: number[] }>({ a: 1, items: [] });',
38
+ 'let spread = reactive({ ...{ a: 1 }, b: 2 });',
39
+ 'let method = reactive({ m() { return 1; } });',
40
+ 'let arr = reactive([1, 2, 3]);',
41
+ 'let empty = reactive([] as string[]);',
42
+ 'let declared: number[] = reactive([4]);',
43
+ 'count = 5;',
44
+ 'count += 2;',
45
+ 'count ||= 3;',
46
+ 'count++;',
47
+ '--count;',
48
+ 'let p = count++;',
49
+ 'let q = ++count;',
50
+ 'let o = { count };',
51
+ 'let t = typeof count;',
52
+ 'arr[0] = 4;',
53
+ 'arr[1] += 2;',
54
+ 'arr[2]++;',
55
+ 'let r = ++arr[0];',
56
+ 'let size = arr.length;',
57
+ 'arr.length = 1;',
58
+ 'arr.length += 1;',
59
+ 'arr.length++;',
60
+ 'arr = [5, 6];',
61
+ 'arr = [];',
62
+ 'export { count, doubled, typed, fn, runtime, obj, shorthand, hinted, spread, method, empty, declared, p, q, o, t, r, size };',
63
+ 'export default reactive(0);'
64
+ ].join('\n'),
65
+ 'state.ts': [
66
+ "import { reactive } from '@esportsplus/reactivity';",
67
+ 'export let count = reactive(0);',
68
+ 'export let list = reactive([1, 2]);',
69
+ 'export let later: number;',
70
+ 'later = reactive(1);',
71
+ 'export default reactive(() => 2);'
72
+ ].join('\n')
73
+ };
74
+
75
+
76
+ let fixtures: string[] = [];
77
+
78
+
79
+ function compile(name: string): string | null {
80
+ let directory = mkdtempSync(join(process.cwd(), '.fixture-output-')).replace(/\\/g, '/');
81
+
82
+ fixtures.push(directory);
83
+
84
+ for (let [file, code] of Object.entries(FILES)) {
85
+ writeFileSync(join(directory, file), code);
86
+ }
87
+
88
+ writeFileSync(join(directory, 'tsconfig.json'), JSON.stringify({
89
+ compilerOptions: { module: 'esnext', moduleResolution: 'bundler', strict: true, target: 'esnext', types: [] },
90
+ files: Object.keys(FILES).map(file => './' + file)
91
+ }));
92
+
93
+ let plugin = vite({ root: directory });
94
+
95
+ plugin.configResolved({ command: 'build', root: directory });
96
+
97
+ return plugin.transform.handler(FILES[name], directory + '/' + name)?.code ?? null;
98
+ }
99
+
100
+
101
+ afterEach(() => {
102
+ for (let i = 0, n = fixtures.length; i < n; i++) {
103
+ rmSync(fixtures[i], { force: true, recursive: true });
104
+ }
105
+
106
+ fixtures = [];
107
+ });
108
+
109
+
110
+ describe('compiled output', () => {
111
+ for (let name of Object.keys(FILES)) {
112
+ it(name, () => {
113
+ expect(compile(name)).toMatchSnapshot();
114
+ });
115
+ }
116
+ });
@@ -1,5 +1,5 @@
1
1
  import { describe, expect, it } from 'vitest';
2
- import { computed, effect, flush, peek, read, signal, write } from '~/system';
2
+ import { computed, dispose, effect, flush, peek, read, signal, write } from '~/system';
3
3
 
4
4
 
5
5
  // Depth chosen well past the V8 call-stack frame limit (~10-15k): a recursive notify/update/dispose
@@ -44,7 +44,7 @@ describe('deep graphs', () => {
44
44
 
45
45
  expect(seen).toBe(DEPTH + 2);
46
46
 
47
- stop();
47
+ dispose(stop);
48
48
  }, 30000);
49
49
 
50
50
  it('a wide fan-out with deep branches settles (notify sibling traversal)', () => {
@@ -90,7 +90,7 @@ describe('deep graphs', () => {
90
90
 
91
91
  expect(observed).toBe(width * (2 + branchDepth));
92
92
 
93
- stop();
93
+ dispose(stop);
94
94
  });
95
95
 
96
96
  it('a diamond recomputes the sink once per source change (glitch-free, no redundant checks)', () => {
@@ -134,7 +134,7 @@ describe('deep graphs', () => {
134
134
  expect(cRuns).toBe(2);
135
135
  expect(peek(c)).toBe(7);
136
136
 
137
- stop();
137
+ dispose(stop);
138
138
  });
139
139
 
140
140
  it('a dynamic conditional dep does not over-recompute (DIRTY-break)', () => {
@@ -174,7 +174,7 @@ describe('deep graphs', () => {
174
174
 
175
175
  expect(cRuns).toBe(2);
176
176
 
177
- stop();
177
+ dispose(stop);
178
178
  });
179
179
 
180
180
  it('teardown of a 200k-deep chain completes without overflow; cleanup fires once per node', () => {
@@ -202,7 +202,7 @@ describe('deep graphs', () => {
202
202
  read(chain[DEPTH - 1]);
203
203
  });
204
204
 
205
- stop();
205
+ dispose(stop);
206
206
 
207
207
  expect(cleanups).toBe(DEPTH);
208
208
 
@@ -1,5 +1,5 @@
1
1
  import { describe, expect, it } from 'vitest';
2
- import { computed, effect, onCleanup, read, root, signal, write } from '~/system';
2
+ import { computed, dispose, effect, onCleanup, read, root, signal, write } from '~/system';
3
3
  import { ReactiveArray } from '~/reactive/array';
4
4
  import { ReactiveObject } from '~/reactive/object';
5
5
  import { waitFor } from './lib/wait-for';
@@ -14,7 +14,7 @@ describe('effect patterns', () => {
14
14
  let s = signal(0),
15
15
  runs = 0;
16
16
 
17
- let dispose = effect(() => {
17
+ let node = effect(() => {
18
18
  read(s);
19
19
  runs++;
20
20
  });
@@ -32,7 +32,7 @@ describe('effect patterns', () => {
32
32
 
33
33
  expect(runs).toBe(4);
34
34
 
35
- dispose();
35
+ dispose(node);
36
36
 
37
37
  write(s, 4);
38
38
  write(s, 5);
@@ -49,7 +49,7 @@ describe('effect patterns', () => {
49
49
  cleanupCalls = 0,
50
50
  effectValues: number[] = [];
51
51
 
52
- let dispose = effect(() => {
52
+ let node = effect(() => {
53
53
  let current = read(s);
54
54
 
55
55
  effectValues.push(current);
@@ -74,7 +74,7 @@ describe('effect patterns', () => {
74
74
  expect(effectValues).toEqual([0, 1, 2]);
75
75
  expect(cleanupCalls).toBe(2);
76
76
 
77
- dispose();
77
+ dispose(node);
78
78
 
79
79
  expect(cleanupCalls).toBe(3);
80
80
  });