Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Added `quiet` option to suppress some errors #517

Closed
wants to merge 3 commits into from

2 participants

@stagas

Currently suppresses not defined errors but it can be extended for more if needed. Tests included.
Not sure it's a good idea but I said what the heck :P

@ForbesLindesay

Ouch, it's a clever hack, but I don't think it's something we can really include in core. You could always use options.self and refer to self.variable instead of variable you could even put something like - var $ = self; in the first line of your template.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 56 additions and 7 deletions.
  1. +37 −5 lib/jade.js
  2. +4 −1 package.json
  3. +15 −1 test/jade.test.js
View
42 lib/jade.js
@@ -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
5 package.json
@@ -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
16 test/jade.test.js
@@ -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',
Something went wrong with that request. Please try again.