@reticlehq/react 2.4.0 → 2.6.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,7 +31,7 @@
31
31
  * an opaque evaluation error).
32
32
  */
33
33
  export function readComponentAt(el) {
34
- if (el === null)
34
+ if (null === el)
35
35
  return { ok: false, reason: 'no-element' };
36
36
  try {
37
37
  const FIBER_PREFIXES = ['__reactFiber$', '__reactInternalInstance$'];
@@ -39,6 +39,23 @@ export function readComponentAt(el) {
39
39
  if (key === undefined)
40
40
  return { ok: false, reason: 'no-fiber' };
41
41
  let fiber = el[key];
42
+ // React keeps two fibers per element (current ↔ work-in-progress) and swaps them on every commit,
43
+ // but the `__reactFiber$…` key keeps pointing at the one created at mount — so from the second
44
+ // render on it is the PREVIOUS commit's fiber half the time, and hooks read one commit behind.
45
+ // Climb to the HostRoot: the tree is the committed one iff its FiberRoot's `current` is the top
46
+ // fiber reached. Otherwise the committed counterpart is `alternate`. Unknown shape => leave as-is.
47
+ const alternate = fiber?.alternate;
48
+ if (fiber !== null && fiber !== undefined && alternate !== null && alternate !== undefined) {
49
+ let top = fiber;
50
+ let climbed = 0;
51
+ while (top.return !== null && top.return !== undefined && climbed < 200) {
52
+ top = top.return;
53
+ climbed += 1;
54
+ }
55
+ const committed = top.stateNode?.current;
56
+ if ('object' === typeof committed && null !== committed && committed !== top)
57
+ fiber = alternate;
58
+ }
42
59
  // Climb to the nearest function-component fiber; host fibers (strings) carry no hook state.
43
60
  let guard = 0;
44
61
  while (fiber !== null &&
@@ -48,20 +65,20 @@ export function readComponentAt(el) {
48
65
  fiber = fiber.return;
49
66
  guard += 1;
50
67
  }
51
- if (fiber === null || fiber === undefined)
68
+ if (null === fiber || fiber === undefined)
52
69
  return { ok: false, reason: 'no-component-fiber' };
53
70
  const type = fiber.type;
54
71
  const component = type?.displayName ?? type?.name ?? undefined;
55
72
  const hooks = [];
56
73
  let hook = fiber.memoizedState;
57
74
  let i = 0;
58
- while (hook !== null && typeof hook === 'object' && i < 100) {
75
+ while (hook !== null && 'object' === typeof hook && i < 100) {
59
76
  const state = hook.memoizedState;
60
77
  // Only carry JSON-serializable primitives/plain values across the boundary; a function or DOM
61
78
  // node in a hook (a ref, a callback) is dropped rather than breaking the whole read.
62
- hooks.push(typeof state === 'function' ? '[fn]' : state);
79
+ hooks.push('function' === typeof state ? '[fn]' : state);
63
80
  const next = hook.next;
64
- if (next === null || typeof next !== 'object')
81
+ if (null === next || typeof next !== 'object')
65
82
  break;
66
83
  hook = next;
67
84
  i += 1;
@@ -86,7 +103,7 @@ export function buildReaderExpression(selector) {
86
103
  * rather than surfacing as a confusing tool output. */
87
104
  export function parseComponentRead(raw) {
88
105
  if (typeof raw !== 'object' ||
89
- raw === null ||
106
+ null === raw ||
90
107
  typeof raw.ok !== 'boolean') {
91
108
  return { ok: false, reason: 'malformed-read' };
92
109
  }
@@ -7,10 +7,11 @@
7
7
  export interface CommitAggregator {
8
8
  onCommit(): void;
9
9
  }
10
- export interface CommitAggregatorOptions {
10
+ interface CommitAggregatorOptions {
11
11
  /** Injected scheduler (rAF / setTimeout) — deterministic in tests. */
12
12
  schedule: (fn: () => void) => void;
13
13
  /** Called once per window with the number of commits accumulated since the last flush. */
14
14
  flush: (commits: number) => void;
15
15
  }
16
16
  export declare function createCommitAggregator(options: CommitAggregatorOptions): CommitAggregator;
17
+ export {};
@@ -8,7 +8,7 @@ import { RETICLE_ERROR_BOUNDARY_SIGNAL, TRANSPORT_LIMITS } from '@reticlehq/core
8
8
  */
9
9
  const MAX_STACK_LEN = TRANSPORT_LIMITS.MAX_STACK_LENGTH;
10
10
  function cap(value) {
11
- if (value === undefined || value.length === 0)
11
+ if (value === undefined || 0 === value.length)
12
12
  return undefined;
13
13
  return value.length > MAX_STACK_LEN ? value.slice(0, MAX_STACK_LEN) : value;
14
14
  }
@@ -20,7 +20,7 @@ const HYDRATION_ERROR_CODES = new Set([
20
20
  '425',
21
21
  ]);
22
22
  function cap(value) {
23
- if (value === undefined || value.length === 0)
23
+ if (value === undefined || 0 === value.length)
24
24
  return undefined;
25
25
  return value.length > MAX_STACK_LEN ? value.slice(0, MAX_STACK_LEN) : value;
26
26
  }
package/dist/index.d.ts CHANGED
@@ -1,5 +1,24 @@
1
1
  import { type ComponentInfo } from '@reticlehq/browser';
2
2
  import { type ComponentStateResult } from '@reticlehq/core';
3
+ /**
4
+ * Make a source pointer repo-relative.
5
+ *
6
+ * Both sides of this comparison are native paths, and on Windows that means BACKSLASHES on both:
7
+ * `C:\Users\dev\app` and `C:\Users\dev\app\src\Counter.tsx`. Building the prefix as
8
+ * `root + '/'` produced `C:\Users\dev\app/` — a mixed separator that can never match the start of
9
+ * the file — so on Windows the root was never stripped and every pointer came back as an absolute
10
+ * path from the developer's own machine. Exactly what the root exists to prevent, on the platform
11
+ * carrying two thirds of Reticle's users.
12
+ *
13
+ * Normalizing to posix fixes three cases at once: native Windows, the mixed pair Vite produces (it
14
+ * normalizes module ids to forward slashes while `cwd()` stays native), and plain posix. The result
15
+ * is always forward-slashed, because flow anchors and `file:line` pointers all speak posix.
16
+ *
17
+ * The drive letter is compared case-insensitively — Windows treats `C:\App` and `c:\app` as the
18
+ * same path, and the two spellings routinely come from different APIs — but the returned suffix is
19
+ * sliced from the ORIGINAL file, so nothing else gets case-folded.
20
+ */
21
+ export declare function relativeToRoot(file: string): string;
3
22
  /** Walk the fiber tree from a DOM node up to the root, collecting components + source. */
4
23
  export declare function identify(el: Element): ComponentInfo | null;
5
24
  export declare function readState(el: Element): ComponentStateResult;
package/dist/index.js CHANGED
@@ -6,12 +6,40 @@ import { ComponentStateReason, DATA_RETICLE_SOURCE_ATTR, RETICLE_ROOT_GLOBAL, }
6
6
  * app was on — and `/Users/someone/tmp/wt/apps/x/src/A.tsx` is noise in a report, not a pointer.
7
7
  * The build plugins define the root; without one we return the path unchanged rather than guessing.
8
8
  */
9
- function relativeToRoot(file) {
9
+ /** Backslashes to forward slashes, so a Windows path can be compared with a posix one. */
10
+ function toPosix(path) {
11
+ return path.replace(/\\/g, '/');
12
+ }
13
+ /**
14
+ * Make a source pointer repo-relative.
15
+ *
16
+ * Both sides of this comparison are native paths, and on Windows that means BACKSLASHES on both:
17
+ * `C:\Users\dev\app` and `C:\Users\dev\app\src\Counter.tsx`. Building the prefix as
18
+ * `root + '/'` produced `C:\Users\dev\app/` — a mixed separator that can never match the start of
19
+ * the file — so on Windows the root was never stripped and every pointer came back as an absolute
20
+ * path from the developer's own machine. Exactly what the root exists to prevent, on the platform
21
+ * carrying two thirds of Reticle's users.
22
+ *
23
+ * Normalizing to posix fixes three cases at once: native Windows, the mixed pair Vite produces (it
24
+ * normalizes module ids to forward slashes while `cwd()` stays native), and plain posix. The result
25
+ * is always forward-slashed, because flow anchors and `file:line` pointers all speak posix.
26
+ *
27
+ * The drive letter is compared case-insensitively — Windows treats `C:\App` and `c:\app` as the
28
+ * same path, and the two spellings routinely come from different APIs — but the returned suffix is
29
+ * sliced from the ORIGINAL file, so nothing else gets case-folded.
30
+ */
31
+ export function relativeToRoot(file) {
10
32
  const root = globalThis[RETICLE_ROOT_GLOBAL];
11
- if (typeof root !== 'string' || root.length === 0)
33
+ if (typeof root !== 'string' || 0 === root.length)
12
34
  return file;
13
- const prefix = root.endsWith('/') ? root : `${root}/`;
14
- return file.startsWith(prefix) ? file.slice(prefix.length) : file;
35
+ const normalizedRoot = toPosix(root).replace(/\/+$/, '');
36
+ if (0 === normalizedRoot.length)
37
+ return file;
38
+ const normalizedFile = toPosix(file);
39
+ const prefix = `${normalizedRoot}/`;
40
+ const matches = normalizedFile.startsWith(prefix) ||
41
+ normalizedFile.toLowerCase().startsWith(prefix.toLowerCase());
42
+ return matches ? normalizedFile.slice(prefix.length) : file;
15
43
  }
16
44
  const FIBER_PREFIXES = ['__reactFiber$', '__reactInternalInstance$'];
17
45
  const MAX_DEPTH = 200;
@@ -33,22 +61,56 @@ const FRAMEWORK_NOISE = /(\.Provider|\.Consumer)$|^(Fragment|Suspense|StrictMode
33
61
  function isFrameworkNoise(name) {
34
62
  return FRAMEWORK_NOISE.test(name);
35
63
  }
64
+ /**
65
+ * Resolve a fiber to the one in the COMMITTED tree.
66
+ *
67
+ * React keeps two fibers per element — `current` and its `alternate` (the work-in-progress) — and
68
+ * swaps which is which on every commit. The `__reactFiber$…` key on a host DOM node keeps pointing at
69
+ * the fiber object created at mount, so from the second render on it is the previous commit's fiber
70
+ * half the time: hooks read off it alternate correct/stale, one commit behind.
71
+ *
72
+ * The principled test, not a heuristic: climb `return` to the HostRoot, whose `stateNode` is the
73
+ * FiberRoot. The tree we climbed is the committed one iff `fiberRoot.current` is the top fiber we
74
+ * reached. If it is not, the committed counterpart is `fiber.alternate`. Anything unrecognizable
75
+ * (no alternate, no FiberRoot, a detached fiber) returns the fiber unchanged — a possibly-stale read
76
+ * is still better than none, and this must never throw on a React version we have not seen.
77
+ */
78
+ function currentFiber(fiber) {
79
+ const alternate = fiber.alternate;
80
+ if (alternate === undefined || null === alternate)
81
+ return fiber;
82
+ let top = fiber;
83
+ let depth = 0;
84
+ while (top.return !== null && depth < MAX_DEPTH) {
85
+ top = top.return;
86
+ depth += 1;
87
+ }
88
+ const fiberRoot = top.stateNode;
89
+ if (typeof fiberRoot !== 'object' || null === fiberRoot)
90
+ return fiber;
91
+ const committed = fiberRoot.current;
92
+ if (typeof committed !== 'object' || null === committed)
93
+ return fiber;
94
+ return committed === top ? fiber : alternate;
95
+ }
36
96
  function getFiber(el) {
37
97
  const key = Object.keys(el).find((k) => FIBER_PREFIXES.some((p) => k.startsWith(p)));
38
98
  if (key === undefined)
39
99
  return null;
40
100
  const value = el[key];
41
- return (value ?? null);
101
+ if (value === undefined || null === value)
102
+ return null;
103
+ return currentFiber(value);
42
104
  }
43
105
  /** Display name of a component type (function, forwardRef/memo object, or host string). */
44
106
  function componentName(type) {
45
- if (typeof type === 'function') {
107
+ if ('function' === typeof type) {
46
108
  const fn = type;
47
109
  if (fn.displayName !== undefined && fn.displayName.length > 0)
48
110
  return fn.displayName;
49
111
  return fn.name !== undefined && fn.name.length > 0 ? fn.name : null;
50
112
  }
51
- if (typeof type === 'object' && type !== null) {
113
+ if ('object' === typeof type && type !== null) {
52
114
  const obj = type;
53
115
  return obj.displayName !== undefined && obj.displayName.length > 0 ? obj.displayName : null;
54
116
  }
@@ -90,7 +152,7 @@ export function identify(el) {
90
152
  }
91
153
  // Prefer the de-noised stack; fall back to the nearest raw name if filtering left nothing.
92
154
  const componentStack = stack.length > 0 ? stack : rawStack.slice(0, 1);
93
- if (componentStack.length === 0 && source === undefined)
155
+ if (0 === componentStack.length && source === undefined)
94
156
  return null;
95
157
  const info = { componentStack };
96
158
  if (source !== undefined)
@@ -100,10 +162,10 @@ export function identify(el) {
100
162
  function sourceFromAttribute(el) {
101
163
  const stamped = el.closest(`[${DATA_RETICLE_SOURCE_ATTR}]`);
102
164
  const raw = stamped?.getAttribute(DATA_RETICLE_SOURCE_ATTR);
103
- if (raw === null || raw === undefined)
165
+ if (null === raw || raw === undefined)
104
166
  return undefined;
105
167
  const match = /^(.*):(\d+):(\d+)$/.exec(raw);
106
- if (match === null)
168
+ if (null === match)
107
169
  return undefined;
108
170
  const [, file, line, column] = match;
109
171
  if (file === undefined || line === undefined || column === undefined)
@@ -116,7 +178,7 @@ function nearestComponentFiber(el) {
116
178
  let depth = 0;
117
179
  while (fiber !== null && depth < MAX_DEPTH) {
118
180
  depth += 1;
119
- if (typeof fiber.type === 'function' || typeof fiber.elementType === 'function')
181
+ if ('function' === typeof fiber.type || 'function' === typeof fiber.elementType)
120
182
  return fiber;
121
183
  fiber = fiber.return;
122
184
  }
@@ -129,12 +191,12 @@ function nearestComponentFiber(el) {
129
191
  * serializing those un-guarded can cause hangs. Never throws.
130
192
  */
131
193
  function safeValue(value, depth, seen) {
132
- if (value === null)
194
+ if (null === value)
133
195
  return null;
134
196
  const t = typeof value;
135
- if (t === 'string' || t === 'number' || t === 'boolean')
197
+ if ('string' === t || 'number' === t || 'boolean' === t)
136
198
  return value;
137
- if (t === 'undefined' || t === 'function' || t === 'symbol' || t === 'bigint')
199
+ if ('undefined' === t || 'function' === t || 'symbol' === t || 'bigint' === t)
138
200
  return null;
139
201
  if (typeof Node !== 'undefined' && value instanceof Node)
140
202
  return DOM_NODE_MARKER;
@@ -165,27 +227,27 @@ function safeValue(value, depth, seen) {
165
227
  */
166
228
  /** Build a success result, omitting `component` when the name is unknown (exactOptional-safe). */
167
229
  function ok(name, hooks) {
168
- return name === null ? { ok: true, hooks } : { ok: true, component: name, hooks };
230
+ return null === name ? { ok: true, hooks } : { ok: true, component: name, hooks };
169
231
  }
170
232
  export function readState(el) {
171
233
  try {
172
234
  const fiber = nearestComponentFiber(el);
173
- if (fiber === null) {
235
+ if (null === fiber) {
174
236
  return { ok: false, reason: ComponentStateReason.UNAVAILABLE };
175
237
  }
176
238
  const name = componentName(fiber.elementType ?? fiber.type);
177
239
  const head = fiber.memoizedState;
178
- if (typeof head !== 'object' || head === null) {
240
+ if (typeof head !== 'object' || null === head) {
179
241
  return ok(name, []);
180
242
  }
181
243
  const hooks = [];
182
244
  const seen = new WeakSet();
183
245
  let hook = head;
184
246
  let i = 0;
185
- while (typeof hook === 'object' && i < MAX_HOOKS) {
247
+ while ('object' === typeof hook && i < MAX_HOOKS) {
186
248
  hooks.push(safeValue(hook.memoizedState, 0, seen));
187
249
  const next = hook.next;
188
- if (next === null || typeof next !== 'object')
250
+ if (null === next || typeof next !== 'object')
189
251
  break;
190
252
  hook = next;
191
253
  i += 1;
@@ -210,10 +272,10 @@ const HOVER_HANDLER_KEYS = [
210
272
  export function hasHoverHandlers(el) {
211
273
  const fiber = getFiber(el);
212
274
  const props = fiber?.memoizedProps;
213
- if (typeof props !== 'object' || props === null)
275
+ if (typeof props !== 'object' || null === props)
214
276
  return false;
215
277
  const p = props;
216
- return HOVER_HANDLER_KEYS.some((k) => typeof p[k] === 'function');
278
+ return HOVER_HANDLER_KEYS.some((k) => 'function' === typeof p[k]);
217
279
  }
218
280
  import { installRenderMeter } from './render-meter.js';
219
281
  let installed = false;
@@ -38,7 +38,7 @@ function reticleInstance() {
38
38
  // storm is one event of magnitude N, never a per-render flood. requestAnimationFrame when available.
39
39
  const commitStream = createCommitAggregator({
40
40
  schedule: (fn) => {
41
- if (typeof requestAnimationFrame === 'function')
41
+ if ('function' === typeof requestAnimationFrame)
42
42
  requestAnimationFrame(() => fn());
43
43
  else
44
44
  setTimeout(fn, 16);
@@ -110,7 +110,7 @@ export function installRenderMeter() {
110
110
  };
111
111
  }
112
112
  else {
113
- const original = typeof existing.onCommitFiberRoot === 'function'
113
+ const original = 'function' === typeof existing.onCommitFiberRoot
114
114
  ? existing.onCommitFiberRoot.bind(existing)
115
115
  : undefined;
116
116
  existing.onCommitFiberRoot = (...args) => {
package/dist/store.js CHANGED
@@ -29,7 +29,7 @@ export function useReticleStore(name, value) {
29
29
  handle.current ??= pushStore(value);
30
30
  useEffect(() => {
31
31
  const current = handle.current;
32
- if (current === null)
32
+ if (null === current)
33
33
  return;
34
34
  registerStore(name, current.store);
35
35
  return () => unregisterStore(name);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reticlehq/react",
3
- "version": "2.4.0",
3
+ "version": "2.6.0",
4
4
  "description": "Reticle React adapter — maps a DOM ref to its component stack and source file via the fiber tree.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -13,11 +13,12 @@
13
13
  "./store": {
14
14
  "types": "./dist/store.d.ts",
15
15
  "default": "./dist/store.js"
16
- }
16
+ },
17
+ "./package.json": "./package.json"
17
18
  },
18
19
  "dependencies": {
19
- "@reticlehq/browser": "2.4.0",
20
- "@reticlehq/core": "2.4.0"
20
+ "@reticlehq/browser": "2.6.0",
21
+ "@reticlehq/core": "2.6.0"
21
22
  },
22
23
  "peerDependencies": {
23
24
  "react": ">=18"
@@ -60,6 +61,7 @@
60
61
  "build": "tsc -b",
61
62
  "typecheck": "tsc -b",
62
63
  "lint": "eslint src",
63
- "test:unit": "vitest run src --passWithNoTests"
64
+ "test:unit": "vitest run src --passWithNoTests",
65
+ "test:coverage": "vitest run src --passWithNoTests --coverage.enabled --coverage.provider=v8 --coverage.reporter=text-summary --coverage.include='src/**/*.ts' --coverage.exclude='src/**/*.test.ts'"
64
66
  }
65
67
  }