diff options
author | kpdecker <kpdecker@gmail.com> | 2015-08-15 14:19:02 -0500 |
---|---|---|
committer | kpdecker <kpdecker@gmail.com> | 2015-08-22 10:59:08 -0700 |
commit | 233caf3f7c78b310b723fe80473e932418d01c65 (patch) | |
tree | 9ffd98febb11985175804a09ec42736263e84360 | |
parent | 91ffd32cad32b2d1cd310ff94f65b28c428206ac (diff) | |
download | handlebars.js-233caf3f7c78b310b723fe80473e932418d01c65.zip handlebars.js-233caf3f7c78b310b723fe80473e932418d01c65.tar.gz handlebars.js-233caf3f7c78b310b723fe80473e932418d01c65.tar.bz2 |
Create validateClose helper method
Avoid duplicating the logic needed to check for close block mismatches.
-rw-r--r-- | lib/handlebars/compiler/helpers.js | 39 | ||||
-rw-r--r-- | src/handlebars.yy | 2 |
2 files changed, 20 insertions, 21 deletions
diff --git a/lib/handlebars/compiler/helpers.js b/lib/handlebars/compiler/helpers.js index e04f4dd..9c40f0d 100644 --- a/lib/handlebars/compiler/helpers.js +++ b/lib/handlebars/compiler/helpers.js @@ -1,5 +1,15 @@ import Exception from '../exception'; +function validateClose(open, close) { + close = close.path ? close.path.original : close; + + if (open.path.original !== close) { + let errorNode = {loc: open.path.loc}; + + throw new Exception(open.path.original + " doesn't match " + close, errorNode); + } +} + export function SourceLocation(source, locInfo) { this.source = source; this.start = { @@ -86,11 +96,7 @@ export function prepareMustache(path, params, hash, open, strip, locInfo) { } export function prepareRawBlock(openRawBlock, contents, close, locInfo) { - if (openRawBlock.path.original !== close) { - let errorNode = {loc: openRawBlock.path.loc}; - - throw new Exception(openRawBlock.path.original + " doesn't match " + close, errorNode); - } + validateClose(openRawBlock, close); locInfo = this.locInfo(locInfo); let program = { @@ -114,11 +120,8 @@ export function prepareRawBlock(openRawBlock, contents, close, locInfo) { } export function prepareBlock(openBlock, program, inverseAndProgram, close, inverted, locInfo) { - // When we are chaining inverse calls, we will not have a close path - if (close && close.path && openBlock.path.original !== close.path.original) { - let errorNode = {loc: openBlock.path.loc}; - - throw new Exception(openBlock.path.original + ' doesn\'t match ' + close.path.original, errorNode); + if (close && close.path) { + validateClose(openBlock, close); } program.blockParams = openBlock.blockParams; @@ -185,20 +188,16 @@ export function prepareProgram(statements, loc) { } -export function preparePartialBlock(openPartialBlock, program, close, locInfo) { - if (openPartialBlock.name.original !== close.path.original) { - let errorNode = {loc: openPartialBlock.name.loc}; - - throw new Exception(openPartialBlock.name.original + " doesn't match " + close.path.original, errorNode); - } +export function preparePartialBlock(open, program, close, locInfo) { + validateClose(open, close); return { type: 'PartialBlockStatement', - path: openPartialBlock.name, - params: openPartialBlock.params, - hash: openPartialBlock.hash, + name: open.path, + params: open.params, + hash: open.hash, program, - openStrip: openPartialBlock.strip, + openStrip: open.strip, closeStrip: close && close.strip, loc: this.locInfo(locInfo) }; diff --git a/src/handlebars.yy b/src/handlebars.yy index ee0eddb..e94ab51 100644 --- a/src/handlebars.yy +++ b/src/handlebars.yy @@ -106,7 +106,7 @@ partialBlock : openPartialBlock program closeBlock -> yy.preparePartialBlock($1, $2, $3, @$) ; openPartialBlock - : OPEN_PARTIAL_BLOCK partialName param* hash? CLOSE -> { name: $2, params: $3, hash: $4, strip: yy.stripFlags($1, $5) } + : OPEN_PARTIAL_BLOCK partialName param* hash? CLOSE -> { path: $2, params: $3, hash: $4, strip: yy.stripFlags($1, $5) } ; param |