Skip to content

Commit 962989b

Browse files
committed
Create validateClose helper method
Avoid duplicating the logic needed to check for close block mismatches.
1 parent 815fa88 commit 962989b

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

lib/handlebars/compiler/helpers.js

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
import Exception from '../exception';
22

3+
function validateClose(open, close) {
4+
close = close.path ? close.path.original : close;
5+
6+
if (open.path.original !== close) {
7+
let errorNode = {loc: open.path.loc};
8+
9+
throw new Exception(open.path.original + " doesn't match " + close, errorNode);
10+
}
11+
}
12+
313
export function SourceLocation(source, locInfo) {
414
this.source = source;
515
this.start = {
@@ -71,11 +81,7 @@ export function prepareMustache(path, params, hash, open, strip, locInfo) {
7181
}
7282

7383
export function prepareRawBlock(openRawBlock, contents, close, locInfo) {
74-
if (openRawBlock.path.original !== close) {
75-
let errorNode = {loc: openRawBlock.path.loc};
76-
77-
throw new Exception(openRawBlock.path.original + " doesn't match " + close, errorNode);
78-
}
84+
validateClose(openRawBlock, close);
7985

8086
locInfo = this.locInfo(locInfo);
8187
let program = new this.Program(contents, null, {}, locInfo);
@@ -88,11 +94,8 @@ export function prepareRawBlock(openRawBlock, contents, close, locInfo) {
8894
}
8995

9096
export function prepareBlock(openBlock, program, inverseAndProgram, close, inverted, locInfo) {
91-
// When we are chaining inverse calls, we will not have a close path
92-
if (close && close.path && openBlock.path.original !== close.path.original) {
93-
let errorNode = {loc: openBlock.path.loc};
94-
95-
throw new Exception(openBlock.path.original + ' doesn\'t match ' + close.path.original, errorNode);
97+
if (close && close.path) {
98+
validateClose(openBlock, close);
9699
}
97100

98101
program.blockParams = openBlock.blockParams;
@@ -147,17 +150,13 @@ export function prepareProgram(statements, loc) {
147150
}
148151

149152

150-
export function preparePartialBlock(openPartialBlock, program, close, locInfo) {
151-
if (openPartialBlock.name.original !== close.path.original) {
152-
let errorNode = {loc: openPartialBlock.name.loc};
153-
154-
throw new Exception(openPartialBlock.name.original + " doesn't match " + close.path.original, errorNode);
155-
}
153+
export function preparePartialBlock(open, program, close, locInfo) {
154+
validateClose(open, close);
156155

157156
return new this.PartialBlockStatement(
158-
openPartialBlock.name, openPartialBlock.params, openPartialBlock.hash,
157+
open.path, open.params, open.hash,
159158
program,
160-
openPartialBlock.strip, close && close.strip,
159+
open.strip, close && close.strip,
161160
this.locInfo(locInfo));
162161
}
163162

src/handlebars.yy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ partialBlock
8484
: openPartialBlock program closeBlock -> yy.preparePartialBlock($1, $2, $3, @$)
8585
;
8686
openPartialBlock
87-
: OPEN_PARTIAL_BLOCK partialName param* hash? CLOSE -> { name: $2, params: $3, hash: $4, strip: yy.stripFlags($1, $5) }
87+
: OPEN_PARTIAL_BLOCK partialName param* hash? CLOSE -> { path: $2, params: $3, hash: $4, strip: yy.stripFlags($1, $5) }
8888
;
8989

9090
param

0 commit comments

Comments
 (0)