Skip to content

Commit

Permalink
Create validateClose helper method
Browse files Browse the repository at this point in the history
Avoid duplicating the logic needed to check for close block mismatches.
  • Loading branch information
kpdecker committed Aug 15, 2015
1 parent c62da22 commit 2a933fd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
35 changes: 17 additions & 18 deletions lib/handlebars/compiler/helpers.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -71,11 +81,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 = new this.Program(contents, null, {}, locInfo);
Expand All @@ -88,11 +94,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;
Expand Down Expand Up @@ -147,17 +150,13 @@ 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 new this.PartialBlockStatement(
openPartialBlock.name, openPartialBlock.params, openPartialBlock.hash,
open.path, open.params, open.hash,
program,
openPartialBlock.strip, close && close.strip,
open.strip, close && close.strip,
this.locInfo(locInfo));
}

2 changes: 1 addition & 1 deletion src/handlebars.yy
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,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
Expand Down

0 comments on commit 2a933fd

Please sign in to comment.