forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapper.js
More file actions
93 lines (87 loc) · 2.75 KB
/
mapper.js
File metadata and controls
93 lines (87 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
// TODO change this
import { getCurrentManager } from 'app/src/sandbox/compile';
// @flow
import StackFrame from './stack-frame';
import { getSourceMap } from './getSourceMap';
import { getLinesAround } from './getLinesAround';
import { settle } from 'settle-promise';
/**
* Enhances a set of <code>StackFrame</code>s with their original positions and code (when available).
* @param {StackFrame[]} frames A set of <code>StackFrame</code>s which contain (generated) code positions.
* @param {number} [contextLines=3] The number of lines to provide before and after the line specified in the <code>StackFrame</code>.
*/
async function map(
frames: StackFrame[],
contextLines: number = 3
): Promise<StackFrame[]> {
const cache: any = {};
const files: string[] = [];
frames.forEach(frame => {
const { fileName } = frame;
if (fileName == null) {
return;
}
if (files.indexOf(fileName) !== -1) {
return;
}
files.push(fileName);
});
await settle(
files.map(async fileName => {
const manager = getCurrentManager();
if (manager != null && !fileName.startsWith('webpack')) {
let transpiledModule;
if (fileName.includes('?')) {
transpiledModule = manager.getTranspiledModuleByHash(
fileName.split('?')[1]
);
} else {
transpiledModule = manager.resolveTranspiledModule(
fileName.replace(location.origin, ''),
'/'
);
}
if (transpiledModule) {
const fileSource =
transpiledModule.source && transpiledModule.source.compiledCode;
const map = await getSourceMap(fileName, fileSource);
cache[fileName] = { fileSource, map };
}
}
})
);
return frames.map(frame => {
const { functionName, fileName, lineNumber, columnNumber } = frame;
let { map, fileSource } = cache[fileName] || {};
if (map == null || lineNumber == null) {
return frame;
}
const { source, line, column } = map.getOriginalPosition(
lineNumber,
columnNumber
);
const originalSource = source == null ? [] : map.getSource(source);
return new StackFrame(
functionName,
fileName,
lineNumber,
columnNumber,
getLinesAround(lineNumber, contextLines, fileSource),
functionName,
source,
line,
column,
getLinesAround(line, contextLines, originalSource)
);
});
}
export { map };
export default map;