From ffdd79f86bce6d24714cc579c08df325b7b43cb7 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Mon, 3 Sep 2012 16:05:12 -0700 Subject: [PATCH] Adjust how `_.template` handles compiled syntax errors for compatibility with Underscore. Former-commit-id: ba84c5b468938a1be1a1fd0afd31cb83f563e1ca --- lodash.js | 6 ++---- test/test.js | 14 +++++++------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/lodash.js b/lodash.js index c564d7c37..2e5132e0d 100644 --- a/lodash.js +++ b/lodash.js @@ -4066,10 +4066,8 @@ try { result = Function('_', 'return ' + text)(lodash); } catch(e) { - // defer syntax errors until the compiled template is executed to allow - // examining the `source` property beforehand and for consistency, - // because other template related errors occur at execution - result = function() { throw e; }; + e.source = text; + throw e; } if (data) { diff --git a/test/test.js b/test/test.js index 21d78209e..d18b39a97 100644 --- a/test/test.js +++ b/test/test.js @@ -1410,13 +1410,13 @@ deepEqual(options, {}); }); - test('should be debuggable if compiled with errors', function() { - var source = _.template('<% if x %>').source; - ok(source.indexOf('__p') > -1); - }); - - test('should raise an error if a template, compiled with errors, is executed', function() { - raises(_.template('<% if x %>')); + test('should provide the template source when a SyntaxError occurs', function() { + try { + _.template('<% if x %>'); + } catch(e) { + var source = e.source; + } + ok((source + '').indexOf('__p') > -1); }); test('should work with complex "interpolate" delimiters', function() {