forked from gjtorikian/function-extractor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample.js
126 lines (115 loc) · 4.19 KB
/
sample.js
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
var CoffeeScript, Module, SourceMapConsumer, formatSourcePosition, fs, patchStackTrace, patched, path, runModule;
fs = require('fs');
path = require('path');
Module = require('module');
CoffeeScript = require('./module');
SourceMapConsumer = require('source-map').SourceMapConsumer;
patched = false;
patchStackTrace = function() {
if (patched) {
return;
}
patched = true;
Module._sourceMaps = {};
return Error.prepareStackTrace = function(err, stack) {
var frame, frames, getSourceMapping, sourceFiles, _ref;
sourceFiles = {};
getSourceMapping = function(filename, line, column) {
var mapString, sourceMap, _base, _ref;
mapString = typeof (_base = Module._sourceMaps)[filename] === "function" ? _base[filename]() : void 0;
if (mapString) {
sourceMap = (_ref = sourceFiles[filename]) != null ? _ref : sourceFiles[filename] = new SourceMapConsumer(mapString);
return sourceMap.originalPositionFor({
line: line,
column: column
});
}
};
frames = (function() {
var _i, _len, _results;
_results = [];
for (_i = 0, _len = stack.length; _i < _len; _i++) {
frame = stack[_i];
if (frame.getFunction() === exports.runMain) {
break;
}
_results.push(" at " + (formatSourcePosition(frame, getSourceMapping)));
}
return _results;
})();
return "" + err.name + ": " + ((_ref = err.message) != null ? _ref : '') + "\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.line + ":" + source.column + ", <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;
}
};
exports.runMain = function(csSource, jsSource, jsAst, filename) {
var mainModule;
mainModule = new Module('.');
mainModule.filename = process.argv[1] = filename;
process.mainModule = mainModule;
Module._cache[mainModule.filename] = mainModule;
mainModule.paths = Module._nodeModulePaths(path.dirname(filename));
return runModule(mainModule, jsSource, jsAst, filename);
};
runModule = function(module, jsSource, jsAst, filename) {
patchStackTrace();
Module._sourceMaps[filename] = function() {
return CoffeeScript.sourceMap(jsAst, filename);
};
return module._compile(jsSource, filename);
};
require.extensions['.coffee'] = function(module, filename) {
var csAst, input, js, jsAst;
input = fs.readFileSync(filename, 'utf8');
csAst = CoffeeScript.parse(input, {
raw: true
});
jsAst = CoffeeScript.compile(csAst);
js = CoffeeScript.js(jsAst);
return runModule(module, js, jsAst, filename);
};