mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
port patchStackTrace from Redux
This commit is contained in:
parent
20d98c7106
commit
b7f0653a0e
2 changed files with 191 additions and 6 deletions
|
@ -1,6 +1,6 @@
|
|||
// Generated by CoffeeScript 1.6.1
|
||||
(function() {
|
||||
var Lexer, compile, ext, fs, helpers, lexer, loadFile, parser, path, sourcemap, vm, _i, _len, _ref,
|
||||
var Lexer, compile, ext, formatSourcePosition, fs, helpers, lexer, loadFile, parser, patchStackTrace, patched, path, sourcemap, vm, _i, _len, _ref,
|
||||
__hasProp = {}.hasOwnProperty;
|
||||
|
||||
fs = require('fs');
|
||||
|
@ -100,16 +100,20 @@
|
|||
};
|
||||
|
||||
exports.run = function(code, options) {
|
||||
var mainModule;
|
||||
var answer, mainModule;
|
||||
if (options == null) {
|
||||
options = {};
|
||||
}
|
||||
mainModule = require.main;
|
||||
options.sourceMap = true;
|
||||
mainModule.filename = process.argv[1] = options.filename ? fs.realpathSync(options.filename) : '.';
|
||||
mainModule.moduleCache && (mainModule.moduleCache = {});
|
||||
mainModule.paths = require('module')._nodeModulePaths(path.dirname(fs.realpathSync(options.filename)));
|
||||
if (!helpers.isCoffee(mainModule.filename) || require.extensions) {
|
||||
return mainModule._compile(compile(code, options), mainModule.filename);
|
||||
answer = compile(code, options);
|
||||
patchStackTrace();
|
||||
mainModule._sourceMaps[mainModule.filename] = answer.sourceMap;
|
||||
return mainModule._compile(answer.js, mainModule.filename);
|
||||
} else {
|
||||
return mainModule._compile(code, mainModule.filename);
|
||||
}
|
||||
|
@ -210,4 +214,89 @@
|
|||
return helpers.throwSyntaxError(message, parser.lexer.yylloc);
|
||||
};
|
||||
|
||||
patched = false;
|
||||
|
||||
patchStackTrace = function() {
|
||||
var mainModule;
|
||||
if (patched) {
|
||||
return;
|
||||
}
|
||||
patched = true;
|
||||
mainModule = require.main;
|
||||
mainModule._sourceMaps = {};
|
||||
return Error.prepareStackTrace = function(err, stack) {
|
||||
var frame, frames, getSourceMapping, sourceFiles, _ref1;
|
||||
sourceFiles = {};
|
||||
getSourceMapping = function(filename, line, column) {
|
||||
var answer, sourceMap;
|
||||
sourceMap = mainModule._sourceMaps[filename];
|
||||
if (sourceMap) {
|
||||
answer = sourceMap.getSourcePosition([line, column]);
|
||||
}
|
||||
return answer;
|
||||
};
|
||||
frames = (function() {
|
||||
var _j, _len1, _results;
|
||||
_results = [];
|
||||
for (_j = 0, _len1 = stack.length; _j < _len1; _j++) {
|
||||
frame = stack[_j];
|
||||
if (frame.getFunction() === exports.run) {
|
||||
break;
|
||||
}
|
||||
_results.push(" at " + (formatSourcePosition(frame, getSourceMapping)));
|
||||
}
|
||||
return _results;
|
||||
})();
|
||||
return "" + err.name + ": " + ((_ref1 = err.message) != null ? _ref1 : '') + "\n" + (frames.join('\n')) + "\n";
|
||||
};
|
||||
};
|
||||
|
||||
formatSourcePosition = function(frame, getSourceMapping) {
|
||||
var as, column, fileLocation, fileName, functionName, isConstructor, isMethodCall, line, methodName, source, tp, typeName;
|
||||
fileName = void 0;
|
||||
fileLocation = '';
|
||||
if (frame.isNative()) {
|
||||
fileLocation = "native";
|
||||
} else {
|
||||
if (frame.isEval()) {
|
||||
fileName = frame.getScriptNameOrSourceURL();
|
||||
if (!fileName) {
|
||||
fileLocation = "" + (frame.getEvalOrigin()) + ", ";
|
||||
}
|
||||
} else {
|
||||
fileName = frame.getFileName();
|
||||
}
|
||||
fileName || (fileName = "<anonymous>");
|
||||
line = frame.getLineNumber();
|
||||
column = frame.getColumnNumber();
|
||||
source = getSourceMapping(fileName, line, column);
|
||||
fileLocation = source ? "" + fileName + ":" + source[0] + ":" + source[1] + ", <js>:" + line + ":" + column : "" + fileName + ":" + line + ":" + column;
|
||||
}
|
||||
functionName = frame.getFunctionName();
|
||||
isConstructor = frame.isConstructor();
|
||||
isMethodCall = !(frame.isToplevel() || isConstructor);
|
||||
if (isMethodCall) {
|
||||
methodName = frame.getMethodName();
|
||||
typeName = frame.getTypeName();
|
||||
if (functionName) {
|
||||
tp = as = '';
|
||||
if (typeName && functionName.indexOf(typeName)) {
|
||||
tp = "" + typeName + ".";
|
||||
}
|
||||
if (methodName && functionName.indexOf("." + methodName) !== functionName.length - methodName.length - 1) {
|
||||
as = " [as " + methodName + "]";
|
||||
}
|
||||
return "" + tp + functionName + as + " (" + fileLocation + ")";
|
||||
} else {
|
||||
return "" + typeName + "." + (methodName || '<anonymous>') + " (" + fileLocation + ")";
|
||||
}
|
||||
} else if (isConstructor) {
|
||||
return "new " + (functionName || '<anonymous>') + " (" + fileLocation + ")";
|
||||
} else if (functionName) {
|
||||
return "" + functionName + " (" + fileLocation + ")";
|
||||
} else {
|
||||
return fileLocation;
|
||||
}
|
||||
};
|
||||
|
||||
}).call(this);
|
||||
|
|
|
@ -96,7 +96,7 @@ exports.nodes = (source, options) ->
|
|||
# setting `__filename`, `__dirname`, and relative `require()`.
|
||||
exports.run = (code, options = {}) ->
|
||||
mainModule = require.main
|
||||
|
||||
options.sourceMap = true
|
||||
# Set the filename.
|
||||
mainModule.filename = process.argv[1] =
|
||||
if options.filename then fs.realpathSync(options.filename) else '.'
|
||||
|
@ -109,7 +109,12 @@ exports.run = (code, options = {}) ->
|
|||
|
||||
# Compile.
|
||||
if not helpers.isCoffee(mainModule.filename) or require.extensions
|
||||
mainModule._compile compile(code, options), mainModule.filename
|
||||
answer = compile(code, options)
|
||||
# Attach sourceMap object to mainModule._sourceMaps[options.filename] so that
|
||||
# it is accessible by Error.prepareStackTrace.
|
||||
do patchStackTrace
|
||||
mainModule._sourceMaps[mainModule.filename] = answer.sourceMap
|
||||
mainModule._compile answer.js, mainModule.filename
|
||||
else
|
||||
mainModule._compile code, mainModule.filename
|
||||
|
||||
|
@ -181,4 +186,95 @@ parser.yy.parseError = (message, {token}) ->
|
|||
# data for this token. Unfortunately, Jison seems to send an outdated `loc`
|
||||
# (from the previous token), so we take the location information directly
|
||||
# from the lexer.
|
||||
helpers.throwSyntaxError message, parser.lexer.yylloc
|
||||
helpers.throwSyntaxError message, parser.lexer.yylloc
|
||||
|
||||
# Based on [michaelficarra/CoffeeScriptRedux](http://goo.gl/ZTx1p)
|
||||
# NodeJS / V8 have no support for transforming positions in stack traces using
|
||||
# sourceMap, so we must monkey-patch Error to display CoffeeScript source
|
||||
# positions.
|
||||
|
||||
# Ideally, this would happen in a way that is scalable to multiple compile-to-
|
||||
# JS languages trying to do the same thing in the same NodeJS process. We can
|
||||
# implement it as if there were an API, and then patch in support for that
|
||||
# API. The following maybe should be in its own npm module that multiple
|
||||
# compilers can include.
|
||||
|
||||
patched = false
|
||||
patchStackTrace = ->
|
||||
return if patched
|
||||
patched = true
|
||||
mainModule = require.main
|
||||
# Map of filenames -> functions that return a sourceMap string.
|
||||
mainModule._sourceMaps = {}
|
||||
|
||||
# (Assigning to a property of the Module object in the normal module cache is
|
||||
# unsuitable, because node deletes those objects from the cache if an
|
||||
# exception is thrown in the module body.)
|
||||
|
||||
Error.prepareStackTrace = (err, stack) ->
|
||||
sourceFiles = {}
|
||||
|
||||
getSourceMapping = (filename, line, column) ->
|
||||
sourceMap = mainModule._sourceMaps[filename]
|
||||
answer = sourceMap.getSourcePosition [line, column] if sourceMap
|
||||
answer
|
||||
|
||||
frames = for frame in stack
|
||||
break if frame.getFunction() is exports.run
|
||||
" at #{formatSourcePosition frame, getSourceMapping}"
|
||||
|
||||
"#{err.name}: #{err.message ? ''}\n#{frames.join '\n'}\n"
|
||||
|
||||
# Based on http://v8.googlecode.com/svn/branches/bleeding_edge/src/messages.js
|
||||
# Modified to handle sourceMap
|
||||
formatSourcePosition = (frame, getSourceMapping) ->
|
||||
fileName = undefined
|
||||
fileLocation = ''
|
||||
|
||||
if frame.isNative()
|
||||
fileLocation = "native"
|
||||
else
|
||||
if frame.isEval()
|
||||
fileName = frame.getScriptNameOrSourceURL()
|
||||
fileLocation = "#{frame.getEvalOrigin()}, " unless fileName
|
||||
else
|
||||
fileName = frame.getFileName()
|
||||
|
||||
fileName or= "<anonymous>"
|
||||
|
||||
line = frame.getLineNumber()
|
||||
column = frame.getColumnNumber()
|
||||
|
||||
# Check for a sourceMap position
|
||||
source = getSourceMapping fileName, line, column
|
||||
fileLocation =
|
||||
if source
|
||||
"#{fileName}:#{source[0]}:#{source[1]}, <js>:#{line}:#{column}"
|
||||
else
|
||||
"#{fileName}:#{line}:#{column}"
|
||||
|
||||
|
||||
functionName = frame.getFunctionName()
|
||||
isConstructor = frame.isConstructor()
|
||||
isMethodCall = not (frame.isToplevel() or isConstructor)
|
||||
|
||||
if isMethodCall
|
||||
methodName = frame.getMethodName()
|
||||
typeName = frame.getTypeName()
|
||||
|
||||
if functionName
|
||||
tp = as = ''
|
||||
if typeName and functionName.indexOf typeName
|
||||
tp = "#{typeName}."
|
||||
if methodName and functionName.indexOf(".#{methodName}") isnt functionName.length - methodName.length - 1
|
||||
as = " [as #{methodName}]"
|
||||
|
||||
"#{tp}#{functionName}#{as} (#{fileLocation})"
|
||||
else
|
||||
"#{typeName}.#{methodName or '<anonymous>'} (#{fileLocation})"
|
||||
else if isConstructor
|
||||
"new #{functionName or '<anonymous>'} (#{fileLocation})"
|
||||
else if functionName
|
||||
"#{functionName} (#{fileLocation})"
|
||||
else
|
||||
fileLocation
|
Loading…
Reference in a new issue