Updating error message to also show part of the code, making it easier to find the error
Fabio M. Costa committed
Dec 29, 2013 at 19:40 UTC
de7a92afa703d27cf05f176b2bbd176beb03ea52
1 file changed
+38
vendor/browser-transforms.js
+38
@@ -37,6 +37,43 @@ exports.exec = function(code) {
37
38
var inlineScriptCount = 0;
39
40
+// This method returns a nicely formated line of code pointing the
41
+// exactly location of the error `e`.
42
+// The line is limited in size so big lines of code are also shown
43
+// in a readable way.
44
+// Example:
45
+//
46
+// ... x', overflow:'scroll'}} id={} onScroll={this.scroll} class=" ...
47
+// ^
48
+var createSourceCodeErrorMessage = function(code, e) {
49
+ var sourceLines = code.split('\n');
50
+ var erroneousLine = sourceLines[e.lineNumber - 1];
51
+
52
+ // Removes any leading indenting spaces and gets the number of
53
+ // chars indenting the `erroneousLine`
54
+ var indentation = 0;
55
+ erroneousLine = erroneousLine.replace(/^\s+/, function(leadingSpaces) {
56
+ indentation = leadingSpaces.length;
57
+ return '';
58
+ });
59
+
60
+ // Defines the number of characters that are going to show
61
+ // before and after the erroneous code
62
+ var LIMIT = 30;
63
+ var errorColumn = e.column - indentation;
64
+
65
+ if (errorColumn > LIMIT) {
66
+ erroneousLine = '... ' + erroneousLine.slice(errorColumn - LIMIT);
67
+ errorColumn = 4 + LIMIT;
68
+ }
69
+ if (erroneousLine.length - errorColumn > LIMIT) {
70
+ erroneousLine = erroneousLine.slice(0, errorColumn + LIMIT) + ' ...';
71
+ }
72
+ var message = '\n\n' + erroneousLine + '\n';
73
+ message += new Array(errorColumn - 1).join(' ') + '^';
74
+ return message;
75
+};
76
+
77
var transformCode = function(code, source) {
78
var jsx = docblock.parseAsObject(docblock.extract(code)).jsx;
79
@@ -56,6 +93,7 @@ var transformCode = function(code, source) {
93
} else {
94
e.message += location.href;
95
}
96
+ e.message += createSourceCodeErrorMessage(code, e);
97
throw e;
98
}
99