From de7a92afa703d27cf05f176b2bbd176beb03ea52 Mon Sep 17 00:00:00 2001 From: "Fabio M. Costa" Date: Sun, 29 Dec 2013 19:40:16 -0800 Subject: [PATCH] Updating error message to also show part of the code, making it easier to find the error --- vendor/browser-transforms.js | 38 ++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/vendor/browser-transforms.js b/vendor/browser-transforms.js index 6e9319a14e..701693c34a 100644 --- a/vendor/browser-transforms.js +++ b/vendor/browser-transforms.js @@ -37,6 +37,43 @@ exports.exec = function(code) { var inlineScriptCount = 0; +// This method returns a nicely formated line of code pointing the +// exactly location of the error `e`. +// The line is limited in size so big lines of code are also shown +// in a readable way. +// Example: +// +// ... x', overflow:'scroll'}} id={} onScroll={this.scroll} class=" ... +// ^ +var createSourceCodeErrorMessage = function(code, e) { + var sourceLines = code.split('\n'); + var erroneousLine = sourceLines[e.lineNumber - 1]; + + // Removes any leading indenting spaces and gets the number of + // chars indenting the `erroneousLine` + var indentation = 0; + erroneousLine = erroneousLine.replace(/^\s+/, function(leadingSpaces) { + indentation = leadingSpaces.length; + return ''; + }); + + // Defines the number of characters that are going to show + // before and after the erroneous code + var LIMIT = 30; + var errorColumn = e.column - indentation; + + if (errorColumn > LIMIT) { + erroneousLine = '... ' + erroneousLine.slice(errorColumn - LIMIT); + errorColumn = 4 + LIMIT; + } + if (erroneousLine.length - errorColumn > LIMIT) { + erroneousLine = erroneousLine.slice(0, errorColumn + LIMIT) + ' ...'; + } + var message = '\n\n' + erroneousLine + '\n'; + message += new Array(errorColumn - 1).join(' ') + '^'; + return message; +}; + var transformCode = function(code, source) { var jsx = docblock.parseAsObject(docblock.extract(code)).jsx; @@ -56,6 +93,7 @@ var transformCode = function(code, source) { } else { e.message += location.href; } + e.message += createSourceCodeErrorMessage(code, e); throw e; }