dir = if options.filename
+ dir = if options.filename?
path.dirname fs.realpathSync options.filename
else
fs.realpathSync '.'
@@ -499,11 +554,11 @@ setting __filename
, __dirname
, and relative requ
-
+
Compile.
@@ -518,11 +573,11 @@ setting
__filename
,
__dirname
, and relative
requ
-
+
Compile and evaluate a string of CoffeeScript (in a Node.js-like environment).
The CoffeeScript REPL uses this to run the input.
@@ -552,11 +607,11 @@ The CoffeeScript REPL uses this to run the input.
-
+
define module/require only if they chose not to specify their own
@@ -573,11 +628,11 @@ The CoffeeScript REPL uses this to run the input.
-
+
use the same hack node currently uses for their own REPL
@@ -599,11 +654,11 @@ exports.register =
->
+
Throw error with deprecation warning when depending upon implicit require.extensions
registration
@@ -617,8 +672,22 @@ exports.register =
->
exports._compileFile = (filename, sourceMap = no, inlineMap = no) ->
- raw = fs.readFileSync filename, 'utf8'
- stripped = if raw.charCodeAt(0) is 0xFEFF then raw.substring 1 else raw
+ raw = fs.readFileSync filename, 'utf8'
+
+
+
+
+
+
+
+
+
Strip the Unicode byte order mark, if this file begins with one.
+
+
+
+ stripped = if raw.charCodeAt(0) is 0xFEFF then raw.substring 1 else raw
try
answer = compile stripped, {
@@ -631,11 +700,11 @@ exports._compileFile = (fi
-
+
As the filename and code of a dynamically loaded file will be different
from the original file compiled with CoffeeScript.run, add that
@@ -650,11 +719,11 @@ information to error so it can be pretty-printed later.
-
+
Instantiate a Lexer for our use here.
@@ -665,11 +734,11 @@ information to error so it can be pretty-printed later.
-
+
The real Lexer produces a generic stream of tokens. This object provides a
thin wrapper around it, compatible with the Jison API. We can then pass it
@@ -697,11 +766,11 @@ directly as a “Jison lexer”.
-
+
Make all the AST nodes visible to the parser.
@@ -712,11 +781,11 @@ directly as a “Jison lexer”.
-
+
Override Jison’s default error handling function.
@@ -727,11 +796,11 @@ directly as a “Jison lexer”.
-
+
Disregard Jison’s message, it contains redundant line number information.
Disregard the token, we take its value directly from the lexer in case
@@ -755,11 +824,11 @@ the error is caused by a generated token which might refer to its origin.
-
+
The second argument has a loc
property, which should have the location
data for this token. Unfortunately, Jison seems to send an outdated loc
@@ -772,6 +841,140 @@ from the lexer.
+
+
+
+
+ formatSourcePosition = (frame, getSourceMapping) ->
+ filename = undefined
+ fileLocation = ''
+
+ if frame.isNative()
+ fileLocation = "native"
+ else
+ if frame.isEval()
+ filename = frame.getScriptNameOrSourceURL()
+ fileLocation = "#{frame.getEvalOrigin()}, " unless filename
+ else
+ filename = frame.getFileName()
+
+ filename or= "<anonymous>"
+
+ line = frame.getLineNumber()
+ column = frame.getColumnNumber()
+
+
+
+
+
+
+
+
+
Check for a sourceMap position
+
+
+
+ source = getSourceMapping filename, line, column
+ fileLocation =
+ if source
+ "#{filename}:#{source[0]}:#{source[1]}"
+ else
+ "#{filename}:#{line}:#{column}"
+
+ functionName = frame.getFunctionName()
+ isConstructor = frame.isConstructor()
+ isMethodCall = not (frame.isToplevel() or isConstructor)
+
+ if isMethodCall
+ methodName = frame.getMethodName()
+ typeName = frame.getTypeName()
+
+ if functionName
+ tp = as = ''
+ if typeName and functionName.indexOf typeName
+ tp = "#{typeName}."
+ if methodName and functionName.indexOf(".#{methodName}") isnt functionName.length - methodName.length - 1
+ as = " [as #{methodName}]"
+
+ "#{tp}#{functionName}#{as} (#{fileLocation})"
+ else
+ "#{typeName}.#{methodName or '<anonymous>'} (#{fileLocation})"
+ else if isConstructor
+ "new #{functionName or '<anonymous>'} (#{fileLocation})"
+ else if functionName
+ "#{functionName} (#{fileLocation})"
+ else
+ fileLocation
+
+getSourceMap = (filename) ->
+ if sourceMaps[filename]?
+ sourceMaps[filename]
+
+
+
+
+
+
+
+
+
CoffeeScript compiled in a browser may get compiled with options.filename
+of <anonymous>
, but the browser may request the stack trace with the
+filename of the script file.
+
+
+
+ else if sourceMaps['<anonymous>']?
+ sourceMaps['<anonymous>']
+ else if sources[filename]?
+ answer = compile sources[filename],
+ filename: filename
+ sourceMap: yes
+ answer.sourceMap
+ else
+ null
+
+
+
+
+
+
+
+
+
Based on michaelficarra/CoffeeScriptRedux
+NodeJS / V8 have no support for transforming positions in stack traces using
+sourceMap, so we must monkey-patch Error to display CoffeeScript source
+positions.
+
+
+
+ Error.prepareStackTrace = (err, stack) ->
+ getSourceMapping = (filename, line, column) ->
+ sourceMap = getSourceMap filename
+ answer = sourceMap.sourceLocation [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
+ " at #{formatSourcePosition frame, getSourceMapping}"
+
+ "#{err.toString()}\n#{frames.join '\n'}\n"
+
+
+