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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify helper wrapper #2041

Open
wants to merge 1 commit into
base: 4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions lib/handlebars/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,32 @@ export function moveHelperToHooks(instance, helperName, keepHelper) {
}
}
}

export function mergeHelpers(env, options, container) {
const mergedHelpers = {};
addHelpers(mergedHelpers, env.helpers, container);
addHelpers(mergedHelpers, options.helpers, container);
return mergedHelpers;
}

function addHelpers(mergedHelpers, helpers, container) {
if (!helpers) return;
Object.keys(helpers).forEach(helperName => {
const helper = helpers[helperName];
mergedHelpers[helperName] = passLookupPropertyOption(helper, container);
});
}

function passLookupPropertyOption(helper, container) {
if (typeof helper !== 'function') {
// This should not happen, but apparently it does in https://github.com/wycats/handlebars.js/issues/1639
// We try to make the wrapper least-invasive by not wrapping it, if the helper is not a function.
return helper;
}
const lookupProperty = container.lookupProperty;
return function invokeHelper(/* dynamic arguments */) {
const options = arguments[arguments.length - 1];
options.lookupProperty = lookupProperty;
return helper.apply(this, arguments);
};
}
13 changes: 0 additions & 13 deletions lib/handlebars/internal/wrapHelper.js

This file was deleted.

24 changes: 2 additions & 22 deletions lib/handlebars/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import {
LAST_COMPATIBLE_COMPILER_REVISION,
REVISION_CHANGES
} from './base';
import { moveHelperToHooks } from './helpers';
import { wrapHelper } from './internal/wrapHelper';
import { mergeHelpers, moveHelperToHooks } from './helpers';
import {
createProtoAccessControl,
resultIsAllowed
Expand Down Expand Up @@ -240,10 +239,7 @@ export function template(templateSpec, env) {

ret._setup = function(options) {
if (!options.partial) {
let mergedHelpers = {};
addHelpers(mergedHelpers, env.helpers, container);
addHelpers(mergedHelpers, options.helpers, container);
container.helpers = mergedHelpers;
container.helpers = mergeHelpers(env, options, container);

if (templateSpec.usePartial) {
// Use mergeIfNeeded here to prevent compiling global partials multiple times
Expand Down Expand Up @@ -421,19 +417,3 @@ function executeDecorators(fn, prog, container, depths, data, blockParams) {
}
return prog;
}

function addHelpers(mergedHelpers, helpers, container) {
if (!helpers) return;
Object.keys(helpers).forEach(helperName => {
let helper = helpers[helperName];
mergedHelpers[helperName] = passLookupPropertyOption(helper, container);
});
}

function passLookupPropertyOption(helper, container) {
const lookupProperty = container.lookupProperty;
return wrapHelper(helper, options => {
options.lookupProperty = lookupProperty;
return options;
});
}
Loading