Skip to content

Commit

Permalink
fix: add guard to if & unless helpers (#1549)
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 authored and nknapp committed Oct 27, 2019
1 parent a15d01d commit c76ded8
Show file tree
Hide file tree
Showing 4 changed files with 5,912 additions and 2 deletions.
5 changes: 4 additions & 1 deletion lib/handlebars/helpers/if.js
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
66 changes: 66 additions & 0 deletions spec/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -766,4 +766,70 @@ describe('helpers', function() {
shouldCompileTo('{{#if bar}}{{else goodbyes as |value|}}{{value}}{{/if}}{{value}}', [hash, helpers], '1foo');
});
});

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

template({});
}, 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, /#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, /#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, /#if requires exactly one argument/);
});

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

template({});
}, 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, /#unless requires exactly one argument/);
});

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

template({});
}, 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, /#with requires exactly one argument/);
});
});
});
Loading

0 comments on commit c76ded8

Please sign in to comment.