1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

keep track of source map information for require()d coffee files

This commit is contained in:
In-Ho Yi 2013-04-29 17:05:13 +10:00
parent 3650d6eb4e
commit b54db2ea07
2 changed files with 21 additions and 13 deletions

View file

@ -1,6 +1,6 @@
// Generated by CoffeeScript 1.6.2
(function() {
var Lexer, SourceMap, child_process, compile, ext, fork, formatSourcePosition, fs, helpers, lexer, loadFile, parser, patchStackTrace, patched, path, vm, _i, _len, _ref,
var Lexer, SourceMap, child_process, compile, ext, fork, formatSourcePosition, fs, helpers, lexer, loadFile, parser, patchStackTrace, patched, path, sourceMaps, vm, _i, _len, _ref,
__hasProp = {}.hasOwnProperty;
fs = require('fs');
@ -99,7 +99,7 @@
if (!helpers.isCoffee(mainModule.filename) || require.extensions) {
answer = compile(code, options);
patchStackTrace();
mainModule._sourceMaps[mainModule.filename] = answer.sourceMap;
sourceMaps[mainModule.filename] = answer.sourceMap;
return mainModule._compile(answer.js, mainModule.filename);
} else {
return mainModule._compile(code, mainModule.filename);
@ -170,13 +170,16 @@
};
loadFile = function(module, filename) {
var raw, stripped;
var answer, raw, stripped;
raw = fs.readFileSync(filename, 'utf8');
stripped = raw.charCodeAt(0) === 0xFEFF ? raw.substring(1) : raw;
return module._compile(compile(stripped, {
answer = compile(stripped, {
filename: filename,
sourceMap: true,
literate: helpers.isLiterate(filename)
}), filename);
});
sourceMaps[filename] = answer.sourceMap;
return module._compile(answer.js, filename);
};
if (require.extensions) {
@ -241,6 +244,8 @@
patched = false;
sourceMaps = {};
patchStackTrace = function() {
var mainModule;
if (patched) {
@ -248,13 +253,12 @@
}
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];
sourceMap = sourceMaps[filename];
if (sourceMap) {
answer = sourceMap.sourceLocation([line - 1, column - 1]);
}

View file

@ -98,10 +98,10 @@ exports.run = (code, options = {}) ->
# Compile.
if not helpers.isCoffee(mainModule.filename) or require.extensions
answer = compile(code, options)
# Attach sourceMap object to mainModule._sourceMaps[options.filename] so that
# Attach sourceMap object to sourceMaps[options.filename] so that
# it is accessible by Error.prepareStackTrace.
do patchStackTrace
mainModule._sourceMaps[mainModule.filename] = answer.sourceMap
sourceMaps[mainModule.filename] = answer.sourceMap
mainModule._compile answer.js, mainModule.filename
else
mainModule._compile code, mainModule.filename
@ -146,7 +146,9 @@ exports.eval = (code, options = {}) ->
loadFile = (module, filename) ->
raw = fs.readFileSync filename, 'utf8'
stripped = if raw.charCodeAt(0) is 0xFEFF then raw.substring 1 else raw
module._compile compile(stripped, {filename, literate: helpers.isLiterate filename}), filename
answer = compile(stripped, {filename, sourceMap: true, literate: helpers.isLiterate filename})
sourceMaps[filename] = answer.sourceMap
module._compile answer.js, filename
# If the installed version of Node supports `require.extensions`, register
# CoffeeScript as an extension.
@ -206,12 +208,14 @@ parser.yy.parseError = (message, {token}) ->
# positions.
patched = false
# Map of filenames -> sourceMap object.
sourceMaps = {}
patchStackTrace = ->
return if patched
patched = true
mainModule = require.main
# Map of filenames -> sourceMap object.
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
@ -221,7 +225,7 @@ patchStackTrace = ->
sourceFiles = {}
getSourceMapping = (filename, line, column) ->
sourceMap = mainModule._sourceMaps[filename]
sourceMap = sourceMaps[filename]
answer = sourceMap.sourceLocation [line - 1, column - 1] if sourceMap
if answer then [answer[0] + 1, answer[1] + 1] else null