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) { elseIfStatement(test, body, elseStatement) {
test = makeNode(test); test = makeNode(test);
return new ElseIf({ return new ElseIf({test, body, else: elseStatement});
if: new If({test, body, else: elseStatement})
});
} }
expression(value) { expression(value) {

View File

@ -1,12 +1,13 @@
'use strict'; 'use strict';
var Node = require('./Node'); var Node = require('./Node');
var ok = require('assert').ok;
class ElseIf extends Node { class ElseIf extends Node {
constructor(def) { constructor(def) {
super('ElseIf'); super('ElseIf');
this.if = def.if; this.test = def.test;
this.body = this.makeContainer(def.body);
this.else = def.else;
this.matched = false; this.matched = false;
} }
@ -16,11 +17,9 @@ class ElseIf extends Node {
return; return;
} }
var ifStatement = this.if; var ifStatement = generator.builder.ifStatement(this.test, this.body, this.else);
ok(ifStatement);
generator.write('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++) { for (var i=0; i<whitespaceNodes.length; i++) {
whitespaceNodes[i].detach(); whitespaceNodes[i].detach();
} }
whitespaceNodes.length = 0;
} }
class If extends Node { class If extends Node {

View File

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

View File

@ -1,17 +1,18 @@
module.exports = function nodeFactory(el, context) { module.exports = function nodeFactory(el, context) {
var argument = el.argument; var argument = el.argument;
var attributes = el.attributes; var attributes = el.attributes;
var elseIfStatement = context.builder.elseIfStatement(argument || 'INVALID');
if (!argument) { 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) { 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; 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 = {};