bippy 0.7.3-dev.e910a3b → 0.7.3-dev.fbafc40

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.
@@ -28,16 +28,14 @@ export const parseStack = (stackString: string, options?: ParseOptions): StackFr
28
28
  const frames: StackFrame[] = [];
29
29
  for (const rawLine of lines) {
30
30
  if (/^\s*at\s+/.test(rawLine)) {
31
- const parsed = parseV8OrIeString(rawLine)[0];
32
- if (parsed) frames.push(parsed);
31
+ if (CHROME_IE_STACK_REGEXP.test(rawLine)) frames.push(parseV8Line(rawLine));
33
32
  } else if (/^\s*in\s+/.test(rawLine)) {
34
33
  const elementName = rawLine
35
34
  .replace(/^\s*in\s+/, "")
36
35
  .replace(/\s*(?:\(at .*\)|\[[^\]]+\])$/, "");
37
36
  frames.push({ functionName: elementName, source: rawLine });
38
37
  } else if (rawLine.match(FIREFOX_SAFARI_STACK_REGEXP)) {
39
- const parsed = parseFFOrSafariString(rawLine)[0];
40
- if (parsed) frames.push(parsed);
38
+ if (!SAFARI_NATIVE_CODE_REGEXP.test(rawLine)) frames.push(parseSafariLine(rawLine));
41
39
  }
42
40
  }
43
41
  return frames;
@@ -48,6 +46,18 @@ export const parseStack = (stackString: string, options?: ParseOptions): StackFr
48
46
  return parseFFOrSafariString(stackString);
49
47
  };
50
48
 
49
+ const getPositionIndex = (location: string, endIndex: number): number => {
50
+ let positionIndex = endIndex - 1;
51
+ while (positionIndex >= 0) {
52
+ const character = location.charCodeAt(positionIndex);
53
+ if (character < 48 || character > 57) break;
54
+ positionIndex--;
55
+ }
56
+ return positionIndex < endIndex - 1 && location.charCodeAt(positionIndex) === 58
57
+ ? positionIndex
58
+ : -1;
59
+ };
60
+
51
61
  export const extractLocation = (
52
62
  urlLike: string,
53
63
  ): [string, string | undefined, string | undefined] => {
@@ -59,77 +69,115 @@ export const extractLocation = (
59
69
  const isWrappedLocation = urlLike.startsWith("(") && /:\d+\)$/.test(urlLike);
60
70
  const sanitizedResult = isWrappedLocation ? urlLike.slice(1, -1) : urlLike;
61
71
 
62
- const regExp = /(.+?)(?::(\d+))?(?::(\d+))?$/;
63
- const parts = regExp.exec(sanitizedResult);
64
- if (!parts) return [sanitizedResult, undefined, undefined];
65
- return [parts[1], parts[2] || undefined, parts[3] || undefined] as const;
66
- };
72
+ if (/[\n\r\u2028\u2029]/.test(sanitizedResult)) {
73
+ const parts = /(.+?)(?::(\d+))?(?::(\d+))?$/.exec(sanitizedResult);
74
+ return parts
75
+ ? [parts[1], parts[2] || undefined, parts[3] || undefined]
76
+ : [sanitizedResult, undefined, undefined];
77
+ }
67
78
 
68
- export const parseV8OrIeString = (stack: string): StackFrame[] => {
69
- const filteredLines = stack.split("\n").filter((line) => {
70
- return !!line.match(CHROME_IE_STACK_REGEXP);
71
- });
72
-
73
- return filteredLines.map((line): StackFrame => {
74
- let currentLine = line;
75
- if (currentLine.includes("(eval ")) {
76
- currentLine = currentLine
77
- .replace(/eval code/g, "eval")
78
- .replace(/(\(eval at [^()]*)|(,.*$)/g, "");
79
- }
80
- let sanitizedLine = currentLine
81
- .replace(/^\s+/, "")
82
- .replace(/\(eval code/g, "(")
83
- .replace(/^.*?\s+/, "");
79
+ const lastPositionIndex = getPositionIndex(sanitizedResult, sanitizedResult.length);
80
+ if (lastPositionIndex <= 0) return [sanitizedResult, undefined, undefined];
81
+ const previousPositionIndex = getPositionIndex(sanitizedResult, lastPositionIndex);
82
+ if (previousPositionIndex <= 0) {
83
+ return [
84
+ sanitizedResult.slice(0, lastPositionIndex),
85
+ sanitizedResult.slice(lastPositionIndex + 1),
86
+ undefined,
87
+ ];
88
+ }
89
+ return [
90
+ sanitizedResult.slice(0, previousPositionIndex),
91
+ sanitizedResult.slice(previousPositionIndex + 1, lastPositionIndex),
92
+ sanitizedResult.slice(lastPositionIndex + 1),
93
+ ];
94
+ };
84
95
 
85
- const locationMatch = sanitizedLine.match(/ (\(.+\)$)/);
96
+ const parseV8Line = (line: string): StackFrame => {
97
+ let currentLine = line;
98
+ if (currentLine.includes("(eval ")) {
99
+ currentLine = currentLine
100
+ .replace(/eval code/g, "eval")
101
+ .replace(/(\(eval at [^()]*)|(,.*$)/g, "");
102
+ }
103
+ let sanitizedLine = currentLine
104
+ .replace(/^\s+/, "")
105
+ .replace(/\(eval code/g, "(")
106
+ .replace(/^.*?\s+/, "");
107
+
108
+ const locationMatch = sanitizedLine.match(/ (\(.+\)$)/);
109
+
110
+ sanitizedLine = locationMatch ? sanitizedLine.replace(locationMatch[0], "") : sanitizedLine;
111
+
112
+ const locationParts = extractLocation(locationMatch ? locationMatch[1] : sanitizedLine);
113
+ const functionName = (locationMatch && sanitizedLine) || undefined;
114
+ const fileName = ["eval", "<anonymous>", "(native)"].includes(locationParts[0])
115
+ ? undefined
116
+ : locationParts[0];
117
+
118
+ return {
119
+ functionName,
120
+ fileName,
121
+ lineNumber: locationParts[1] ? +locationParts[1] : undefined,
122
+ columnNumber: locationParts[2] ? +locationParts[2] : undefined,
123
+ source: currentLine,
124
+ };
125
+ };
86
126
 
87
- sanitizedLine = locationMatch ? sanitizedLine.replace(locationMatch[0], "") : sanitizedLine;
127
+ const parseSafariLine = (line: string): StackFrame => {
128
+ let currentLine = line;
129
+ if (currentLine.includes(" > eval"))
130
+ currentLine = currentLine.replace(/ line (\d+)(?: > eval line \d+)* > eval:\d+:\d+/g, ":$1");
88
131
 
89
- const locationParts = extractLocation(locationMatch ? locationMatch[1] : sanitizedLine);
90
- const functionName = (locationMatch && sanitizedLine) || undefined;
91
- const fileName = ["eval", "<anonymous>", "(native)"].includes(locationParts[0])
92
- ? undefined
93
- : locationParts[0];
132
+ if (!currentLine.includes("@") && !currentLine.includes(":")) {
133
+ return {
134
+ functionName: currentLine,
135
+ };
136
+ } else {
137
+ const functionNameRegex =
138
+ /(([^\n\r"\u2028\u2029]*".[^\n\r"\u2028\u2029]*"[^\n\r@\u2028\u2029]*(?:@[^\n\r"\u2028\u2029]*"[^\n\r@\u2028\u2029]*)*(?:[\n\r\u2028\u2029][^@]*)?)?[^@]*)@/;
139
+ const matches = currentLine.match(functionNameRegex);
140
+ const functionName = matches && matches[1] ? matches[1] : undefined;
141
+ const locationParts = extractLocation(currentLine.replace(functionNameRegex, ""));
94
142
 
95
143
  return {
96
144
  functionName,
97
- fileName,
145
+ fileName: locationParts[0],
98
146
  lineNumber: locationParts[1] ? +locationParts[1] : undefined,
99
147
  columnNumber: locationParts[2] ? +locationParts[2] : undefined,
100
148
  source: currentLine,
101
149
  };
102
- });
150
+ }
103
151
  };
104
152
 
105
- export const parseFFOrSafariString = (stack: string): StackFrame[] => {
106
- const filteredLines = stack.split("\n").filter((line) => {
107
- return !line.match(SAFARI_NATIVE_CODE_REGEXP);
108
- });
109
-
110
- return filteredLines.map((line): StackFrame => {
111
- let currentLine = line;
112
- if (currentLine.includes(" > eval"))
113
- currentLine = currentLine.replace(/ line (\d+)(?: > eval line \d+)* > eval:\d+:\d+/g, ":$1");
114
-
115
- if (!currentLine.includes("@") && !currentLine.includes(":")) {
116
- return {
117
- functionName: currentLine,
118
- };
119
- } else {
120
- const functionNameRegex =
121
- /(([^\n\r"\u2028\u2029]*".[^\n\r"\u2028\u2029]*"[^\n\r@\u2028\u2029]*(?:@[^\n\r"\u2028\u2029]*"[^\n\r@\u2028\u2029]*)*(?:[\n\r\u2028\u2029][^@]*)?)?[^@]*)@/;
122
- const matches = currentLine.match(functionNameRegex);
123
- const functionName = matches && matches[1] ? matches[1] : undefined;
124
- const locationParts = extractLocation(currentLine.replace(functionNameRegex, ""));
125
-
126
- return {
127
- functionName,
128
- fileName: locationParts[0],
129
- lineNumber: locationParts[1] ? +locationParts[1] : undefined,
130
- columnNumber: locationParts[2] ? +locationParts[2] : undefined,
131
- source: currentLine,
132
- };
153
+ const parseLines = (
154
+ stack: string,
155
+ isV8: boolean,
156
+ cache?: Map<string, StackFrame>,
157
+ ): StackFrame[] => {
158
+ const frames: StackFrame[] = [];
159
+ for (const line of stack.split("\n")) {
160
+ let frame = cache?.get(line);
161
+ if (!frame) {
162
+ if (isV8 ? !CHROME_IE_STACK_REGEXP.test(line) : SAFARI_NATIVE_CODE_REGEXP.test(line))
163
+ continue;
164
+ frame = isV8 ? parseV8Line(line) : parseSafariLine(line);
165
+ cache?.set(line, frame);
133
166
  }
134
- });
167
+ frames.push(frame);
168
+ }
169
+ return frames;
170
+ };
171
+
172
+ export const parseV8OrIeString = (stack: string): StackFrame[] => parseLines(stack, true);
173
+
174
+ export const parseFFOrSafariString = (stack: string): StackFrame[] => parseLines(stack, false);
175
+
176
+ export const createStackParser = () => {
177
+ const v8Frames = new Map<string, StackFrame>();
178
+ const safariFrames = new Map<string, StackFrame>();
179
+ return (stack: string): StackFrame[] => {
180
+ const isV8 = CHROME_IE_STACK_REGEXP.test(stack);
181
+ return parseLines(stack, isV8, isV8 ? v8Frames : safariFrames);
182
+ };
135
183
  };
@@ -1,6 +1,6 @@
1
- import type { RendererDispatcherRef } from "../react-internals/index.js";
2
- import { _renderers, getRDTHook, isRendererMap } from "../rdt-hook.js";
3
- import type { ReactDevToolsTarget } from "../rdt-hook.js";
1
+ import type { RendererDispatcherRef } from "../react-internals/index";
2
+ import { _renderers, getRDTHook, isRendererMap } from "../rdt-hook";
3
+ import type { ReactDevToolsTarget } from "../rdt-hook";
4
4
 
5
5
  export const getRendererDispatcherRefs = (
6
6
  target: ReactDevToolsTarget = globalThis,
@@ -1,8 +1,9 @@
1
1
  import { decode, type SourceMapMappings, type SourceMapSegment } from "@jridgewell/sourcemap-codec";
2
2
 
3
- import { BippySourceMapError } from "../errors.js";
4
- import { SCHEME_REGEX } from "./constants.js";
5
- import type { StackFrame } from "./parse-stack.js";
3
+ import { BippySourceMapError } from "../errors";
4
+ import { SCHEME_REGEX } from "./constants";
5
+ import { normalizeFileName } from "./normalize-file-name";
6
+ import type { StackFrame } from "./parse-stack";
6
7
 
7
8
  export interface DecodedSourceMapSection {
8
9
  map: {
@@ -207,6 +208,13 @@ export const getSourceFromSourceMap = (
207
208
  );
208
209
  };
209
210
 
211
+ const getStringIndex = (values: string[], target: string): number => {
212
+ for (let valueIndex = 0; valueIndex < values.length; valueIndex++) {
213
+ if (values[valueIndex] === target) return valueIndex;
214
+ }
215
+ return -1;
216
+ };
217
+
210
218
  const getSourceFromMappingsByFunctionName = (
211
219
  mappings: SourceMapMappings,
212
220
  sources: string[],
@@ -215,13 +223,17 @@ const getSourceFromMappingsByFunctionName = (
215
223
  ignoredSourceIndices?: Set<number>,
216
224
  ): StackFrame | null => {
217
225
  if (!names) return null;
218
- const functionNameIndex = names.indexOf(functionName);
226
+ const functionNameIndex = getStringIndex(names, functionName);
219
227
  if (functionNameIndex === -1) return null;
220
228
 
221
229
  let ignoredSource: StackFrame | null = null;
222
- for (const lineMapping of mappings) {
223
- for (const segment of lineMapping) {
230
+ for (let lineIndex = 0; lineIndex < mappings.length; lineIndex++) {
231
+ const lineMapping = mappings[lineIndex];
232
+ for (let segmentIndex = 0; segmentIndex < lineMapping.length; segmentIndex++) {
233
+ const segment = lineMapping[segmentIndex];
224
234
  if (segment[4] !== functionNameIndex) continue;
235
+ if (ignoredSource && segment[1] !== undefined && ignoredSourceIndices?.has(segment[1]))
236
+ continue;
225
237
  const source = getSourceFromSegment(segment, sources, ignoredSourceIndices, names);
226
238
  if (!source) continue;
227
239
  if (!source.isIgnoreListed) return source;
@@ -261,37 +273,40 @@ export const getSourceFromSourceMapByFunctionName = (
261
273
  );
262
274
  };
263
275
 
264
- const findSourceContentByFileName = (
265
- sources: string[],
266
- sourcesContent: Array<string | null> | undefined,
267
- fileName: string,
268
- ): string | null => {
269
- if (!sourcesContent) return null;
270
- const sourceIndex = sources.indexOf(fileName);
271
- return sourceIndex === -1 ? null : (sourcesContent[sourceIndex] ?? null);
276
+ const canonicalSourcePath = (fileName: string): string => {
277
+ const normalizedFileName = normalizeFileName(fileName);
278
+ return normalizedFileName.startsWith("./") ? normalizedFileName.slice(2) : normalizedFileName;
272
279
  };
273
280
 
274
281
  export const getSourceContentFromSourceMap = (
275
282
  sourceMap: SourceMap,
276
283
  originalFileName: string,
277
284
  ): string | null => {
278
- const sourceContent = findSourceContentByFileName(
279
- sourceMap.sources,
280
- sourceMap.sourcesContent,
281
- originalFileName,
282
- );
283
- if (sourceContent !== null) return sourceContent;
284
-
285
- if (!sourceMap.sections) return null;
286
- for (const section of sourceMap.sections) {
287
- const sectionSourceContent = findSourceContentByFileName(
288
- section.map.sources,
289
- section.map.sourcesContent,
290
- originalFileName,
291
- );
292
- if (sectionSourceContent !== null) return sectionSourceContent;
285
+ const maps = [sourceMap, ...(sourceMap.sections?.map((section) => section.map) ?? [])];
286
+ let hasExactSource = false;
287
+ for (const map of maps) {
288
+ const sourceIndex = getStringIndex(map.sources, originalFileName);
289
+ if (sourceIndex === -1) continue;
290
+ hasExactSource = true;
291
+ const sourceContent = map.sourcesContent?.[sourceIndex];
292
+ if (typeof sourceContent === "string") return sourceContent;
293
+ }
294
+ if (hasExactSource) return null;
295
+
296
+ const canonicalFileName = canonicalSourcePath(originalFileName);
297
+ if (!canonicalFileName) return null;
298
+ let matchedFileName: string | undefined;
299
+ let matchedContent: string | null = null;
300
+ for (const map of maps) {
301
+ for (let index = 0; index < map.sources.length; index++) {
302
+ const fileName = map.sources[index];
303
+ if (!fileName || canonicalSourcePath(fileName) !== canonicalFileName) continue;
304
+ if (matchedFileName !== undefined && matchedFileName !== fileName) return null;
305
+ matchedFileName = fileName;
306
+ matchedContent ??= map.sourcesContent?.[index] ?? null;
307
+ }
293
308
  }
294
- return null;
309
+ return matchedContent;
295
310
  };
296
311
 
297
312
  const resolveUrl = (reference: string, baseUrl: string): string | null => {
@@ -434,12 +449,46 @@ const getIgnoredSourceIndices = (rawSourceMap: StandardSourceMap): Set<number> |
434
449
  return ignoreList?.length ? new Set(ignoreList) : undefined;
435
450
  };
436
451
 
452
+ const isAbsoluteSourcePath = (source: string): boolean =>
453
+ source.startsWith("/") || /^[a-zA-Z]:[\\/]/.test(source) || SCHEME_REGEX.test(source);
454
+
455
+ const resolveRelativeSource = (source: string, sourceMapUrl: string): string => {
456
+ if (!source.startsWith("./") && !source.startsWith("../")) return source;
457
+ if (INLINE_SOURCEMAP_REGEX.test(sourceMapUrl)) return source;
458
+ return resolveUrl(source, sourceMapUrl) ?? source;
459
+ };
460
+
461
+ const normalizeJoinedSourcePath = (joinedSource: string): string => {
462
+ const pathSegments = joinedSource.split("/");
463
+ const normalizedPathSegments: string[] = [];
464
+ for (const pathSegment of pathSegments) {
465
+ if (pathSegment === "" || pathSegment === ".") {
466
+ if (pathSegment === "" && normalizedPathSegments.length === 0) {
467
+ normalizedPathSegments.push("");
468
+ }
469
+ continue;
470
+ }
471
+ if (pathSegment === "..") {
472
+ const previousSegment = normalizedPathSegments[normalizedPathSegments.length - 1];
473
+ if (previousSegment && previousSegment !== ".." && previousSegment !== "") {
474
+ normalizedPathSegments.pop();
475
+ } else {
476
+ normalizedPathSegments.push("..");
477
+ }
478
+ continue;
479
+ }
480
+ normalizedPathSegments.push(pathSegment);
481
+ }
482
+ return normalizedPathSegments.join("/");
483
+ };
484
+
437
485
  const resolveSourceRoot = (
438
486
  sourceRoot: string | undefined,
439
487
  source: string,
440
488
  sourceMapUrl: string,
441
489
  ): string => {
442
- if (!sourceRoot || SCHEME_REGEX.test(source) || source.startsWith("/")) return source;
490
+ if (isAbsoluteSourcePath(source)) return source;
491
+ if (!sourceRoot) return resolveRelativeSource(source, sourceMapUrl);
443
492
  const normalizedSourceRoot = sourceRoot.endsWith("/") ? sourceRoot : `${sourceRoot}/`;
444
493
  const normalizedSource = source.replace(/^\.\//, "");
445
494
  try {
@@ -448,16 +497,7 @@ const resolveSourceRoot = (
448
497
  : new URL(normalizedSourceRoot, sourceMapUrl).toString();
449
498
  return new URL(normalizedSource, baseUrl).toString();
450
499
  } catch {
451
- const pathSegments = `${normalizedSourceRoot}${normalizedSource}`.split("/");
452
- const normalizedPathSegments: string[] = [];
453
- for (const pathSegment of pathSegments) {
454
- if (pathSegment === "..") {
455
- normalizedPathSegments.pop();
456
- } else if (pathSegment !== ".") {
457
- normalizedPathSegments.push(pathSegment);
458
- }
459
- }
460
- return normalizedPathSegments.join("/");
500
+ return normalizeJoinedSourcePath(`${normalizedSourceRoot}${normalizedSource}`);
461
501
  }
462
502
  };
463
503