Create validateClose helper method

Avoid duplicating the logic needed to check for close block mismatches.
This commit is contained in:
kpdecker
2015-08-15 14:19:02 -05:00
parent 91ffd32cad
commit 233caf3f7c
2 changed files with 20 additions and 21 deletions
+19 -20
View File
@@ -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)
};
+1 -1
View File
@@ -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