Skip to content

Commit

Permalink
Rename AST objects to match type names
Browse files Browse the repository at this point in the history
  • Loading branch information
kpdecker committed Nov 28, 2014
1 parent f990cf0 commit 6a7a8c8
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 75 deletions.
24 changes: 12 additions & 12 deletions lib/handlebars/compiler/ast.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Exception from "../exception";

var AST = {
ProgramNode: function(statements, blockParams, strip, locInfo) {
Program: function(statements, blockParams, strip, locInfo) {
this.loc = locInfo;
this.type = 'Program';
this.body = statements;
Expand All @@ -10,7 +10,7 @@ var AST = {
this.strip = strip;
},

MustacheNode: function(rawParams, open, strip, locInfo) {
MustacheStatement: function(rawParams, open, strip, locInfo) {
this.loc = locInfo;
this.type = 'MustacheStatement';

Expand All @@ -28,7 +28,7 @@ var AST = {
this.strip = strip;
},

BlockNode: function(sexpr, program, inverse, strip, locInfo) {
BlockStatement: function(sexpr, program, inverse, strip, locInfo) {
this.loc = locInfo;

this.type = 'BlockStatement';
Expand All @@ -38,7 +38,7 @@ var AST = {
this.strip = strip;
},

PartialNode: function(sexpr, strip, locInfo) {
PartialStatement: function(sexpr, strip, locInfo) {
this.loc = locInfo;
this.type = 'PartialStatement';
this.sexpr = sexpr;
Expand All @@ -48,13 +48,13 @@ var AST = {
this.strip.inlineStandalone = true;
},

ContentNode: function(string, locInfo) {
ContentStatement: function(string, locInfo) {
this.loc = locInfo;
this.type = 'ContentStatement';
this.original = this.value = string;
},

CommentNode: function(comment, strip, locInfo) {
CommentStatement: function(comment, strip, locInfo) {
this.loc = locInfo;
this.type = 'CommentStatement';
this.value = comment;
Expand All @@ -63,7 +63,7 @@ var AST = {
strip.inlineStandalone = true;
},

SexprNode: function(rawParams, hash, locInfo) {
SubExpression: function(rawParams, hash, locInfo) {
this.loc = locInfo;

this.type = 'SubExpression';
Expand All @@ -72,7 +72,7 @@ var AST = {
this.hash = hash;
},

PathNode: function(data, parts, locInfo) {
PathExpression: function(data, parts, locInfo) {
this.loc = locInfo;
this.type = 'PathExpression';

Expand Down Expand Up @@ -103,27 +103,27 @@ var AST = {
this.depth = depth;
},

StringNode: function(string, locInfo) {
StringLiteral: function(string, locInfo) {
this.loc = locInfo;
this.type = 'StringLiteral';
this.original =
this.value = string;
},

NumberNode: function(number, locInfo) {
NumberLiteral: function(number, locInfo) {
this.loc = locInfo;
this.type = 'NumberLiteral';
this.original =
this.value = Number(number);
},

BooleanNode: function(bool, locInfo) {
BooleanLiteral: function(bool, locInfo) {
this.loc = locInfo;
this.type = 'BooleanLiteral';
this.value = bool === 'true';
},

HashNode: function(pairs, locInfo) {
Hash: function(pairs, locInfo) {
this.loc = locInfo;
this.type = 'Hash';
this.pairs = pairs;
Expand Down
8 changes: 4 additions & 4 deletions lib/handlebars/compiler/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ export function prepareRawBlock(openRawBlock, content, close, locInfo) {
throw new Exception(openRawBlock.sexpr.path.original + " doesn't match " + close, errorNode);
}

var program = new this.ProgramNode([content], null, {}, locInfo);
var program = new this.Program([content], null, {}, locInfo);

return new this.BlockNode(openRawBlock.sexpr, program, undefined, undefined, locInfo);
return new this.BlockStatement(openRawBlock.sexpr, program, undefined, undefined, locInfo);
}

export function prepareBlock(openBlock, program, inverseAndProgram, close, inverted, locInfo) {
Expand Down Expand Up @@ -109,9 +109,9 @@ export function prepareBlock(openBlock, program, inverseAndProgram, close, inver
}

if (inverted) {
return new this.BlockNode(openBlock.sexpr, inverse, program, strip, locInfo);
return new this.BlockStatement(openBlock.sexpr, inverse, program, strip, locInfo);
} else {
return new this.BlockNode(openBlock.sexpr, program, inverse, strip, locInfo);
return new this.BlockStatement(openBlock.sexpr, program, inverse, strip, locInfo);
}
}

Expand Down
70 changes: 32 additions & 38 deletions spec/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ describe('ast', function() {
equals(node.loc.end.column, 1);
}

describe('MustacheNode', function() {
describe('MustacheStatement', function() {
function testEscape(open, expected) {
var mustache = new handlebarsEnv.AST.MustacheNode([{}], open, false);
var mustache = new handlebarsEnv.AST.MustacheStatement([{}], open, false);
equals(mustache.escaped, expected);
}

it('should store args', function() {
var id = {isSimple: true},
hash = {},
mustache = new handlebarsEnv.AST.MustacheNode([id, 'param1'], '', false, LOCATION_INFO);
mustache = new handlebarsEnv.AST.MustacheStatement([id, 'param1'], '', false, LOCATION_INFO);
equals(mustache.type, 'MustacheStatement');
equals(mustache.escaped, true);
testLocationInfoStorage(mustache);
Expand Down Expand Up @@ -61,17 +61,17 @@ describe('ast', function() {
testEscape(undefined, false);
});
});
describe('BlockNode', function() {
describe('BlockStatement', function() {
it('should throw on mustache mismatch', function() {
shouldThrow(function() {
handlebarsEnv.parse("\n {{#foo}}{{/bar}}");
}, Handlebars.Exception, "foo doesn't match bar - 2:5");
});

it('stores location info', function(){
var sexprNode = new handlebarsEnv.AST.SexprNode([{ original: 'foo'}], null);
var mustacheNode = new handlebarsEnv.AST.MustacheNode(sexprNode, null, '{{', {});
var block = new handlebarsEnv.AST.BlockNode(mustacheNode,
var sexprNode = new handlebarsEnv.AST.SubExpression([{ original: 'foo'}], null);
var mustacheNode = new handlebarsEnv.AST.MustacheStatement(sexprNode, null, '{{', {});
var block = new handlebarsEnv.AST.BlockStatement(mustacheNode,
{body: [], strip: {}}, {body: [], strip: {}},
{
strip: {},
Expand All @@ -81,24 +81,24 @@ describe('ast', function() {
testLocationInfoStorage(block);
});
});
describe('PathNode', function() {
describe('PathExpression', function() {
it('should throw on invalid path', function() {
shouldThrow(function() {
new handlebarsEnv.AST.PathNode(false, [
new handlebarsEnv.AST.PathExpression(false, [
{part: 'foo'},
{part: '..'},
{part: 'bar'}
], {start: {line: 1, column: 1}});
}, Handlebars.Exception, "Invalid path: foo.. - 1:1");
shouldThrow(function() {
new handlebarsEnv.AST.PathNode(false, [
new handlebarsEnv.AST.PathExpression(false, [
{part: 'foo'},
{part: '.'},
{part: 'bar'}
], {start: {line: 1, column: 1}});
}, Handlebars.Exception, "Invalid path: foo. - 1:1");
shouldThrow(function() {
new handlebarsEnv.AST.PathNode(false, [
new handlebarsEnv.AST.PathExpression(false, [
{part: 'foo'},
{part: 'this'},
{part: 'bar'}
Expand All @@ -107,69 +107,63 @@ describe('ast', function() {
});

it('stores location info', function(){
var idNode = new handlebarsEnv.AST.PathNode(false, [], LOCATION_INFO);
var idNode = new handlebarsEnv.AST.PathExpression(false, [], LOCATION_INFO);
testLocationInfoStorage(idNode);
});
});

describe("HashNode", function(){

describe('Hash', function(){
it('stores location info', function(){
var hash = new handlebarsEnv.AST.HashNode([], LOCATION_INFO);
var hash = new handlebarsEnv.AST.Hash([], LOCATION_INFO);
testLocationInfoStorage(hash);
});
});

describe("ContentNode", function(){

describe('ContentStatement', function(){
it('stores location info', function(){
var content = new handlebarsEnv.AST.ContentNode("HI", LOCATION_INFO);
var content = new handlebarsEnv.AST.ContentStatement("HI", LOCATION_INFO);
testLocationInfoStorage(content);
});
});

describe("CommentNode", function(){

describe('CommentStatement', function(){
it('stores location info', function(){
var comment = new handlebarsEnv.AST.CommentNode("HI", {}, LOCATION_INFO);
var comment = new handlebarsEnv.AST.CommentStatement("HI", {}, LOCATION_INFO);
testLocationInfoStorage(comment);
});
});

describe("NumberNode", function(){

describe('NumberLiteral', function(){
it('stores location info', function(){
var integer = new handlebarsEnv.AST.NumberNode("6", LOCATION_INFO);
var integer = new handlebarsEnv.AST.NumberLiteral("6", LOCATION_INFO);
testLocationInfoStorage(integer);
});
});

describe("StringNode", function(){

describe('StringLiteral', function(){
it('stores location info', function(){
var string = new handlebarsEnv.AST.StringNode("6", LOCATION_INFO);
var string = new handlebarsEnv.AST.StringLiteral("6", LOCATION_INFO);
testLocationInfoStorage(string);
});
});

describe("BooleanNode", function(){

describe('BooleanLiteral', function(){
it('stores location info', function(){
var bool = new handlebarsEnv.AST.BooleanNode("true", LOCATION_INFO);
var bool = new handlebarsEnv.AST.BooleanLiteral("true", LOCATION_INFO);
testLocationInfoStorage(bool);
});
});

describe("PartialNode", function(){
describe('PartialStatement', function(){
it('stores location info', function(){
var pn = new handlebarsEnv.AST.PartialNode('so_partial', {}, LOCATION_INFO);
var pn = new handlebarsEnv.AST.PartialStatement('so_partial', {}, LOCATION_INFO);
testLocationInfoStorage(pn);
});
});

describe('ProgramNode', function(){
describe('Program', function(){
it('storing location info', function(){
var pn = new handlebarsEnv.AST.ProgramNode([], null, {}, LOCATION_INFO);
var pn = new handlebarsEnv.AST.Program([], null, {}, LOCATION_INFO);
testLocationInfoStorage(pn);
});
});
Expand All @@ -193,7 +187,7 @@ describe('ast', function() {
testColumns(contentNode, 1, 1, 0, 7);
});

it('gets MustacheNode line numbers', function(){
it('gets MustacheStatement line numbers', function(){
var mustacheNode = body[1];
testColumns(mustacheNode, 1, 1, 7, 21);
});
Expand All @@ -202,9 +196,9 @@ describe('ast', function() {
testColumns(body[2], 1, 2, 21, 8);
});

it('gets MustacheNode line numbers correct across newlines', function(){
var secondMustacheNode = body[3];
testColumns(secondMustacheNode, 2, 2, 8, 22);
it('gets MustacheStatement line numbers correct across newlines', function(){
var secondMustacheStatement = body[3];
testColumns(secondMustacheStatement, 2, 2, 8, 22);
});

it('gets the block helper information correct', function(){
Expand Down
4 changes: 2 additions & 2 deletions spec/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('compiler', function() {
});

it('can utilize AST instance', function() {
equal(Handlebars.compile(new Handlebars.AST.ProgramNode([ new Handlebars.AST.ContentNode("Hello")], null, {}))(), 'Hello');
equal(Handlebars.compile(new Handlebars.AST.Program([ new Handlebars.AST.ContentStatement("Hello")], null, {}))(), 'Hello');
});

it("can pass through an empty string", function() {
Expand All @@ -60,7 +60,7 @@ describe('compiler', function() {
});

it('can utilize AST instance', function() {
equal(/return "Hello"/.test(Handlebars.precompile(new Handlebars.AST.ProgramNode([ new Handlebars.AST.ContentNode("Hello")]), null, {})), true);
equal(/return "Hello"/.test(Handlebars.precompile(new Handlebars.AST.Program([ new Handlebars.AST.ContentStatement("Hello")]), null, {})), true);
});

it("can pass through an empty string", function() {
Expand Down
2 changes: 1 addition & 1 deletion spec/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ describe('parser', function() {

describe('externally compiled AST', function() {
it('can pass through an already-compiled AST', function() {
equals(ast_for(new Handlebars.AST.ProgramNode([new Handlebars.AST.ContentNode("Hello")], null)), "CONTENT[ \'Hello\' ]\n");
equals(ast_for(new Handlebars.AST.Program([new Handlebars.AST.ContentStatement("Hello")], null)), "CONTENT[ \'Hello\' ]\n");
});
});
});
Loading

0 comments on commit 6a7a8c8

Please sign in to comment.