FIX: Ensure 'unless' helper resolves variable name once (#8820)

This fixes a bug which caused '{{#unless var}}' to act the same as
'{{#if true}}' because 'unless' was transforming the conditional value
to 'undefined'.
This commit is contained in:
Dan Ungureanu 2020-01-30 18:41:39 +02:00 committed by GitHub
parent 6d8f12612b
commit e470b27b41
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 26 additions and 1 deletions

View File

@ -47,7 +47,32 @@ export function registerRawHelpers(hbs, handlebarsClass) {
return old.apply(this, [hbs.helpers.get(context, options), options]);
};
}
// HACK: Ensure that the variable is resolved only once.
// The "get" function will be called twice because both `if` and `unless`
// helpers are patched to resolve the variable and `unless` is implemented
// as not `if`. For example, for {{#unless var}} will generate a stack
// trace like:
//
// - patched-unless("var") "var" is resolved to its value, val
// - unless(val) unless is implemented as !if
// - !patched-if(val) val is already resolved, but it is resolved again
// - !if(???) at this point, ??? usually stands for undefined
//
// The following code ensures that patched-unless will call `if` directly,
// `patched-unless("var")` will return `!if(val)`.
const oldIf = hbs.helpers["if"];
hbs.helpers["unless"] = function(context, options) {
return oldIf.apply(this, [
hbs.helpers.get(context, options),
{
fn: options.inverse,
inverse: options.fn,
hash: options.hash
}
]);
};
stringCompatHelper("if");
stringCompatHelper("unless");
stringCompatHelper("with");
}