From 4afea4855d91a30e7521edc2fcaedd6bb562965c Mon Sep 17 00:00:00 2001 From: Evan Wallace Date: Sat, 26 Apr 2014 11:15:38 -0700 Subject: [PATCH] fix column numbers --- README.md | 2 +- source-map-support.js | 34 ++++++++++++++++------------------ 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 97c71b6..8c0b022 100644 --- a/README.md +++ b/README.md @@ -76,7 +76,7 @@ compiled.js.map: "file": "compiled.js", "sources": ["original.js"], "names": [], - "mappings": ";;;AAAA,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC" + "mappings": ";;;AAAA,MAAM,IAAI" } Run compiled.js using node (notice how the stack trace uses original.js instead of compiled.js): diff --git a/source-map-support.js b/source-map-support.js index dcb5351..e23f99a 100644 --- a/source-map-support.js +++ b/source-map-support.js @@ -154,13 +154,13 @@ function wrapCallSite(frame) { var position = mapSourcePosition({ source: source, line: frame.getLineNumber(), - column: frame.getColumnNumber() + column: frame.getColumnNumber() - 1 }); return { __proto__: frame, getFileName: function() { return position.source; }, getLineNumber: function() { return position.line; }, - getColumnNumber: function() { return position.column; }, + getColumnNumber: function() { return position.column + 1; }, getScriptNameOrSourceURL: function() { return position.source; } }; } @@ -192,25 +192,23 @@ function prepareStackTrace(error, stack) { } // Mimic node's stack trace printing when an exception escapes the process -function handleUncaughtExceptions(error) { +function handleUncaughtException(error) { if (!error || !error.stack) { console.log('Uncaught exception:', error); process.exit(); } var match = /\n at [^(]+ \((.*):(\d+):(\d+)\)/.exec(error.stack); if (match) { - var position = mapSourcePosition({ - source: match[1], - line: match[2], - column: match[3] - }); - if (fs.existsSync(position.source)) { - var contents = fs.readFileSync(position.source, 'utf8'); - var line = contents.split(/(?:\r\n|\r|\n)/)[position.line - 1]; - if (line) { - console.log('\n' + position.source + ':' + position.line); - console.log(line); - console.log(new Array(+position.column).join(' ') + '^'); + var source = match[1]; + var line = +match[2]; + var column = +match[3]; + if (fs.existsSync(source)) { + var contents = fs.readFileSync(source, 'utf8'); + var code = contents.split(/(?:\r\n|\r|\n)/)[line - 1]; + if (code) { + console.log('\n' + source + ':' + line); + console.log(code); + console.log(new Array(column).join(' ') + '^'); } } } @@ -225,8 +223,8 @@ exports.install = function(options) { // Configure options options = options || {}; - var installHandler = 'handleUncaughtExceptions' in options ? - options.handleUncaughtExceptions : true; + var installHandler = 'handleUncaughtException' in options ? + options.handleUncaughtException : true; emptyCacheBetweenOperations = 'emptyCacheBetweenOperations' in options ? options.emptyCacheBetweenOperations : false; @@ -243,7 +241,7 @@ exports.install = function(options) { // generated JavaScript code will be shown above the stack trace instead of // the original source code. if (installHandler && !isInBrowser()) { - process.on('uncaughtException', handleUncaughtExceptions); + process.on('uncaughtException', handleUncaughtException); } } };