Skip to content

Commit

Permalink
Simplify helper wrapper
Browse files Browse the repository at this point in the history
This improves performance slightly and gives the wrapper function a more
descriptive name to help with debugging/profiling.
  • Loading branch information
mohd-akram committed Sep 6, 2024
1 parent e914d60 commit 668d2fa
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 35 deletions.
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;
});
}

0 comments on commit 668d2fa

Please sign in to comment.