Added `quiet` option to suppress some errors #517

Closed
wants to merge 3 commits into
from
View
@@ -134,20 +134,52 @@ function parse(str, options){
exports.compile = function(str, options){
var options = options || {}
+ , quiet = options.quiet
, client = options.client
, filename = options.filename
? JSON.stringify(options.filename)
: 'undefined'
- , fn;
+ , fn
+ , code_pre
+ , code_post;
+
+ if (quiet) {
+ code_pre = [
+ 'var err;'
+ , 'do {'
+ , ' err = undefined;'
+ , ' try {'
+ ].join('\n');
+
+ code_post = [
+ ' } catch (e) {'
+ , ' err = e'
+ , ' if (err.type == \'not_defined\') {'
+ , ' locals[err.arguments[0]] = undefined;'
+ , ' } else {'
+ , ' rethrow(err, __jade[0].filename, __jade[0].lineno);'
+ , ' }'
+ , ' }'
+ , '} while (err && err.type == \'not_defined\');'
+ ].join('\n');
+ } else {
+ code_pre = [
+ 'try {'
+ ].join('\n');
+
+ code_post = [
+ '} catch (err) {'
+ , ' rethrow(err, __jade[0].filename, __jade[0].lineno);'
+ , '}'
+ ].join('\n');
+ }
if (options.compileDebug !== false) {
fn = [
'var __jade = [{ lineno: 1, filename: ' + filename + ' }];'
- , 'try {'
+ , code_pre
, parse(String(str), options)
- , '} catch (err) {'
- , ' rethrow(err, __jade[0].filename, __jade[0].lineno);'
- , '}'
+ , code_post
].join('\n');
} else {
fn = parse(String(str), options);
View
@@ -18,6 +18,9 @@
"uubench": "0.0.1",
"uglify-js": ">= 1.0.7"
},
- "scripts" : { "prepublish" : "npm prune" },
+ "scripts": {
+ "prepublish": "npm prune",
+ "test": "mocha --ui exports --globals name test/jade.test.js"
+ },
"engines": { "node": ">= 0.1.98" }
}
View
@@ -1021,7 +1021,21 @@ module.exports = {
var fn = jade.compile('p foo\np #{bar}', {compileDebug: false, helpers: 'global'});
assert.equal('<p>foo</p><p>baz</p>', fn({bar: 'baz'}));
},
-
+
+ 'test .compile() missing local no quiet': function(){
+ var fn = jade.compile('p= foo\np= bar');
+ try {
+ fn({ foo: 'foo' });
+ } catch (err) {
+ assert.throws(err);
+ }
+ },
+
+ 'test .compile() missing local with quiet': function(){
+ var fn = jade.compile('p= foo\np= bar', {quiet: true});
+ assert.equal('<p>foo</p><p></p>', fn({ foo: 'foo' }));
+ },
+
'test null attrs on tag': function(){
var tag = new jade.nodes.Tag('a'),
name = 'href',