From 052e3cc8eb55200628c23dba64789724fd50f5fd Mon Sep 17 00:00:00 2001 From: Hao-kang Den Date: Wed, 13 Mar 2013 21:43:29 +0800 Subject: [PATCH 1/2] found a linear shift of lineno, dirty patch for now It seems that js -> coffee line mapping has a linear shifting. Dirty patch by applying linear correction. Dumb, but maybe a hint. --- lib/coffee-script/coffee-script.js | 5 ++++- src/coffee-script.coffee | 17 +++++++++-------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/lib/coffee-script/coffee-script.js b/lib/coffee-script/coffee-script.js index c29ba622..c238a3f2 100644 --- a/lib/coffee-script/coffee-script.js +++ b/lib/coffee-script/coffee-script.js @@ -233,7 +233,10 @@ var answer, sourceMap; sourceMap = mainModule._sourceMaps[filename]; if (sourceMap) { - answer = sourceMap.getSourcePosition([line, column]); + answer = sourceMap.getSourcePosition([line - 1, column]); + } + if (answer) { + answer[0] += 1; } return answer; }; diff --git a/src/coffee-script.coffee b/src/coffee-script.coffee index 7592039d..550603c5 100644 --- a/src/coffee-script.coffee +++ b/src/coffee-script.coffee @@ -193,18 +193,12 @@ parser.yy.parseError = (message, {token}) -> # 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. + # Map of filenames -> sourceMap object. mainModule._sourceMaps = {} # (Assigning to a property of the Module object in the normal module cache is @@ -216,7 +210,14 @@ patchStackTrace = -> getSourceMapping = (filename, line, column) -> sourceMap = mainModule._sourceMaps[filename] - answer = sourceMap.getSourcePosition [line, column] if sourceMap + answer = sourceMap.getSourcePosition [line - 1, column] if sourceMap + if answer + answer[0] += 1 + # # un-comment to dirty patch the column number, not very accurate + # if answer[1] > 3 + # answer[1] -= 3 + # else + # answer[1] = 0 answer frames = for frame in stack From d4254a40a9b809d93e3dc7a8d950ce4f25152b3a Mon Sep 17 00:00:00 2001 From: Hao-kang Den Date: Thu, 14 Mar 2013 08:23:27 +0800 Subject: [PATCH 2/2] Apply modifications by @jwalton --- lib/coffee-script/coffee-script.js | 7 ++++--- src/coffee-script.coffee | 11 ++--------- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/lib/coffee-script/coffee-script.js b/lib/coffee-script/coffee-script.js index c238a3f2..2b5bc335 100644 --- a/lib/coffee-script/coffee-script.js +++ b/lib/coffee-script/coffee-script.js @@ -233,12 +233,13 @@ var answer, sourceMap; sourceMap = mainModule._sourceMaps[filename]; if (sourceMap) { - answer = sourceMap.getSourcePosition([line - 1, column]); + answer = sourceMap.getSourcePosition([line - 1, column - 1]); } if (answer) { - answer[0] += 1; + return [answer[0] + 1, answer[1] + 1]; + } else { + return null; } - return answer; }; frames = (function() { var _j, _len1, _results; diff --git a/src/coffee-script.coffee b/src/coffee-script.coffee index 550603c5..e806bf46 100644 --- a/src/coffee-script.coffee +++ b/src/coffee-script.coffee @@ -210,15 +210,8 @@ patchStackTrace = -> getSourceMapping = (filename, line, column) -> sourceMap = mainModule._sourceMaps[filename] - answer = sourceMap.getSourcePosition [line - 1, column] if sourceMap - if answer - answer[0] += 1 - # # un-comment to dirty patch the column number, not very accurate - # if answer[1] > 3 - # answer[1] -= 3 - # else - # answer[1] = 0 - answer + answer = sourceMap.getSourcePosition [line-1, column-1] if sourceMap + if answer then [answer[0]+1, answer[1]+1] else null frames = for frame in stack break if frame.getFunction() is exports.run