Skip to content

Commit 12a7a6b

Browse files
committed
support eval origins
1 parent c880997 commit 12a7a6b

File tree

1 file changed

+108
-78
lines changed

1 file changed

+108
-78
lines changed

Diff for: index.js

+108-78
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,107 @@ function getSourceMapPosition(position) {
5656
return position;
5757
}
5858

59+
function getEvalOrigin(frame) {
60+
const origin = frame.getEvalOrigin();
61+
if (origin) {
62+
let match = /^eval at ([^(]+) \((.+):(\d+):(\d+)\)$/.exec(origin);
63+
if (match) {
64+
const position = getSourceMapPosition({
65+
source: match[2],
66+
line: Number.parseInt(match[3], 10),
67+
column: Number.parseInt(match[4], 10) - 1,
68+
});
69+
return `eval at ${match[1]} (${position.source}:${position.line}:${position.column + 1})`;
70+
}
71+
match = /^eval at ([^(]+) \((.+)\)$/.exec(origin);
72+
if (match) {
73+
return `eval at ${match[1]} (${getEvalOrigin(match[2])})`;
74+
}
75+
}
76+
return null;
77+
}
78+
79+
function frameToString(frame, position, evalOrigin) {
80+
// JSStackFrame::ToString
81+
const isTopLevel = frame.isToplevel();
82+
const isAsync = frame.isAsync && frame.isAsync();
83+
const isPromiseAll = frame.isPromiseAll && frame.isPromiseAll();
84+
const isConstructor = frame.isConstructor();
85+
const isMethodCall = !(isTopLevel || isConstructor);
86+
const functionName = position.name || frame.getFunctionName();
87+
88+
// AppendFileLocation
89+
const locationInfo = () => {
90+
const fileName = position ? position.source : null;
91+
let out = '';
92+
if (!fileName && frame.isEval()) {
93+
out += `${evalOrigin}, `;
94+
}
95+
if (fileName) {
96+
out += fileName;
97+
} else {
98+
out += '<anonymous>';
99+
}
100+
const lineNumber = position ? position.line : frame.getLineNumber();
101+
if (lineNumber !== -1) {
102+
out += `:${lineNumber}`;
103+
const columnNumber = position ? position.column : frame.getColumnNumber();
104+
if (columnNumber !== -1) {
105+
out += `:${columnNumber}`;
106+
}
107+
}
108+
return out;
109+
};
110+
111+
let string = isAsync ? 'async ' : '';
112+
if (isPromiseAll) {
113+
string += `Promise.all (index ${frame.getPromiseIndex()})`;
114+
return string;
115+
}
116+
if (isMethodCall) {
117+
// AppendMethodCall
118+
const typeName = frame.getTypeName();
119+
const methodName = frame.getMethodName();
120+
if (functionName) {
121+
if (typeName) {
122+
const startsWithTypeName = functionName.startsWith(typeName);
123+
if (!startsWithTypeName) {
124+
string += `${typeName}.`;
125+
}
126+
}
127+
string += functionName;
128+
if (methodName) {
129+
// StringEndsWithMethodName(functionName, methodName);
130+
if (functionName !== methodName && functionName.endsWith(`.${methodName}`)) {
131+
string += ` [as ${methodName}]`;
132+
}
133+
}
134+
} else {
135+
if (typeName) {
136+
string += `${typeName}.`;
137+
}
138+
if (methodName) {
139+
string += methodName;
140+
} else {
141+
string += '<anonymous>';
142+
}
143+
}
144+
} else if (isConstructor) {
145+
string += 'new ';
146+
if (functionName) {
147+
string += functionName;
148+
} else {
149+
string += '<anonymous>';
150+
}
151+
} else if (functionName) {
152+
string += functionName;
153+
} else {
154+
return `${string}${locationInfo()}`;
155+
}
156+
157+
return `${string} (${locationInfo()})`;
158+
}
159+
59160
function getMappedString(frame) {
60161
if (frame.isNative()) {
61162
return frame.toString();
@@ -72,85 +173,14 @@ function getMappedString(frame) {
72173
column: frame.getColumnNumber() - 1,
73174
});
74175
if (position) {
75-
// JSStackFrame::ToString
76-
const isTopLevel = frame.isToplevel();
77-
const isAsync = frame.isAsync && frame.isAsync();
78-
const isPromiseAll = frame.isPromiseAll && frame.isPromiseAll();
79-
const isConstructor = frame.isConstructor();
80-
const isMethodCall = !(isTopLevel || isConstructor);
81-
const functionName = position.name || frame.getFunctionName();
82-
83-
// AppendFileLocation
84-
const locationInfo = () => {
85-
const fileName = position.source;
86-
let out = '';
87-
if (!fileName && frame.isEval()) {
88-
const evalOrigin = frame.getEvalOrigin();
89-
out += `${evalOrigin}, `;
90-
}
91-
if (fileName) {
92-
out += fileName;
93-
} else {
94-
out += '<anonymous>';
95-
}
96-
const lineNumber = position.line;
97-
if (lineNumber !== -1) {
98-
out += `:${lineNumber}`;
99-
const columnNumber = position.column;
100-
if (columnNumber !== -1) {
101-
out += `:${columnNumber}`;
102-
}
103-
}
104-
return out;
105-
};
106-
107-
let string = isAsync ? 'async ' : '';
108-
if (isPromiseAll) {
109-
string += `Promise.all (index ${frame.getPromiseIndex()})`;
110-
return string;
111-
}
112-
if (isMethodCall) {
113-
// AppendMethodCall
114-
const typeName = frame.getTypeName();
115-
const methodName = frame.getMethodName();
116-
if (functionName) {
117-
if (typeName) {
118-
const startsWithTypeName = functionName.startsWith(typeName);
119-
if (!startsWithTypeName) {
120-
string += `${typeName}.`;
121-
}
122-
}
123-
string += functionName;
124-
if (methodName) {
125-
// StringEndsWithMethodName(functionName, methodName);
126-
if (functionName !== methodName && functionName.endsWith(`.${methodName}`)) {
127-
string += ` [as ${methodName}]`;
128-
}
129-
}
130-
} else {
131-
if (typeName) {
132-
string += `${typeName}.`;
133-
}
134-
if (methodName) {
135-
string += methodName;
136-
} else {
137-
string += '<anonymous>';
138-
}
139-
}
140-
} else if (isConstructor) {
141-
string += 'new ';
142-
if (functionName) {
143-
string += functionName;
144-
} else {
145-
string += '<anonymous>';
146-
}
147-
} else if (functionName) {
148-
string += functionName;
149-
} else {
150-
return `${string}${locationInfo()}`;
151-
}
176+
return frameToString(frame, position, null);
177+
}
178+
}
152179

153-
return `${string} (${locationInfo()})`;
180+
if (frame.isEval()) {
181+
const evalOrigin = getEvalOrigin(frame);
182+
if (evalOrigin !== null) {
183+
return frameToString(frame, null, evalOrigin);
154184
}
155185
}
156186

0 commit comments

Comments
 (0)