Skip to content

Commit 7ed6d22

Browse files
committed
Simplify BlockNode by removing intermediate MustacheNode
1 parent 3e1840a commit 7ed6d22

File tree

6 files changed

+40
-26
lines changed

6 files changed

+40
-26
lines changed

lib/handlebars/compiler/ast.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ var AST = {
7979
this.strip.inlineStandalone = true;
8080
},
8181

82-
BlockNode: function(mustache, program, inverse, strip, locInfo) {
82+
BlockNode: function(sexpr, program, inverse, strip, locInfo) {
8383
LocationInfo.call(this, locInfo);
8484

8585
this.type = 'block';
86-
this.mustache = mustache;
86+
this.sexpr = sexpr;
8787
this.program = program;
8888
this.inverse = inverse;
8989
this.strip = strip;
@@ -93,17 +93,11 @@ var AST = {
9393
}
9494
},
9595

96-
RawBlockNode: function(mustache, content, close, locInfo) {
96+
RawBlockNode: function(sexpr, content, locInfo) {
9797
LocationInfo.call(this, locInfo);
9898

99-
if (mustache.sexpr.id.original !== close) {
100-
throw new Exception(mustache.sexpr.id.original + " doesn't match " + close, this);
101-
}
102-
103-
content = new AST.ContentNode(content, locInfo);
104-
10599
this.type = 'block';
106-
this.mustache = mustache;
100+
this.sexpr = sexpr;
107101
this.program = new AST.ProgramNode([content], {}, locInfo);
108102
},
109103

lib/handlebars/compiler/compiler.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ Compiler.prototype = {
108108
},
109109

110110
block: function(block) {
111-
var mustache = block.mustache,
111+
var sexpr = block.sexpr,
112112
program = block.program,
113113
inverse = block.inverse;
114114

@@ -120,7 +120,6 @@ Compiler.prototype = {
120120
inverse = this.compileProgram(inverse);
121121
}
122122

123-
var sexpr = mustache.sexpr;
124123
var type = this.classifySexpr(sexpr);
125124

126125
if (type === "helper") {

lib/handlebars/compiler/helpers.js

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,34 @@ export function stripFlags(open, close) {
77
};
88
}
99

10+
export function prepareRawBlock(openRawBlock, content, close, locInfo) {
11+
if (openRawBlock.sexpr.id.original !== close) {
12+
var errorNode = {
13+
firstLine: openRawBlock.sexpr.firstLine,
14+
firstColumn: openRawBlock.sexpr.firstColumn
15+
};
16+
17+
throw new Exception(openRawBlock.sexpr.id.original + " doesn't match " + close, errorNode);
18+
}
19+
20+
return new this.RawBlockNode(openRawBlock.sexpr, content, locInfo);
21+
}
1022

11-
export function prepareBlock(mustache, program, inverseAndProgram, close, inverted, locInfo) {
23+
export function prepareBlock(openBlock, program, inverseAndProgram, close, inverted, locInfo) {
1224
/*jshint -W040 */
13-
if (mustache.sexpr.id.original !== close.path.original) {
14-
throw new Exception(mustache.sexpr.id.original + ' doesn\'t match ' + close.path.original, mustache);
25+
if (openBlock.sexpr.id.original !== close.path.original) {
26+
var errorNode = {
27+
firstLine: openBlock.sexpr.firstLine,
28+
firstColumn: openBlock.sexpr.firstColumn
29+
};
30+
31+
throw new Exception(openBlock.sexpr.id.original + ' doesn\'t match ' + close.path.original, errorNode);
1532
}
1633

1734
var inverse = inverseAndProgram && inverseAndProgram.program;
1835

1936
var strip = {
20-
left: mustache.strip.left,
37+
left: openBlock.strip.left,
2138
right: close.strip.right,
2239

2340
// Determine the standalone candiacy. Basically flag our content as being possibly standalone
@@ -26,7 +43,7 @@ export function prepareBlock(mustache, program, inverseAndProgram, close, invert
2643
closeStandalone: isPrevWhitespace((inverse || program).statements)
2744
};
2845

29-
if (mustache.strip.right) {
46+
if (openBlock.strip.right) {
3047
omitRight(program.statements, null, true);
3148
}
3249

@@ -57,9 +74,9 @@ export function prepareBlock(mustache, program, inverseAndProgram, close, invert
5774
}
5875

5976
if (inverted) {
60-
return new this.BlockNode(mustache, inverse, program, strip, locInfo);
77+
return new this.BlockNode(openBlock.sexpr, inverse, program, strip, locInfo);
6178
} else {
62-
return new this.BlockNode(mustache, program, inverse, strip, locInfo);
79+
return new this.BlockNode(openBlock.sexpr, program, inverse, strip, locInfo);
6380
}
6481
}
6582

lib/handlebars/compiler/printer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ PrintVisitor.prototype.block = function(block) {
4040

4141
out = out + this.pad("BLOCK:");
4242
this.padding++;
43-
out = out + this.accept(block.mustache);
43+
out = out + this.accept(block.sexpr);
4444
if (block.program) {
4545
out = out + this.pad("PROGRAM:");
4646
this.padding++;

spec/ast.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ describe('ast', function() {
7171
it('should throw on mustache mismatch', function() {
7272
shouldThrow(function() {
7373
handlebarsEnv.parse("\n {{#foo}}{{/bar}}");
74-
}, Handlebars.Exception, "foo doesn't match bar - 2:2");
74+
}, Handlebars.Exception, "foo doesn't match bar - 2:5");
7575
});
7676

7777
it('stores location info', function(){

src/handlebars.yy

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,20 @@ statement
1717
| block -> $1
1818
| rawBlock -> $1
1919
| partial -> $1
20-
| CONTENT -> new yy.ContentNode($1, @$)
20+
| content -> $1
2121
| COMMENT -> new yy.CommentNode($1, @$)
2222
;
2323

24+
content
25+
: CONTENT -> new yy.ContentNode($1, @$)
26+
;
27+
2428
rawBlock
25-
: openRawBlock CONTENT END_RAW_BLOCK -> new yy.RawBlockNode($1, $2, $3, @$)
29+
: openRawBlock content END_RAW_BLOCK -> yy.prepareRawBlock($1, $2, $3, @$)
2630
;
2731

2832
openRawBlock
29-
: OPEN_RAW_BLOCK sexpr CLOSE_RAW_BLOCK -> new yy.MustacheNode($2, null, '', '', @$)
33+
: OPEN_RAW_BLOCK sexpr CLOSE_RAW_BLOCK -> { sexpr: $2 }
3034
;
3135

3236
block
@@ -35,11 +39,11 @@ block
3539
;
3640

3741
openBlock
38-
: OPEN_BLOCK sexpr CLOSE -> new yy.MustacheNode($2, null, $1, yy.stripFlags($1, $3), @$)
42+
: OPEN_BLOCK sexpr CLOSE -> { sexpr: $2, strip: yy.stripFlags($1, $3) }
3943
;
4044

4145
openInverse
42-
: OPEN_INVERSE sexpr CLOSE -> new yy.MustacheNode($2, null, $1, yy.stripFlags($1, $3), @$)
46+
: OPEN_INVERSE sexpr CLOSE -> { sexpr: $2, strip: yy.stripFlags($1, $3) }
4347
;
4448

4549
inverseAndProgram

0 commit comments

Comments
 (0)