Skip to content

Commit afba3fc

Browse files
committed
Browser sort is not stable
1 parent 1ede6a8 commit afba3fc

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

packages/react-error-overlay/src/utils/unmapper.js

+21-20
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ import { getSourceMap } from './getSourceMap';
44
import { getLinesAround } from './getLinesAround';
55
import path from 'path';
66

7+
function count(search: string, string: string): number {
8+
let count = -1, index = 0;
9+
for (; index !== -1; count++, (index = string.indexOf(search, index + 1)));
10+
return count;
11+
}
12+
713
/**
814
* Turns a set of mapped <code>StackFrame</code>s back into their generated code position and enhances them with code.
915
* @param {string} fileUri The URI of the <code>bundle.js</code> file.
@@ -39,28 +45,22 @@ async function unmap(
3945
return frame;
4046
}
4147
const fN: string = fileName;
42-
const splitCache1: any = {}, splitCache2: any = {}, splitCache3: any = {};
4348
const source = map
4449
.getSources()
45-
.map(s => s.replace(/[\\]+/g, '/'))
46-
.filter(s => {
47-
s = path.normalize(s);
48-
return s.indexOf(fN) === s.length - fN.length;
49-
})
50-
.sort((a, b) => {
51-
let a2 = splitCache1[a] || (splitCache1[a] = a.split(path.sep)),
52-
b2 = splitCache1[b] || (splitCache1[b] = b.split(path.sep));
53-
return Math.sign(a2.length - b2.length);
54-
})
55-
.sort((a, b) => {
56-
let a2 = splitCache2[a] || (splitCache2[a] = a.split('node_modules')),
57-
b2 = splitCache2[b] || (splitCache2[b] = b.split('node_modules'));
58-
return Math.sign(a2.length - b2.length);
50+
.map(s => path.normalize(s.replace(/[\\]+/g, '/')))
51+
.filter(p => {
52+
const i = p.lastIndexOf(fN);
53+
return i !== -1 && i === p.length - fN.length;
5954
})
55+
.map(p => ({
56+
token: p,
57+
seps: count(path.sep, p),
58+
penalties: count('node_modules', p) + count('~', p),
59+
}))
6060
.sort((a, b) => {
61-
let a2 = splitCache3[a] || (splitCache3[a] = a.split('~')),
62-
b2 = splitCache3[b] || (splitCache3[b] = b.split('~'));
63-
return Math.sign(a2.length - b2.length);
61+
const s = Math.sign(a.seps - b.seps);
62+
if (s !== 0) return s;
63+
return Math.sign(a.penalties - b.penalties);
6464
});
6565
if (source.length < 1 || lineNumber == null) {
6666
return new StackFrame(
@@ -76,13 +76,14 @@ async function unmap(
7676
null
7777
);
7878
}
79+
const sourceT = source[0].token;
7980
const { line, column } = map.getGeneratedPosition(
80-
source[0],
81+
sourceT,
8182
lineNumber,
8283
// $FlowFixMe
8384
columnNumber
8485
);
85-
const originalSource = map.getSource(source[0]);
86+
const originalSource = map.getSource(sourceT);
8687
return new StackFrame(
8788
functionName,
8889
fileUri,

0 commit comments

Comments
 (0)