bippy 0.6.1-dev.051a9b2 → 0.6.1-dev.3702582

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 (67) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +32 -48
  3. package/dist/core.cjs +1 -1
  4. package/dist/core.d.cts +45 -60
  5. package/dist/core.d.ts +45 -60
  6. package/dist/core.js +1 -1
  7. package/dist/core2.cjs +9 -0
  8. package/dist/core2.d.cts +11 -3
  9. package/dist/core2.d.ts +11 -3
  10. package/dist/core2.js +9 -0
  11. package/dist/errors.d.cts +410 -0
  12. package/dist/errors.d.ts +410 -0
  13. package/dist/index.cjs +1 -1
  14. package/dist/index.d.cts +11 -3
  15. package/dist/index.d.ts +11 -3
  16. package/dist/index.iife.js +1 -1
  17. package/dist/index.js +1 -1
  18. package/dist/install-hook-only.cjs +1 -1
  19. package/dist/install-hook-only.d.cts +9 -1
  20. package/dist/install-hook-only.d.ts +9 -1
  21. package/dist/install-hook-only.iife.js +1 -1
  22. package/dist/install-hook-only.js +1 -1
  23. package/dist/rdt-hook.cjs +1 -1
  24. package/dist/rdt-hook.js +1 -1
  25. package/dist/source.cjs +14 -5
  26. package/dist/source.d.cts +84 -69
  27. package/dist/source.d.ts +84 -69
  28. package/dist/source.js +14 -5
  29. package/package.json +22 -16
  30. package/src/core.ts +348 -344
  31. package/src/errors.ts +34 -0
  32. package/src/install-hook-only.ts +2 -2
  33. package/src/rdt-hook.ts +160 -161
  34. package/src/react-internals/generated/react-work-tags.ts +318 -0
  35. package/src/react-internals/index.ts +67 -0
  36. package/src/react-internals/semver.ts +80 -0
  37. package/src/react-internals/types.ts +186 -0
  38. package/src/source/constants.ts +1 -1
  39. package/src/source/error-stack.ts +11 -0
  40. package/src/source/get-display-name-from-source.ts +44 -41
  41. package/src/source/get-source.ts +10 -24
  42. package/src/source/index.ts +9 -1
  43. package/src/source/inspect-hooks.ts +220 -207
  44. package/src/source/owner-stack.ts +113 -172
  45. package/src/source/parse-debug-stack.ts +4 -4
  46. package/src/source/parse-hook-names.ts +14 -53
  47. package/src/source/parse-stack.ts +13 -30
  48. package/src/source/renderer-dispatchers.ts +30 -0
  49. package/src/source/symbolication.ts +517 -92
  50. package/dist/get-source.cjs +0 -19
  51. package/dist/get-source.js +0 -19
  52. package/dist/react-refresh.cjs +0 -9
  53. package/dist/react-refresh.d.cts +0 -66
  54. package/dist/react-refresh.d.ts +0 -66
  55. package/dist/react-refresh.js +0 -9
  56. package/dist/unsubscribe.d.cts +0 -298
  57. package/dist/unsubscribe.d.ts +0 -298
  58. package/src/react-refresh/constants.ts +0 -9
  59. package/src/react-refresh/detect-hmr-transport.ts +0 -33
  60. package/src/react-refresh/index.ts +0 -173
  61. package/src/react-refresh/metro-hmr-transport.ts +0 -188
  62. package/src/react-refresh/next-webpack-hmr-transport.ts +0 -72
  63. package/src/react-refresh/normalize-hmr-file-path.ts +0 -24
  64. package/src/react-refresh/types.ts +0 -7
  65. package/src/react-refresh/vite-hmr-transport.ts +0 -116
  66. package/src/types.ts +0 -438
  67. package/src/unsubscribe.ts +0 -17
@@ -1,9 +1,33 @@
1
- import { Fiber } from "../types.js";
1
+ import { Fiber } from "../react-internals/index.js";
2
2
  import { getDisplayName } from "../core.js";
3
- import { getParentStack } from "./owner-stack.js";
4
- import { getSourceFromSourceMap, getSourceMap } from "./symbolication.js";
3
+ import { getRawParentStack } from "./owner-stack.js";
4
+ import {
5
+ getSourceContentFromSourceMap,
6
+ getSourceFromSourceMap,
7
+ getSourceMap,
8
+ type SourceFetch,
9
+ } from "./symbolication.js";
5
10
  import { StackFrame } from "./parse-stack.js";
6
11
 
12
+ const COMPONENT_DECLARATION_PATTERNS = [
13
+ /(?:^|export\s+)(?:const|let|var)\s+(\w+)\s*=/,
14
+ /(?:^|export\s+)function\s+(\w+)/,
15
+ /(?:^|export\s+)class\s+(\w+)/,
16
+ ];
17
+
18
+ const findComponentDeclarationOnLine = (line: string): string | null => {
19
+ for (const pattern of COMPONENT_DECLARATION_PATTERNS) {
20
+ const match = line.match(pattern);
21
+ if (match?.[1]) return match[1];
22
+ }
23
+ return null;
24
+ };
25
+
26
+ const MAX_DECLARATION_LINE_DISTANCE = 5;
27
+
28
+ // the frame points inside the component, so the nearest declaration wins;
29
+ // searching by pattern over the whole window instead would let an adjacent
30
+ // component's declaration shadow the right one
7
31
  const extractComponentNameFromSource = (
8
32
  sourceContent: string,
9
33
  lineNumber: number,
@@ -15,27 +39,16 @@ const extractComponentNameFromSource = (
15
39
  return null;
16
40
  }
17
41
 
18
- const startLine = Math.max(0, targetLineIndex - 5);
19
- const endLine = Math.min(lines.length, targetLineIndex + 5);
20
- const contextLines = lines.slice(startLine, endLine).join("\n");
21
-
22
- const arrowFunctionPattern = /(?:^|export\s+)(?:const|let|var)\s+(\w+)\s*=/m;
23
- const functionPattern = /(?:^|export\s+)function\s+(\w+)/m;
24
- const classPattern = /(?:^|export\s+)class\s+(\w+)/m;
25
-
26
- const arrowMatch = contextLines.match(arrowFunctionPattern);
27
- if (arrowMatch?.[1]) {
28
- return arrowMatch[1];
29
- }
30
-
31
- const functionMatch = contextLines.match(functionPattern);
32
- if (functionMatch?.[1]) {
33
- return functionMatch[1];
34
- }
35
-
36
- const classMatch = contextLines.match(classPattern);
37
- if (classMatch?.[1]) {
38
- return classMatch[1];
42
+ for (let lineDistance = 0; lineDistance <= MAX_DECLARATION_LINE_DISTANCE; lineDistance++) {
43
+ const lineIndexes =
44
+ lineDistance === 0
45
+ ? [targetLineIndex]
46
+ : [targetLineIndex - lineDistance, targetLineIndex + lineDistance];
47
+ for (const lineIndex of lineIndexes) {
48
+ if (lineIndex < 0 || lineIndex >= lines.length) continue;
49
+ const declarationName = findComponentDeclarationOnLine(lines[lineIndex]);
50
+ if (declarationName) return declarationName;
51
+ }
39
52
  }
40
53
 
41
54
  return null;
@@ -44,10 +57,9 @@ const extractComponentNameFromSource = (
44
57
  export const getDisplayNameFromSource = async (
45
58
  fiber: Fiber,
46
59
  cache = true,
47
- fetchFn?: (url: string) => Promise<Response>,
60
+ fetchFn?: SourceFetch,
48
61
  ): Promise<string | null> => {
49
- const parentStackFrames = await getParentStack(fiber, cache, fetchFn);
50
- const stackFrame = parentStackFrames.filter((innerFrame) => innerFrame.fileName)[0];
62
+ const stackFrame = getRawParentStack(fiber).find((innerFrame) => innerFrame.fileName);
51
63
 
52
64
  if (!stackFrame?.fileName) {
53
65
  return getDisplayName(fiber.type);
@@ -73,21 +85,12 @@ export const getDisplayNameFromSource = async (
73
85
  return getDisplayName(fiber.type);
74
86
  }
75
87
 
76
- if (!bundleSourceMap.sourcesContent) {
88
+ const sourceContent = getSourceContentFromSourceMap(bundleSourceMap, source.fileName);
89
+ if (!sourceContent) {
77
90
  return getDisplayName(fiber.type);
78
91
  }
79
92
 
80
- const sourceIndex = bundleSourceMap.sources.indexOf(source.fileName);
81
- if (sourceIndex === -1 || !bundleSourceMap.sourcesContent[sourceIndex]) {
82
- return getDisplayName(fiber.type);
83
- }
84
-
85
- const sourceContent = bundleSourceMap.sourcesContent[sourceIndex];
86
- const extractedName = extractComponentNameFromSource(sourceContent, source.lineNumber);
87
-
88
- if (extractedName) {
89
- return extractedName;
90
- }
91
-
92
- return getDisplayName(fiber.type);
93
+ return (
94
+ extractComponentNameFromSource(sourceContent, source.lineNumber) ?? getDisplayName(fiber.type)
95
+ );
93
96
  };
@@ -1,6 +1,6 @@
1
- import { Fiber } from "../types.js";
1
+ import type { Fiber } from "../react-internals/index.js";
2
2
 
3
- import { FiberSource } from "./types.js";
3
+ import type { FiberSource } from "./types.js";
4
4
  import {
5
5
  SCHEME_REGEX,
6
6
  INTERNAL_SCHEME_PREFIXES,
@@ -13,7 +13,7 @@ import {
13
13
  import { getDefinitionFrameFromOwnedChild, getParentStack, hasDebugStack } from "./owner-stack.js";
14
14
  import { parseDebugStack } from "./parse-debug-stack.js";
15
15
  import { StackFrame } from "./parse-stack.js";
16
- import { symbolicateStack } from "./symbolication.js";
16
+ import { symbolicateStack, type SourceFetch } from "./symbolication.js";
17
17
 
18
18
  export const hasDebugSource = (
19
19
  fiber: Fiber,
@@ -26,10 +26,7 @@ export const hasDebugSource = (
26
26
  }
27
27
  return (
28
28
  typeof debugSource === "object" &&
29
- debugSource !== null &&
30
- "fileName" in debugSource &&
31
29
  typeof debugSource.fileName === "string" &&
32
- "lineNumber" in debugSource &&
33
30
  typeof debugSource.lineNumber === "number"
34
31
  );
35
32
  };
@@ -92,11 +89,10 @@ const getUsageFrameFromDebugStack = (fiber: Fiber): StackFrame | null => {
92
89
  export const getSource = async (
93
90
  fiber: Fiber,
94
91
  cache = true,
95
- fetchFn?: (url: string) => Promise<Response>,
92
+ fetchFn?: SourceFetch,
96
93
  ): Promise<FiberSource | null> => {
97
94
  if (hasDebugSource(fiber)) {
98
- const debugSource = fiber._debugSource;
99
- return debugSource || null;
95
+ return fiber._debugSource;
100
96
  }
101
97
 
102
98
  const debugStackFrame =
@@ -120,11 +116,6 @@ export const getSource = async (
120
116
 
121
117
  const getPathSegmentCount = (path: string): number => path.split("/").filter(Boolean).length;
122
118
 
123
- const getFirstPathSegment = (path: string): string | null => {
124
- const segments = path.split("/").filter(Boolean);
125
- return segments[0] ?? null;
126
- };
127
-
128
119
  const stripSingleBasePathPrefix = (path: string): string => {
129
120
  const firstSlashIndex = path.indexOf("/", 1);
130
121
  if (firstSlashIndex === -1) {
@@ -141,15 +132,12 @@ const stripSingleBasePathPrefix = (path: string): string => {
141
132
  return path;
142
133
  }
143
134
 
144
- if (getPathSegmentCount(remainderPath) < 2) {
145
- return path;
146
- }
147
-
148
- const firstRemainderSegment = getFirstPathSegment(remainderPath);
149
- if (!firstRemainderSegment) {
135
+ const remainderSegments = remainderPath.split("/").filter(Boolean);
136
+ if (remainderSegments.length < 2) {
150
137
  return path;
151
138
  }
152
139
 
140
+ const firstRemainderSegment = remainderSegments[0];
153
141
  if (firstRemainderSegment.startsWith("@")) {
154
142
  return path;
155
143
  }
@@ -171,6 +159,7 @@ export const normalizeFileName = (fileName: string): string => {
171
159
  }
172
160
 
173
161
  let normalizedFileName = fileName;
162
+ const isWindowsDrivePath = /^[a-zA-Z]:[\\/]/.test(normalizedFileName);
174
163
 
175
164
  const isHttpUrl =
176
165
  normalizedFileName.startsWith("http://") || normalizedFileName.startsWith("https://");
@@ -179,9 +168,6 @@ export const normalizeFileName = (fileName: string): string => {
179
168
  const parsedUrl = new URL(normalizedFileName);
180
169
  normalizedFileName = parsedUrl.pathname;
181
170
  } catch {}
182
- }
183
-
184
- if (isHttpUrl) {
185
171
  normalizedFileName = stripSingleBasePathPrefix(normalizedFileName);
186
172
  }
187
173
 
@@ -214,7 +200,7 @@ export const normalizeFileName = (fileName: string): string => {
214
200
  }
215
201
  }
216
202
 
217
- if (SCHEME_REGEX.test(normalizedFileName)) {
203
+ if (!isWindowsDrivePath) {
218
204
  const schemeMatch = normalizedFileName.match(SCHEME_REGEX);
219
205
  if (schemeMatch) {
220
206
  normalizedFileName = normalizedFileName.slice(schemeMatch[0].length);
@@ -6,12 +6,20 @@ export {
6
6
  symbolicateStack,
7
7
  type DecodedSourceMapSection,
8
8
  type IndexSourceMap,
9
- type RawSourceMap,
9
+ type SourceFetch,
10
10
  type SourceMap,
11
+ type SourceMapRequestOptions,
11
12
  type StandardSourceMap,
12
13
  } from "./symbolication.js";
13
14
  export type { FiberSource } from "./types.js";
14
15
  export { parseStack, type ParseOptions, type StackFrame } from "./parse-stack.js";
15
16
  export { getDisplayNameFromSource } from "./get-display-name-from-source.js";
17
+ export {
18
+ BippyError,
19
+ BippyHookInspectionError,
20
+ BippyHookRenderError,
21
+ BippySourceMapError,
22
+ BippyUnsupportedHookError,
23
+ } from "../errors.js";
16
24
  export { getFiberHooks, type HookSource, type HooksNode, type HooksTree } from "./inspect-hooks.js";
17
25
  export { parseHookNames, type HookNames } from "./parse-hook-names.js";