From b54db2ea074c82d407de749c14444db21eeb9379 Mon Sep 17 00:00:00 2001 From: In-Ho Yi Date: Mon, 29 Apr 2013 17:05:13 +1000 Subject: [PATCH] keep track of source map information for require()d coffee files --- lib/coffee-script/coffee-script.js | 18 +++++++++++------- src/coffee-script.coffee | 16 ++++++++++------ 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/lib/coffee-script/coffee-script.js b/lib/coffee-script/coffee-script.js index 637755b6..a3f2d230 100644 --- a/lib/coffee-script/coffee-script.js +++ b/lib/coffee-script/coffee-script.js @@ -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]); } diff --git a/src/coffee-script.coffee b/src/coffee-script.coffee index 591a786e..06fa380d 100644 --- a/src/coffee-script.coffee +++ b/src/coffee-script.coffee @@ -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