From 624b045ac0b740cd5b546631bbe2b239b9d5a711 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Tue, 17 Jul 2012 01:39:51 -0400 Subject: [PATCH] Add unit tests and code comments for the conditional compilation patch. Former-commit-id: 557aa43dc7c8db738452a9f3afb8ff2aadf8061a --- lodash.js | 15 +++++++++++---- test/test.js | 13 +++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/lodash.js b/lodash.js index a18cb94a2..6cf221460 100644 --- a/lodash.js +++ b/lodash.js @@ -141,10 +141,14 @@ /** Detect if sourceURL syntax is usable without erroring */ try { - // Adobe's and Narwhal's JS engines will error. IE 8/9/etc also evaluate - // @ symbols as part of a non-standard conditional include system, so it - // parses /a/a as a malformed regular expression, so we know to exclude them. - var useSourceURL = (Function('//@/a/a')(), true); + // The JS engine in Adobe products, like InDesign, will throw a syntax error + // when it encounters a single line comment beginning with the `@` symbol. + // The JS engine in Narwhal will generate the function `function anonymous(){//}` + // and throw a syntax error. In IE, `@` symbols are part of its non-standard + // conditional compilation support. The `@cc_on` statement activates its + // support while the trailing `!` induces a syntax error to exlude it. + // See http://msdn.microsoft.com/en-us/library/121hztk3(v=vs.94).aspx + var useSourceURL = (Function('//@cc_on!')(), true); } catch(e){ } /** @@ -3444,6 +3448,9 @@ 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; }; } diff --git a/test/test.js b/test/test.js index 05f40104f..b0e2477a4 100644 --- a/test/test.js +++ b/test/test.js @@ -855,6 +855,19 @@ ok(false); } }); + + test('should not error with IE conditional comments enabled (test with development build)', function() { + var compiled = _.template(''), + pass = true; + + /*@cc_on @*/ + try { + compiled(); + } catch(e) { + pass = false; + } + ok(pass); + }); }()); /*--------------------------------------------------------------------------*/