Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add guard to if & unless helpers #1549

Merged
merged 2 commits into from Oct 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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');}
nknapp marked this conversation as resolved.
Show resolved Hide resolved
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
66 changes: 66 additions & 0 deletions spec/helpers.js
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/);
});
});
});