Created
November 7, 2016 18:15
-
-
Save pthrasher/231807eb15155e9a6f03b16e37e56a67 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is just a toy. I used this to experient before adding more advanced functionality into Targaryen. | |
const esprima = require('esprima'); | |
var expr = "data.val() == null && auth != null && (auth.isWorker == true || (newData.parent().child('chatType').val() == 'oneToOne' && $chatID.contains(auth.uid) == true || newData.parent().child('chatType').val() == 'group' && newData.parent().child('users').val() != null && newData.parent().child('users').child(auth.uid).val() != null || newData.parent().child('chatType').val() == 'org' && (auth.officialCounselorAt == null && $chatID.contains(auth.uid) == true || newData.parent().child('orgID').val() != null && auth.officialCounselorAt != null && auth.officialCounselorAt[newData.parent().child('orgID').val() + ''] == true))) || data.val() != null && newData.val() != null && auth != null && auth.isWorker == true || data.val() != null && newData.val() == null && auth != null && auth.isWorker == true"; | |
var tree = esprima.parse(expr); | |
// BinaryExpression | |
// CallExpression | |
// ExpressionStatement | |
// Identifier | |
// Literal | |
// LogicalExpression | |
// MemberExpression | |
// Program | |
var indentLevel = 0; | |
function indent() { | |
indentLevel++; | |
} | |
function dedent() { | |
indentLevel--; | |
} | |
function ws() { | |
var ws = ''; | |
for (var i = 0; i < indentLevel; i++) { | |
ws += ' '; | |
} | |
return ws; | |
} | |
function printNode(node) { | |
if (printers[node.type] != null) { | |
return printers[node.type](node); | |
} | |
return '-------'; | |
} | |
var printers = { | |
BinaryExpression(node) { | |
return printNode(node.left) + " " + node.operator + " " + printNode(node.right); | |
}, | |
CallExpression(node) { | |
var args = node.arguments.map((n) => printNode(n)); | |
return printNode(node.callee) + "(" + args.join(', ') + ")"; | |
}, | |
ExpressionStatement(node) { | |
var str = ws() + "(\n"; | |
indent(); | |
str += ws() + printNode(node.expression) + "\n"; | |
dedent(); | |
str += ws() + ")"; | |
return str; | |
}, | |
Identifier(node) { | |
return node.name; | |
}, | |
Literal(node) { | |
return node.raw; | |
}, | |
LogicalExpression(node) { | |
var str = "(\n"; | |
indent(); | |
str += ws() + printNode(node.left) + "\n"; | |
str += ws() + node.operator + "\n"; | |
str += ws() + printNode(node.right) + "\n"; | |
dedent(); | |
str += ws() + ")"; | |
return str | |
}, | |
MemberExpression(node) { | |
if (node.property.type === 'Identifier') { | |
return printNode(node.object) + '.' + printNode(node.property); | |
} else { | |
return printNode(node.object) + '[' + printNode(node.property) + ']'; | |
} | |
}, | |
Program(node) { | |
return printNode(node.body[0]); | |
}, | |
}; | |
console.log(printNode(tree)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment