From 9d5aa363cf3031b586e9945cf990e178f5b370db Mon Sep 17 00:00:00 2001 From: Nils Knappmeier Date: Mon, 13 Jan 2020 21:39:01 +0100 Subject: [PATCH] fix: don't wrap helpers that are not functions - helpers should always be a function, but in #1639 one seems to be undefined. This was not a problem before 4.6 because helpers weren't wrapped then. Now, we must take care only to wrap helpers (when adding the "lookupProperty" function to the options), if they are really functions. --- lib/handlebars/internal/wrapHelper.js | 5 +++++ spec/regressions.js | 9 +++++++++ 2 files changed, 14 insertions(+) diff --git a/lib/handlebars/internal/wrapHelper.js b/lib/handlebars/internal/wrapHelper.js index 0010eb8c2..29d65b033 100644 --- a/lib/handlebars/internal/wrapHelper.js +++ b/lib/handlebars/internal/wrapHelper.js @@ -1,4 +1,9 @@ export function wrapHelper(helper, transformOptionsFn) { + 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; + } let wrapper = function(/* dynamic arguments */) { const options = arguments[arguments.length - 1]; arguments[arguments.length - 1] = transformOptionsFn(options); diff --git a/spec/regressions.js b/spec/regressions.js index 3a84dd7da..f6147ad6e 100644 --- a/spec/regressions.js +++ b/spec/regressions.js @@ -518,4 +518,13 @@ describe('Regressions', function() { sinon.restore(); }); }); + + describe("GH-1639: TypeError: Cannot read property 'apply' of undefined\" when handlebars version > 4.6.0 (undocumented, deprecated usage)", function() { + it('should treat undefined helpers like non-existing helpers', function() { + expectTemplate('{{foo}}') + .withHelper('foo', undefined) + .withInput({ foo: 'bar' }) + .toCompileTo('bar'); + }); + }); });