If/ElseIf/Else cleanup

This commit is contained in:
Patrick Steele-Idem 2015-12-28 14:42:37 -07:00
parent 705f22c29f
commit 81ca6753d4
23 changed files with 150 additions and 26 deletions

View File

@ -64,9 +64,7 @@ class Builder {
elseIfStatement(test, body, elseStatement) {
test = makeNode(test);
return new ElseIf({
if: new If({test, body, else: elseStatement})
});
return new ElseIf({test, body, else: elseStatement});
}
expression(value) {

View File

@ -1,12 +1,13 @@
'use strict';
var Node = require('./Node');
var ok = require('assert').ok;
class ElseIf extends Node {
constructor(def) {
super('ElseIf');
this.if = def.if;
this.test = def.test;
this.body = this.makeContainer(def.body);
this.else = def.else;
this.matched = false;
}
@ -16,11 +17,9 @@ class ElseIf extends Node {
return;
}
var ifStatement = this.if;
ok(ifStatement);
var ifStatement = generator.builder.ifStatement(this.test, this.body, this.else);
generator.write('else ');
generator.generateCode(this.if);
generator.generateCode(ifStatement);
}
}

View File

@ -6,6 +6,7 @@ function removeWhitespaceNodes(whitespaceNodes) {
for (var i=0; i<whitespaceNodes.length; i++) {
whitespaceNodes[i].detach();
}
whitespaceNodes.length = 0;
}
class If extends Node {

View File

@ -25,7 +25,7 @@ var coreAttrHandlers = [
return;
}
var ifNode = this.builder.ifStatement(ifArgument);
//Surround the existing node with an "if" node
//Surround the existing node with an "If" node
node.wrap(ifNode);
}
],
@ -35,6 +35,7 @@ var coreAttrHandlers = [
if (!ifArgument) {
return;
}
ifArgument = this.builder.negate(ifArgument);
var ifNode = this.builder.ifStatement(ifArgument);
//Surround the existing node with an "if" node
node.wrap(ifNode);
@ -47,28 +48,29 @@ var coreAttrHandlers = [
return;
}
var elseIfNode = this.builder.elseIfStatement(elseIfArgument);
//Surround the existing node with an "if" node
//Surround the existing node with an "ElseIf" node
node.wrap(elseIfNode);
}
],
[
'else', function(attr, node) {
var elseNode = this.builder.elseStatement();
//Surround the existing node with an "if" node
//Surround the existing node with an "Else" node
node.wrap(elseNode);
}
],
[
'body-only-if', function(attr, node, el) {
var condition = attr.argument;
if (!condition) {
this.addError('Invalid "body-only-if" attribute');
return;
}
if (el.nodeType !== '')
el.setStripExpression(attr);
throw new Error('body-only-if Not Implemented');
// var condition = attr.argument;
// if (!condition) {
// this.addError('Invalid "body-only-if" attribute');
// return;
// }
//
// if (el.nodeType !== '')
//
// el.setStripExpression(attr);
}
],
[

View File

@ -1,17 +1,18 @@
module.exports = function nodeFactory(el, context) {
var argument = el.argument;
var attributes = el.attributes;
var elseIfStatement = context.builder.elseIfStatement(argument || 'INVALID');
if (!argument) {
context.addError(elseIfStatement, 'Invalid <else-if> tag. Argument is missing. Example; <if(foo === true)>');
context.addError(el, 'Invalid <else-if> tag. Argument is missing. Example; <if(foo === true)>');
return el;
}
if (attributes.length) {
context.addError(elseIfStatement, 'Invalid <else-if> tag. Attributes not allowed.');
context.addError(el, 'Invalid <else-if> tag. Attributes not allowed.');
return el;
}
var elseIfStatement = context.builder.elseIfStatement(argument);
return elseIfStatement;
};

View File

@ -0,0 +1 @@
<div>B</div>

View File

@ -0,0 +1,6 @@
<div if(data.foo === 0)>
A
</div>
<div else>
B
</div>

View File

@ -0,0 +1,24 @@
exports.templateData = {
"accounts": [
{
"balance": 0,
"balanceFormatted": "$0.00",
"status": "open"
},
{
"balance": 10,
"balanceFormatted": "$10.00",
"status": "closed"
},
{
"balance": -100,
"balanceFormatted": "$-100.00",
"status": "suspended"
},
{
"balance": 999,
"balanceFormatted": "$999.00",
"status": "open"
}
]
};

View File

@ -0,0 +1 @@
<div>B</div>

View File

@ -0,0 +1,6 @@
<div if(data.foo === 0)>
A
</div>
<div else-if(true)>
B
</div>

View File

@ -0,0 +1,24 @@
exports.templateData = {
"accounts": [
{
"balance": 0,
"balanceFormatted": "$0.00",
"status": "open"
},
{
"balance": 10,
"balanceFormatted": "$10.00",
"status": "closed"
},
{
"balance": -100,
"balanceFormatted": "$-100.00",
"status": "suspended"
},
{
"balance": 999,
"balanceFormatted": "$999.00",
"status": "open"
}
]
};

View File

@ -0,0 +1 @@
<div>C</div>

View File

@ -0,0 +1,9 @@
<div if(data.foo === 0)>
A
</div>
<div else-if(data.foo === 1)>
B
</div>
<div else>
C
</div>

View File

@ -0,0 +1,24 @@
exports.templateData = {
"accounts": [
{
"balance": 0,
"balanceFormatted": "$0.00",
"status": "open"
},
{
"balance": 10,
"balanceFormatted": "$10.00",
"status": "closed"
},
{
"balance": -100,
"balanceFormatted": "$-100.00",
"status": "suspended"
},
{
"balance": 999,
"balanceFormatted": "$999.00",
"status": "open"
}
]
};

View File

@ -0,0 +1 @@
C

View File

@ -0,0 +1,9 @@
<if(false)>
A
</if>
<else-if(false)>
B
</else-if>
<else>
C
</else>

View File

@ -0,0 +1 @@
exports.templateData = {};

View File

@ -0,0 +1 @@
B

View File

@ -0,0 +1,6 @@
<if(false)>
A
</if>
<else-if(true)>
B
</else-if>

View File

@ -0,0 +1 @@
exports.templateData = {};

View File

@ -0,0 +1 @@
A

View File

@ -0,0 +1,6 @@
<if(true)>
A
</if>
<else>
B
</else>

View File

@ -0,0 +1 @@
exports.templateData = {};