Permalink
Browse files

Add better error reporting for match scripts

Use eval sourceURL, so stakc traces have real file names and line numbers, and
add the filename that was being matched when a matcher throws and error.
  • Loading branch information...
1 parent 6cec0d1 commit e334a7ee4ced5558348bcdd75e0c52e80b7be2ae Ryan Patterson committed Feb 25, 2012
Showing with 11 additions and 4 deletions.
  1. +11 −4 lib/jsgrep.js
View
@@ -463,10 +463,12 @@ function tokenString(tt) {
* accidentally creating a closure with the user script.
*
* @param {String} matchScript the contents of the match script
+ * @param {String} filename the path to the evaluated script
* @type Function
*/
-function evalMatchScript(matchScript) {
- return eval("(function custom_matcher(jsgrep) {" + matchScript + "})");
+function evalMatchScript(matchScript, filename) {
+ return eval("(function custom_matcher(jsgrep) {" + matchScript +
+ '\n//@ sourceURL=' + filename + '\n})');
}
/**
@@ -497,17 +499,19 @@ var jsgrep = exports.jsgrep = function(options) {
if (options.matchScript) {
if (typeof options.matchScript === 'string') {
var matchScript = fs.readFileSync(options.matchScript).toString();
+ // We do this to get better syntax errors than eval gives.
var matchAst =
Narcissus.parser.parse(matchScript, options.matchScript, 1);
- matchFn = evalMatchScript(matchScript);
+ matchFn = evalMatchScript(matchScript, options.matchScript);
} else {
matchFn = options.matchScript;
}
try {
matchFn(matcher);
} catch(e) {
throw new JsgrepError('Matcher threw exception')
+ .setSource(ast.tokenizer.filename)
.setCause(e);
}
@@ -551,7 +555,10 @@ var JsgrepError = exports.JsgrepError = createCustomError('JsgrepError', null, {
}
result += this.name + ": " + this.message;
if (this.filename) {
- result += "\n at " + this.filename + ":" + this.lineno;
+ result += "\n at " + this.filename;
+ if (this.lineno) {
+ result += ":" + this.lineno;
+ }
}
if (this.detail) {
result += "\n" + this.detail + "\n";

0 comments on commit e334a7e

Please sign in to comment.