@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
package/package.json CHANGED
@@ -1,13 +1,18 @@
1
1
  {
2
2
  "author": "ICJR",
3
3
  "dependencies": {
4
+ "@esportsplus/typescript": "^0.41.0",
4
5
  "@esportsplus/utilities": "^0.28.6"
5
6
  },
6
7
  "devDependencies": {
7
- "@esportsplus/typescript": "^0.38.0",
8
- "@types/node": "^26.4.1",
9
- "vite": "^8.2.2",
10
- "vitest": "^5.0.0"
8
+ "@types/node": "^26.6.6",
9
+ "oxlint": "^1.90.0",
10
+ "oxlint-tsgolint": "^7.0.2005",
11
+ "vite": "^8.3.5",
12
+ "vitest": "^5.1.0"
13
+ },
14
+ "engines": {
15
+ "node": ">=26"
11
16
  },
12
17
  "exports": {
13
18
  ".": {
@@ -36,13 +41,16 @@
36
41
  },
37
42
  "type": "module",
38
43
  "types": "build/index.d.ts",
39
- "version": "0.40.0",
44
+ "version": "0.42.0",
40
45
  "scripts": {
41
46
  "agent:bench": "vitest bench --run",
42
- "agent:test": "tsc --noEmit && vitest run",
47
+ "agent:test": "pnpm typecheck && vitest run",
43
48
  "bench": "vitest bench --run",
44
- "build": "tsc",
49
+ "build": "tsc -p tsconfig.build.json",
50
+ "lint": "oxlint -c oxlintrc.json --type-aware --deny-warnings",
45
51
  "test": "vitest run",
52
+ "typecheck": "tsc --noEmit",
53
+ "verify": "pnpm build && pnpm typecheck && pnpm lint && pnpm test",
46
54
  "-": "-"
47
55
  }
48
56
  }
@@ -1,10 +1,7 @@
1
1
  allowBuilds:
2
2
  esbuild: true
3
3
  minimumReleaseAgeExclude:
4
- - '@esportsplus/utilities@0.28.1 || 0.28.3 || 0.28.4 || 0.28.6'
5
- - '@esportsplus/typescript@0.31.0 || 0.32.0 || 0.33.0 || 0.33.2 || 0.33.4 || 0.34.1 || 0.34.3 || 0.34.4 || 0.36.0 || 0.36.1 || 0.37.0 || 0.38.0'
6
- - tsc-alias@1.9.4
7
- - '@vitest/mocker@5.0.0'
8
- - '@vitest/spy@5.0.0'
9
- - vitest@5.0.0
10
- - '@esportsplus/cli-passthrough@0.0.18'
4
+ - '@esportsplus/*'
5
+ - '@vitest/mocker@5.1.0'
6
+ - '@vitest/spy@5.1.0'
7
+ - vitest@5.1.0
@@ -68,10 +68,10 @@ function declare(ctx: { isReactiveCall: IsReactiveCall; replacements: Replacemen
68
68
  let typeParam = elementType ? `<${elementType}>` : '';
69
69
 
70
70
  ctx.replacements.push({
71
- node,
72
- generate: (sf) => expression.elements.length > 0
73
- ? ` new ${NAMESPACE}.ReactiveArray${typeParam}(${expression.getText(sf)})`
74
- : ` new ${NAMESPACE}.ReactiveArray${typeParam}()`
71
+ generate: (_sourceFile, text) => expression.elements.length > 0
72
+ ? ` new ${NAMESPACE}.ReactiveArray${typeParam}(${text(expression)})`
73
+ : ` new ${NAMESPACE}.ReactiveArray${typeParam}()`,
74
+ node
75
75
  });
76
76
  }
77
77
  }
@@ -126,10 +126,10 @@ function length(access: ts.PropertyAccessExpression, replacements: ReplacementIn
126
126
  let op = COMPOUND_OPERATORS.get(parent.operatorToken.kind);
127
127
 
128
128
  replacements.push({
129
- node: parent,
130
- generate: (sf) => op === undefined
131
- ? `${expr.getText(sf)}.$length = ${parent.right.getText(sf)}`
132
- : `${expr.getText(sf)}.$length = ${expr.getText(sf)}.length ${op} ${parent.right.getText(sf)}`
129
+ generate: (_sourceFile, text) => op === undefined
130
+ ? `${text(expr)}.$length = ${text(parent.right)}`
131
+ : `${text(expr)}.$length = ${text(expr)}.length ${op} ${text(parent.right)}`,
132
+ node: parent
133
133
  });
134
134
  }
135
135
  // arr.length++ / arr.length-- / ++arr.length / --arr.length
@@ -141,24 +141,22 @@ function length(access: ts.PropertyAccessExpression, replacements: ReplacementIn
141
141
  let op = parent.operator === ts.SyntaxKind.PlusPlusToken ? '+' : '-';
142
142
 
143
143
  replacements.push({
144
- node: parent,
145
- generate: (sf) => `${expr.getText(sf)}.$length = ${expr.getText(sf)}.length ${op} 1`
144
+ generate: (_sourceFile, text) => `${text(expr)}.$length = ${text(expr)}.length ${op} 1`,
145
+ node: parent
146
146
  });
147
147
  }
148
148
  // Read-only: arr.length -> arr.$length
149
149
  else {
150
150
  replacements.push({
151
- node: access,
152
- generate: (sf) => `${expr.getText(sf)}.$length`
151
+ generate: (_sourceFile, text) => `${text(expr)}.$length`,
152
+ node: access
153
153
  });
154
154
  }
155
155
  }
156
156
 
157
157
  // An element write must notify, so it goes through $set; operands are evaluated once
158
158
  function write(access: ts.ElementAccessExpression, ctx: ArrayTransformResult): void {
159
- let parent = access.parent!,
160
- array = (sf: ts.SourceFile) => access.expression.getText(sf),
161
- index = (sf: ts.SourceFile) => access.argumentExpression.getText(sf);
159
+ let parent = access.parent!;
162
160
 
163
161
  if (ts.isDeleteExpression(parent)) {
164
162
  ctx.failures.push({ message: 'an element of a reactive array cannot be deleted; use splice()', node: parent });
@@ -170,10 +168,10 @@ function write(access: ts.ElementAccessExpression, ctx: ArrayTransformResult): v
170
168
  right = parent.right;
171
169
 
172
170
  ctx.replacements.push({
173
- node: parent,
174
- generate: (sf) => op === undefined
175
- ? `${array(sf)}.$set(${index(sf)}, ${right.getText(sf)})`
176
- : `((_a, _k) => _a.$set(_k, _a[_k] ${op} (${right.getText(sf)})))(${array(sf)}, ${index(sf)})`
171
+ generate: (_sourceFile, text) => op === undefined
172
+ ? `${text(access.expression)}.$set(${text(access.argumentExpression)}, ${text(right)})`
173
+ : `((_a, _k) => _a.$set(_k, _a[_k] ${op} (${text(right)})))(${text(access.expression)}, ${text(access.argumentExpression)})`,
174
+ node: parent
177
175
  });
178
176
 
179
177
  return;
@@ -183,10 +181,15 @@ function write(access: ts.ElementAccessExpression, ctx: ArrayTransformResult): v
183
181
  op = unary.operator === ts.SyntaxKind.PlusPlusToken ? '+' : '-';
184
182
 
185
183
  ctx.replacements.push({
186
- node: unary,
187
- generate: (sf) => ts.isPrefixUnaryExpression(unary)
188
- ? `((_a, _k) => (_a.$set(_k, _a[_k] ${op} 1), _a[_k]))(${array(sf)}, ${index(sf)})`
189
- : `((_a, _k, _v) => (_a.$set(_k, _v ${op} 1), _v))(${array(sf)}, ${index(sf)}, ${array(sf)}[${index(sf)}])`
184
+ generate: (_sourceFile, text) => {
185
+ let array = text(access.expression),
186
+ index = text(access.argumentExpression);
187
+
188
+ return ts.isPrefixUnaryExpression(unary)
189
+ ? `((_a, _k) => (_a.$set(_k, _a[_k] ${op} 1), _a[_k]))(${array}, ${index})`
190
+ : `((_a, _k, _v) => (_a.$set(_k, _v ${op} 1), _v))(${array}, ${index}, ${array}[${index}])`;
191
+ },
192
+ node: unary
190
193
  });
191
194
  }
192
195
 
@@ -234,10 +237,10 @@ export default (sourceFile: ts.SourceFile, bindings: Bindings, isReactiveCall: I
234
237
  elements = scope.unwrap(assignment.right) as ts.ArrayLiteralExpression;
235
238
 
236
239
  result.replacements.push({
237
- node: assignment,
238
- generate: (sf) => elements.elements.length > 0
239
- ? `${name}.splice(0, ${name}.length, ...${elements.getText(sf)})`
240
- : `${name}.splice(0, ${name}.length)`
240
+ generate: (_sourceFile, text) => elements.elements.length > 0
241
+ ? `${name}.splice(0, ${name}.length, ...${text(elements)})`
242
+ : `${name}.splice(0, ${name}.length)`,
243
+ node: assignment
241
244
  });
242
245
  }
243
246
 
@@ -1,5 +1,5 @@
1
1
  import { ts } from '@esportsplus/typescript';
2
- import { references } from '@esportsplus/typescript/compiler';
2
+ import { checker, references } from '@esportsplus/typescript/compiler';
3
3
  import type { Origin } from '@esportsplus/typescript/compiler';
4
4
  import { ENTRYPOINT, PACKAGE_NAME, TYPES } from './constants';
5
5
  import type { Bindings } from './types';
@@ -7,9 +7,9 @@ import type { Bindings } from './types';
7
7
 
8
8
  type Assignment = { call: ts.CallExpression; target: ts.Identifier };
9
9
 
10
- // Per program snapshot: answers about declarations and types hold for the snapshot's lifetime
10
+ // Per program snapshot. `kinds` saves no checker round-trip (the shared memo answers every query it
11
+ // makes), but kind() runs on every read of a binding and re-deriving it per read dominates a warm transform
11
12
  type Caches = {
12
- arrays: Map<number, boolean>;
13
13
  assignments: Map<string, Map<string, Assignment[]>>;
14
14
  kinds: Map<string, TYPES | null>;
15
15
  };
@@ -67,7 +67,7 @@ function cachesOf(program: ts.Program): Caches {
67
67
  let found = caches.get(program);
68
68
 
69
69
  if (!found) {
70
- found = { arrays: new Map(), assignments: new Map(), kinds: new Map() };
70
+ found = { assignments: new Map(), kinds: new Map() };
71
71
  caches.set(program, found);
72
72
  }
73
73
 
@@ -149,8 +149,7 @@ const arrays = (bindings: Bindings, nodes: ts.Node[]): Set<ts.Node> => {
149
149
  return result;
150
150
  }
151
151
 
152
- let cache = cachesOf(bindings.program).arrays,
153
- types = bindings.checker.getTypeAtLocation(nodes);
152
+ let types = bindings.checker.getTypeAtLocation(nodes);
154
153
 
155
154
  for (let i = 0, n = nodes.length; i < n; i++) {
156
155
  let type = types[i];
@@ -159,16 +158,9 @@ const arrays = (bindings: Bindings, nodes: ts.Node[]): Set<ts.Node> => {
159
158
  continue;
160
159
  }
161
160
 
162
- let branded = cache.get(type.id);
161
+ let property = bindings.checker.getPropertyOfType(bindings.checker.getNonNullableType(type) ?? type, ARRAY_BRAND);
163
162
 
164
- if (branded === undefined) {
165
- let property = bindings.checker.getPropertyOfType(bindings.checker.getNonNullableType(type) ?? type, ARRAY_BRAND);
166
-
167
- branded = property?.declarations?.some(declaration => files.has(declaration.path)) === true;
168
- cache.set(type.id, branded);
169
- }
170
-
171
- if (branded) {
163
+ if (property?.declarations?.some(declaration => files.has(declaration.path))) {
172
164
  result.add(nodes[i]);
173
165
  }
174
166
  }
@@ -204,13 +196,19 @@ const classify = (bindings: Bindings, call: ts.CallExpression): TYPES | null =>
204
196
  return type ? kindOf(bindings.checker, type) : null;
205
197
  };
206
198
 
207
- const create = (checker: ts.Checker, program: ts.Program, sourceFile: ts.SourceFile): Bindings => ({
208
- checker,
209
- entries: new Set(references.exported(checker, program, PACKAGE_NAME, ENTRYPOINT).map(references.key)),
210
- origins: references.origins(checker, program, sourceFile),
211
- program,
212
- sourceFile
213
- });
199
+ // Memoized per program (an already memoized checker is kept as is), so batched and repeated queries
200
+ // share one cache whoever hands the checker in
201
+ const create = (queries: ts.Checker, program: ts.Program, sourceFile: ts.SourceFile): Bindings => {
202
+ let memoized = checker.cached(queries, program);
203
+
204
+ return {
205
+ checker: memoized,
206
+ entries: new Set(references.exported(memoized, program, PACKAGE_NAME, ENTRYPOINT).map(references.key)),
207
+ origins: references.origins(memoized, program, sourceFile),
208
+ program,
209
+ sourceFile
210
+ };
211
+ };
214
212
 
215
213
  // Whether a callee expression denotes `reactive`: `reactive`, an alias, `ns.reactive`, `ns['reactive']`
216
214
  const denotes = (bindings: Bindings, expression: ts.Expression): boolean => {
@@ -293,6 +291,37 @@ const origin = (bindings: Bindings, node: ts.Node): Origin | null => {
293
291
  return bindings.origins.get(node) ?? references.origin(bindings.checker, bindings.program, node);
294
292
  };
295
293
 
294
+ // Types of the arguments classify() reads from the checker, requested in one round-trip; the
295
+ // memoized checker then answers each classify() from that request
296
+ const prefetch = (bindings: Bindings, calls: ts.CallExpression[]): void => {
297
+ let nodes: ts.Node[] = [];
298
+
299
+ for (let i = 0, n = calls.length; i < n; i++) {
300
+ let arg = calls[i].arguments[0];
301
+
302
+ if (!arg || calls[i].arguments.length !== 1 || ts.isSpreadElement(arg)) {
303
+ continue;
304
+ }
305
+
306
+ let value = unwrap(arg);
307
+
308
+ if (
309
+ ts.isArrayLiteralExpression(value) ||
310
+ ts.isArrowFunction(value) ||
311
+ ts.isFunctionExpression(value) ||
312
+ ts.isObjectLiteralExpression(value)
313
+ ) {
314
+ continue;
315
+ }
316
+
317
+ nodes.push(arg);
318
+ }
319
+
320
+ if (nodes.length > 0) {
321
+ bindings.checker.getTypeAtLocation(nodes);
322
+ }
323
+ };
324
+
296
325
 
297
326
  // The expression a value comes from, without parentheses, `!` and type assertions
298
327
  const unwrap = (expression: ts.Expression): ts.Expression => {
@@ -300,4 +329,4 @@ const unwrap = (expression: ts.Expression): ts.Expression => {
300
329
  };
301
330
 
302
331
 
303
- export default { arrays, classify, create, denotes, holds, isReactiveCall, kind, origin, unwrap };
332
+ export default { arrays, classify, create, denotes, holds, isReactiveCall, kind, origin, prefetch, unwrap };
@@ -9,6 +9,11 @@ import primitives from './primitives';
9
9
  import scope from './bindings';
10
10
 
11
11
 
12
+ // A binding from this package is only ever reached through an import, so a module without one has
13
+ // nothing to compile and is not resolved at all
14
+ const REGEX_IMPORT = /\bimport\b|\brequire\s*\(/;
15
+
16
+
12
17
  // Every use the compiler cannot lower fails the build, all of them reported at once: shipping one
13
18
  // would leave an uncompiled reactive() or an unread signal in the output
14
19
  function failed(sourceFile: ts.SourceFile, failures: Failure[]): Error {
@@ -64,13 +69,14 @@ function isLowered(site: ts.Node): boolean {
64
69
  }
65
70
 
66
71
 
67
- // `reactive`, or the member access naming it (`ns.reactive`, `ns['reactive']`)
72
+ // `reactive`, or the member access naming it (`ns.reactive`, `ns['reactive']`); in `list[reactive]`
73
+ // the key is the use
68
74
  function siteOf(identifier: ts.Node): ts.Node {
69
75
  let parent = identifier.parent!;
70
76
 
71
77
  if (
72
78
  (ts.isPropertyAccessExpression(parent) && parent.name === identifier) ||
73
- (ts.isElementAccessExpression(parent) && parent.argumentExpression === identifier)
79
+ (ts.isElementAccessExpression(parent) && parent.argumentExpression === identifier && (ts.isStringLiteral(identifier) || ts.isNoSubstitutionTemplateLiteral(identifier)))
74
80
  ) {
75
81
  return parent;
76
82
  }
@@ -79,34 +85,30 @@ function siteOf(identifier: ts.Node): ts.Node {
79
85
  }
80
86
 
81
87
 
82
- // No text patterns: a module can reach `reactive`, or read a reactive binding, under any name
83
- // (a barrel's rename, a default re-export, a namespace), so every module is resolved against the
84
- // program and compiled exactly where its values resolve to reactive declarations
88
+ // No name patterns: a module can reach `reactive`, or read a reactive binding, under any name
89
+ // (a barrel's rename, a default re-export, a namespace), so every module that imports anything is
90
+ // resolved against the program and compiled exactly where its values resolve to reactive declarations
85
91
  export default {
92
+ name: PACKAGE_NAME,
86
93
  transform: (ctx: TransformContext) => {
94
+ if (!REGEX_IMPORT.test(ctx.code)) {
95
+ return {};
96
+ }
97
+
87
98
  let { checker, program, sourceFile } = ctx,
88
99
  bindings = scope.create(checker, program, sourceFile),
89
- calls = new Set<ts.Node>(),
100
+ calls = new Set<ts.CallExpression>(),
90
101
  dependencies = new Set<string>(),
102
+ entries = new Set<ts.Node>(),
91
103
  failures: Failure[] = [],
92
104
  self = sourceFile.fileName.toLowerCase();
93
105
 
94
106
  for (let [identifier, found] of bindings.origins) {
95
- let entry = scope.holds(bindings, found);
96
-
97
- if (!entry && scope.kind(bindings, found) === null) {
107
+ if (!scope.holds(bindings, found)) {
98
108
  continue;
99
109
  }
100
110
 
101
- for (let file of [...found.through, found.declaration.path]) {
102
- if (file.toLowerCase() !== self) {
103
- dependencies.add(file);
104
- }
105
- }
106
-
107
- if (!entry) {
108
- continue;
109
- }
111
+ entries.add(identifier);
110
112
 
111
113
  let site = siteOf(identifier),
112
114
  parent = site.parent;
@@ -126,7 +128,22 @@ export default {
126
128
  }
127
129
  }
128
130
 
129
- let isReactiveCall = (node: ts.Node): node is ts.CallExpression => calls.has(node),
131
+ // Every call is known before any binding is classified, so their argument types arrive in one request
132
+ scope.prefetch(bindings, [...calls]);
133
+
134
+ for (let [identifier, found] of bindings.origins) {
135
+ if (!entries.has(identifier) && scope.kind(bindings, found) === null) {
136
+ continue;
137
+ }
138
+
139
+ for (let file of [...found.through, found.declaration.path]) {
140
+ if (file.toLowerCase() !== self) {
141
+ dependencies.add(file);
142
+ }
143
+ }
144
+ }
145
+
146
+ let isReactiveCall = (node: ts.Node): node is ts.CallExpression => calls.has(node as ts.CallExpression),
130
147
  intents = {
131
148
  imports: [] as ImportIntent[],
132
149
  prepend: [] as string[],
@@ -179,7 +196,7 @@ export default {
179
196
  }
180
197
 
181
198
  intents.replacements.push({
182
- generate: () => `${NAMESPACE}.reactive(${call.arguments.map(a => a.getText(sourceFile)).join(', ')})`,
199
+ generate: (_sourceFile, text) => `${NAMESPACE}.reactive(${call.arguments.map(text).join(', ')})`,
183
200
  node: call
184
201
  });
185
202
  }
@@ -9,7 +9,7 @@ interface AnalyzedProperty {
9
9
  isStatic: boolean;
10
10
  key: string;
11
11
  type: TYPES;
12
- valueText: string;
12
+ value: ts.Expression;
13
13
  }
14
14
 
15
15
  type ObjectTransformResult = {
@@ -26,16 +26,16 @@ interface ReactiveObjectCall {
26
26
  }
27
27
 
28
28
  interface VisitContext {
29
- bindings: Bindings;
30
29
  calls: ReactiveObjectCall[];
31
30
  isReactiveCall: IsReactiveCall;
32
31
  sourceFile: ts.SourceFile;
32
+ typed: AnalyzedProperty[];
33
33
  }
34
34
 
35
35
 
36
- function analyzeProperty(prop: ts.ObjectLiteralElementLike, bindings: Bindings): AnalyzedProperty | null {
37
- let sourceFile = bindings.sourceFile;
38
-
36
+ // Syntax decides every property except a non-literal value, recorded in `typed` as a signal until
37
+ // its type says whether it is an array
38
+ function analyzeProperty(prop: ts.ObjectLiteralElementLike, typed: AnalyzedProperty[]): AnalyzedProperty | null {
39
39
  if (!ts.isPropertyAssignment(prop)) {
40
40
  return null;
41
41
  }
@@ -50,8 +50,7 @@ function analyzeProperty(prop: ts.ObjectLiteralElementLike, bindings: Bindings):
50
50
  }
51
51
 
52
52
  let unwrapped = prop.initializer,
53
- value = unwrapped,
54
- valueText = value.getText(sourceFile);
53
+ value = unwrapped;
55
54
 
56
55
  while (ts.isAsExpression(unwrapped) || ts.isTypeAssertion(unwrapped) || ts.isParenthesizedExpression(unwrapped)) {
57
56
  unwrapped = unwrapped.expression;
@@ -68,12 +67,12 @@ function analyzeProperty(prop: ts.ObjectLiteralElementLike, bindings: Bindings):
68
67
  type.typeName.text === 'Array'
69
68
  )
70
69
  ) {
71
- return { isStatic: false, key, type: TYPES.Array, valueText };
70
+ return { isStatic: false, key, type: TYPES.Array, value };
72
71
  }
73
72
  }
74
73
 
75
74
  if (ts.isArrowFunction(unwrapped) || ts.isFunctionExpression(unwrapped)) {
76
- return { isStatic: false, key, type: TYPES.Computed, valueText };
75
+ return { isStatic: false, key, type: TYPES.Computed, value };
77
76
  }
78
77
 
79
78
  if (ts.isArrayLiteralExpression(unwrapped)) {
@@ -86,26 +85,24 @@ function analyzeProperty(prop: ts.ObjectLiteralElementLike, bindings: Bindings):
86
85
  }
87
86
  }
88
87
 
89
- return { isStatic, key, type: TYPES.Array, valueText };
88
+ return { isStatic, key, type: TYPES.Array, value };
90
89
  }
91
90
 
92
- // A non-literal array is typed Reactive<T[]> on the object, so it must become a ReactiveArray
93
- // exactly like a literal one
94
- let type = isStaticValue(unwrapped) ? undefined : bindings.checker.getTypeAtLocation(value);
91
+ let property: AnalyzedProperty = {
92
+ isStatic: isStaticValue(value),
93
+ key,
94
+ type: TYPES.Signal,
95
+ value
96
+ };
95
97
 
96
- if (type && (bindings.checker.isArrayType(type) || bindings.checker.isTupleType(type))) {
97
- return { isStatic: false, key, type: TYPES.Array, valueText };
98
+ if (!isStaticValue(unwrapped)) {
99
+ typed.push(property);
98
100
  }
99
101
 
100
- return {
101
- isStatic: isStaticValue(value),
102
- key,
103
- type: TYPES.Signal,
104
- valueText
105
- };
102
+ return property;
106
103
  }
107
104
 
108
- function buildClassCode(classname: string, properties: AnalyzedProperty[], typehint: string | null): string {
105
+ function buildClassCode(classname: string, properties: AnalyzedProperty[], sourceFile: ts.SourceFile, typehint: string | null): string {
109
106
  let accessors: string[] = [],
110
107
  body: string[] = [],
111
108
  constraint: string[] = [],
@@ -115,7 +112,7 @@ function buildClassCode(classname: string, properties: AnalyzedProperty[], typeh
115
112
  setters = 0;
116
113
 
117
114
  for (let i = 0, n = properties.length; i < n; i++) {
118
- let { isStatic, key, type, valueText } = properties[i],
115
+ let { isStatic, key, type, value: initializer } = properties[i],
119
116
  generic = typehint ? `T['${key}']` : `T${parameters.length}`,
120
117
  parameter = `_p${parameters.length}`;
121
118
 
@@ -137,7 +134,8 @@ function buildClassCode(classname: string, properties: AnalyzedProperty[], typeh
137
134
  ${NAMESPACE}.write(this.#${key}, ${value});
138
135
  }
139
136
  `);
140
- fields.push(`#${key} = this[${NAMESPACE}.SIGNAL](${valueText});`);
137
+ // A static value is a literal, which no plugin rewrites, so its source is the output
138
+ fields.push(`#${key} = this[${NAMESPACE}.SIGNAL](${initializer.getText(sourceFile)});`);
141
139
  }
142
140
  else {
143
141
  accessors.push(`
@@ -245,7 +243,7 @@ function visit(ctx: VisitContext, node: ts.Node): void {
245
243
  return;
246
244
  }
247
245
 
248
- let analyzed = analyzeProperty(prop, ctx.bindings);
246
+ let analyzed = analyzeProperty(prop, ctx.typed);
249
247
 
250
248
  if (!analyzed) {
251
249
  node.forEachChild(n => visit(ctx, n));
@@ -271,14 +269,29 @@ function visit(ctx: VisitContext, node: ts.Node): void {
271
269
 
272
270
  export default (sourceFile: ts.SourceFile, bindings: Bindings, isReactiveCall: IsReactiveCall): ObjectTransformResult => {
273
271
  let ctx: VisitContext = {
274
- bindings,
275
272
  calls: [],
276
273
  isReactiveCall,
277
- sourceFile
274
+ sourceFile,
275
+ typed: []
278
276
  };
279
277
 
280
278
  visit(ctx, sourceFile);
281
279
 
280
+ // A non-literal array is typed Reactive<T[]> on the object, so it must become a ReactiveArray
281
+ // exactly like a literal one; every such value in the file is typed in one request
282
+ if (ctx.typed.length > 0) {
283
+ let types = bindings.checker.getTypeAtLocation(ctx.typed.map(property => property.value));
284
+
285
+ for (let i = 0, n = ctx.typed.length; i < n; i++) {
286
+ let type = types[i];
287
+
288
+ if (type && (bindings.checker.isArrayType(type) || bindings.checker.isTupleType(type))) {
289
+ ctx.typed[i].isStatic = false;
290
+ ctx.typed[i].type = TYPES.Array;
291
+ }
292
+ }
293
+ }
294
+
282
295
  let prepend: string[] = [],
283
296
  replacements: ReplacementIntent[] = [];
284
297
 
@@ -286,13 +299,13 @@ export default (sourceFile: ts.SourceFile, bindings: Bindings, isReactiveCall: I
286
299
  let call = ctx.calls[i],
287
300
  typehint = call.typehint;
288
301
 
289
- prepend.push(buildClassCode(call.classname, call.properties, typehint));
302
+ prepend.push(buildClassCode(call.classname, call.properties, sourceFile, typehint));
290
303
  replacements.push({
291
- generate: () => {
304
+ generate: (_sourceFile, text) => {
292
305
  // Every property but a static signal (baked into its field) is a constructor parameter
293
306
  let args = call.properties
294
307
  .filter(({ isStatic, type }) => typehint || !isStatic || type !== TYPES.Signal)
295
- .map(p => p.valueText)
308
+ .map(p => text(p.value))
296
309
  .join(', ');
297
310
 
298
311
  return typehint
@@ -144,13 +144,14 @@ function isPatternTarget(node: ts.Node): boolean {
144
144
  ((ts.isForOfStatement(parent) || ts.isForInStatement(parent)) && parent.initializer === current);
145
145
  }
146
146
 
147
- // A binding read as `ns.count` / `ns['count']` is rewritten as a whole
147
+ // A binding read as `ns.count` / `ns['count']` is rewritten as a whole; in `list[count]` the key is
148
+ // the read
148
149
  function readNode(identifier: ts.Node): ts.Expression {
149
150
  let parent = identifier.parent!;
150
151
 
151
152
  if (
152
153
  (ts.isPropertyAccessExpression(parent) && parent.name === identifier) ||
153
- (ts.isElementAccessExpression(parent) && parent.argumentExpression === identifier)
154
+ (ts.isElementAccessExpression(parent) && parent.argumentExpression === identifier && (ts.isStringLiteral(identifier) || ts.isNoSubstitutionTemplateLiteral(identifier)))
154
155
  ) {
155
156
  return parent;
156
157
  }
@@ -175,11 +176,13 @@ function reference(ctx: TransformContext, node: ts.Expression, kind: TYPES): voi
175
176
  return;
176
177
  }
177
178
 
178
- let text = node.getText(ctx.bindings.sourceFile);
179
+ // The binding itself (`count`, `ns.count`, `ns['count']`) holds nothing another rewrite replaces,
180
+ // so its source is copied as is: text() walks the file's intents on every call
181
+ let binding = node.getText(ctx.bindings.sourceFile);
179
182
 
180
183
  if (ts.isShorthandPropertyAssignment(parent)) {
181
184
  ctx.replacements.push({
182
- generate: () => `${text}: ${NAMESPACE}.read(${text})`,
185
+ generate: () => `${binding}: ${NAMESPACE}.read(${binding})`,
183
186
  node: parent
184
187
  });
185
188
 
@@ -208,7 +211,7 @@ function reference(ctx: TransformContext, node: ts.Expression, kind: TYPES): voi
208
211
 
209
212
  if (!writeCtx) {
210
213
  ctx.replacements.push({
211
- generate: () => `${NAMESPACE}.read(${text})`,
214
+ generate: () => `${NAMESPACE}.read(${binding})`,
212
215
  node
213
216
  });
214
217
 
@@ -224,7 +227,7 @@ function reference(ctx: TransformContext, node: ts.Expression, kind: TYPES): voi
224
227
  let right = parent.right;
225
228
 
226
229
  ctx.replacements.push({
227
- generate: (sf) => `${NAMESPACE}.write(${text}, ${right.getText(sf)})`,
230
+ generate: (_sourceFile, text) => `${NAMESPACE}.write(${binding}, ${text(right)})`,
228
231
  node: parent
229
232
  });
230
233
  }
@@ -233,7 +236,7 @@ function reference(ctx: TransformContext, node: ts.Expression, kind: TYPES): voi
233
236
  right = parent.right;
234
237
 
235
238
  ctx.replacements.push({
236
- generate: (sf) => `${NAMESPACE}.write(${text}, ${text}.value ${op} ${right.getText(sf)})`,
239
+ generate: (_sourceFile, text) => `${NAMESPACE}.write(${binding}, ${binding}.value ${op} ${text(right)})`,
237
240
  node: parent
238
241
  });
239
242
  }
@@ -243,13 +246,13 @@ function reference(ctx: TransformContext, node: ts.Expression, kind: TYPES): voi
243
246
 
244
247
  if (ts.isExpressionStatement(unary.parent)) {
245
248
  ctx.replacements.push({
246
- generate: () => `${NAMESPACE}.write(${text}, ${text}.value ${delta})`,
249
+ generate: () => `${NAMESPACE}.write(${binding}, ${binding}.value ${delta})`,
247
250
  node: unary
248
251
  });
249
252
  }
250
253
  else if (ts.isPrefixUnaryExpression(unary)) {
251
254
  ctx.replacements.push({
252
- generate: () => `(${NAMESPACE}.write(${text}, ${text}.value ${delta}), ${text}.value)`,
255
+ generate: () => `(${NAMESPACE}.write(${binding}, ${binding}.value ${delta}), ${binding}.value)`,
253
256
  node: unary
254
257
  });
255
258
  }
@@ -257,7 +260,7 @@ function reference(ctx: TransformContext, node: ts.Expression, kind: TYPES): voi
257
260
  let tmp = `_t${ctx.tmpCounter++}`;
258
261
 
259
262
  ctx.replacements.push({
260
- generate: () => `((${tmp}) => (${NAMESPACE}.write(${text}, ${tmp} ${delta}), ${tmp}))(${text}.value)`,
263
+ generate: () => `((${tmp}) => (${NAMESPACE}.write(${binding}, ${tmp} ${delta}), ${tmp}))(${binding}.value)`,
261
264
  node: unary
262
265
  });
263
266
  }