Skip to content

Commit

Permalink
fix: add guard to if & unless helpers
Browse files Browse the repository at this point in the history
fixes #1548

- add a guard to show readable syntax error for if / unless helper
- prevents against 3 different errors that can be generated by different systax errors
  • Loading branch information
ErisDS committed Oct 25, 2019
1 parent 739addf commit cd83e2f
Show file tree
Hide file tree
Showing 4 changed files with 5,855 additions and 11 deletions.
5 changes: 4 additions & 1 deletion lib/handlebars/helpers/if.js
@@ -1,7 +1,9 @@
import {isEmpty, isFunction} from '../utils';
import { isEmpty, isFunction } from '../utils';
import Exception from '../exception';

export default function(instance) {
instance.registerHelper('if', function(conditional, options) {
if (arguments.length != 2) { throw new Exception('#if requires exactly one argument');}
if (isFunction(conditional)) { conditional = conditional.call(this); }

// Default behavior is to render the positive path if the value is truthy and not empty.
Expand All @@ -15,6 +17,7 @@ export default function(instance) {
});

instance.registerHelper('unless', function(conditional, options) {
if (arguments.length != 2) { throw new Exception('#unless requires exactly one argument');}
return instance.helpers['if'].call(this, conditional, {fn: options.inverse, inverse: options.fn, hash: options.hash});
});
}
4 changes: 3 additions & 1 deletion lib/handlebars/helpers/with.js
@@ -1,7 +1,9 @@
import {appendContextPath, blockParams, createFrame, isEmpty, isFunction} from '../utils';
import { appendContextPath, blockParams, createFrame, isEmpty, isFunction } from '../utils';
import Exception from '../exception';

export default function(instance) {
instance.registerHelper('with', function(context, options) {
if (arguments.length != 2) { throw new Exception('#with requires exactly one argument');}
if (isFunction(context)) { context = context.call(this); }

let fn = options.fn;
Expand Down
18 changes: 9 additions & 9 deletions spec/helpers.js
Expand Up @@ -767,69 +767,69 @@ describe('helpers', function() {
});
});

describe.only('built-in helpers malformed arguments ', function() {
describe('built-in helpers malformed arguments ', function() {
it('if helper - too few arguments', function() {
var template = CompilerContext.compile('{{#if}}{{/if}}');
shouldThrow(function() {

template({});
}, undefined, /Cannot read property 'hash' of undefined/);
}, undefined, /#if requires exactly one argument/);
});

it('if helper - too many arguments, string', function() {
var template = CompilerContext.compile('{{#if test "string"}}{{/if}}');
shouldThrow(function() {

template({});
}, undefined, /Cannot read property 'includeZero' of undefined/);
}, undefined, /#if requires exactly one argument/);
});

it('if helper - too many arguments, undefined', function() {
var template = CompilerContext.compile('{{#if test undefined}}{{/if}}');
shouldThrow(function() {

template({});
}, undefined, /Cannot read property 'hash' of undefined/);
}, undefined, /#if requires exactly one argument/);
});

it('if helper - too many arguments, null', function() {
var template = CompilerContext.compile('{{#if test null}}{{/if}}');
shouldThrow(function() {

template({});
}, undefined, /Cannot read property 'hash' of null/);
}, undefined, /#if requires exactly one argument/);
});

it('unless helper - too few arguments', function() {
var template = CompilerContext.compile('{{#unless}}{{/unless}}');
shouldThrow(function() {

template({});
}, undefined, /Cannot read property 'inverse' of undefined/);
}, undefined, /#unless requires exactly one argument/);
});

it('unless helper - too many arguments', function() {
var template = CompilerContext.compile('{{#unless test null}}{{/unless}}');
shouldThrow(function() {

template({});
}, undefined, /Cannot read property 'inverse' of null/);
}, undefined, /#unless requires exactly one argument/);
});

it('with helper - too few arguments', function() {
var template = CompilerContext.compile('{{#with}}{{/with}}');
shouldThrow(function() {

template({});
}, undefined, /Cannot read property 'fn' of undefined/);
}, undefined, /#with requires exactly one argument/);
});

it('with helper - too many arguments', function() {
var template = CompilerContext.compile('{{#with test "string"}}{{/with}}');
shouldThrow(function() {

template({});
}, undefined, /options.inverse is not a function/);
}, undefined, /#with requires exactly one argument/);
});
});
});

0 comments on commit cd83e2f

Please sign in to comment.