Skip to content

Commit

Permalink
Merge pull request #14 from gastonrobledo/master
Browse files Browse the repository at this point in the history
increase version of handlerbar and update with latest 4.3.0 changes
  • Loading branch information
gastonrobledo authored Oct 1, 2019
2 parents fd5926a + fcc8292 commit cefe1bc
Show file tree
Hide file tree
Showing 7 changed files with 523 additions and 32 deletions.
9 changes: 5 additions & 4 deletions lib/handlebars/base.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
const { createFrame, extend, toString } = require('./utils'),
Exception = require('./exception'),
registerDefaultHelpers = require('./helpers'),
{ registerDefaultHelpers } = require('./helpers'),
registerDefaultDecorators = require('./decorators'),
logger = require('./logger'),
{ log } = logger,
VERSION = '4.1.2',
COMPILER_REVISION = 7,
VERSION = '4.3.0',
COMPILER_REVISION = 8,
REVISION_CHANGES = {
1: '<= 1.0.rc.2', // 1.0.rc.2 is actually rev2 but doesn't report it
2: '== 1.0.0-rc.3',
3: '== 1.0.0-rc.4',
4: '== 1.x.x',
5: '== 2.0.0-alpha.x',
6: '>= 2.0.0-beta.1',
7: '>= 4.0.0'
7: '>= 4.0.0 <4.3.0',
8: '>= 4.3.0'
},
objectType = '[object Object]'

Expand Down
33 changes: 24 additions & 9 deletions lib/handlebars/compiler/javascript-compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ JavaScriptCompiler.prototype = {
// replace it on the stack with the result of properly
// invoking blockHelperMissing.
blockValue(name) {
const blockHelperMissing = this.aliasable('helpers.blockHelperMissing'),
const blockHelperMissing = this.aliasable('container.hooks.blockHelperMissing'),
params = [this.contextName(0)]
this.setupHelperArgs(name, 0, params)

Expand All @@ -275,7 +275,7 @@ JavaScriptCompiler.prototype = {
// On stack, after, if lastHelper: value
ambiguousBlockValue() {
// We're being a bit cheeky and reusing the options value from the prior exec
const blockHelperMissing = this.aliasable('helpers.blockHelperMissing'),
const blockHelperMissing = this.aliasable('container.hooks.blockHelperMissing'),
params = [this.contextName(0)]
this.setupHelperArgs('', 0, params, true)

Expand Down Expand Up @@ -571,16 +571,31 @@ JavaScriptCompiler.prototype = {
// If the helper is not found, `helperMissing` is called.
invokeHelper(paramSize, name, isSimple) {
const nonHelper = this.popStack(),
helper = this.setupHelper(paramSize, name),
simple = isSimple ? [helper.name, ' || '] : '',
helper = this.setupHelper(paramSize, name)

let possibleFunctionCalls = []

if (isSimple) { // direct call to helper
possibleFunctionCalls.push(helper.name)
}
// call a function from the input object
possibleFunctionCalls.push(nonHelper)

lookup = ['('].concat(simple, nonHelper)
if (!this.options.strict) {
lookup.push(' || ', this.aliasable('helpers.helperMissing'))
possibleFunctionCalls.push(this.aliasable('container.hooks.helperMissing'))
}
lookup.push(')')
let functionLookupCode = ['(', this.itemsSeparatedBy(possibleFunctionCalls, '||'), ')'];
let functionCall = this.source.functionCall(functionLookupCode, 'call', helper.callParams);
this.push(functionCall);
},

this.push(this.source.functionCall(lookup, 'call', helper.callParams))
itemsSeparatedBy: function(items, separator) {
let result = [];
result.push(items[0]);
for (let i = 1; i < items.length; i++) {
result.push(separator, items[i]);
}
return result;
},

// [invokeKnownHelper]
Expand Down Expand Up @@ -622,7 +637,7 @@ JavaScriptCompiler.prototype = {
lookup[0] = '(helper = '
lookup.push(
' != null ? helper : ',
this.aliasable('helpers.helperMissing')
this.aliasable('container.hooks.helperMissing')
)
}

Expand Down
15 changes: 14 additions & 1 deletion lib/handlebars/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const registerBlockHelperMissing = require('./helpers/block-helper-missing'),
registerExtend = require('./helpers/extend'),
registerBlock = require('./helpers/block')

module.exports = function registerDefaultHelpers(instance) {
function registerDefaultHelpers(instance) {
registerBlockHelperMissing(instance)
registerEach(instance)
registerHelperMissing(instance)
Expand All @@ -19,3 +19,16 @@ module.exports = function registerDefaultHelpers(instance) {
registerBlock(instance)
registerExtend(instance)
}
function moveHelperToHooks(instance, helperName, keepHelper) {
if (instance.helpers[helperName]) {
instance.hooks[helperName] = instance.helpers[helperName];
if (!keepHelper) {
delete instance.helpers[helperName];
}
}
}

module.exports = {
registerDefaultHelpers,
moveHelperToHooks
}
28 changes: 13 additions & 15 deletions lib/handlebars/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
/* eslint-disable no-multi-assign */
const Utils = require('./utils'),
Exception = require('./exception'),
{ COMPILER_REVISION, REVISION_CHANGES, createFrame } = require('./base')
{ COMPILER_REVISION, REVISION_CHANGES, createFrame } = require('./base'),
{ moveHelperToHooks } = require('./helpers')

function checkRevision(compilerInfo) {
const compilerRevision = compilerInfo && (compilerInfo[0] || 1),
Expand Down Expand Up @@ -45,11 +46,12 @@ function template(templateSpec, env, callback) {
}

partial = env.VM.resolvePartial.call(this, partial, context, options)
let result = env.VM.invokePartial.call(this, partial, context, options)
let optionsWithHooks = Utils.extend({}, options, {hooks: this.hooks})
let result = env.VM.invokePartial.call(this, partial, context, optionsWithHooks);

if (result == null && env.compile) {
options.partials[options.name] = env.compile(partial, templateSpec.compilerOptions, env)
result = options.partials[options.name](context, options)
result = options.partials[options.name](context, optionsWithHooks)
}
if (result != null) {
if (options.indent) {
Expand Down Expand Up @@ -120,15 +122,6 @@ function template(templateSpec, env, callback) {
}
return value
},
merge(param, common) {
let obj = param || common

if (param && common && (param !== common)) {
obj = Utils.extend({}, common, param)
}

return obj
},
// An empty object to use as replacement for null-contexts
nullContext: Object.seal({}),

Expand Down Expand Up @@ -164,18 +157,23 @@ function template(templateSpec, env, callback) {

function _setup(options) {
if (!options.partial) {
container.helpers = container.merge(options.helpers, env.helpers)
container.helpers = Utils.extend({}, options.helpers, env.helpers)

if (templateSpec.usePartial) {
container.partials = container.merge(options.partials, env.partials)
container.partials = Utils.extend({}, options.partials, env.partials)
}
if (templateSpec.usePartial || templateSpec.useDecorators) {
container.decorators = container.merge(options.decorators, env.decorators)
container.decorators = Utils.extend({}, options.decorators, env.decorators)
}
container.hooks = {};
let keepHelper = options.allowCallsToHelperMissing;
moveHelperToHooks(container, 'helperMissing', keepHelper);
moveHelperToHooks(container, 'blockHelperMissing', keepHelper);
} else {
container.helpers = options.helpers
container.partials = options.partials
container.decorators = options.decorators
container.hooks = options.hooks
}
if (options.callback) {
container.callback = options.callback
Expand Down
Loading

0 comments on commit cefe1bc

Please sign in to comment.