Skip to content

Instantly share code, notes, and snippets.

@FreeMasen
Created May 21, 2019 18:55
Show Gist options
  • Save FreeMasen/cf76ca958cf1d5af16cd7f57a1c130e0 to your computer and use it in GitHub Desktop.
Save FreeMasen/cf76ca958cf1d5af16cd7f57a1c130e0 to your computer and use it in GitHub Desktop.
JS AST Example
{
"type": "Program",
"body": [
{
"type": "IfStatement",
"test": {
"type": "BinaryExpression",
"operator": ">",
"left": {
"type": "CallExpression",
"callee": {
"type": "MemberExpression",
"computed": false,
"object": {
"type": "Identifier",
"name": "Math"
},
"property": {
"type": "Identifier",
"name": "random"
}
},
"arguments": []
},
"right": {
"type": "Literal",
"value": 0.5,
"raw": "0.5"
}
},
"consequent": {
"type": "BlockStatement",
"body": [
{
"type": "ExpressionStatement",
"expression": {
"type": "CallExpression",
"callee": {
"type": "MemberExpression",
"computed": false,
"object": {
"type": "Identifier",
"name": "console"
},
"property": {
"type": "Identifier",
"name": "log"
}
},
"arguments": [
{
"type": "Literal",
"value": "More than half!",
"raw": "'More than half!'"
}
]
}
}
]
},
"alternate": {
"type": "IfStatement",
"test": {
"type": "BinaryExpression",
"operator": "===",
"left": {
"type": "BinaryExpression",
"operator": "%",
"left": {
"type": "CallExpression",
"callee": {
"type": "MemberExpression",
"computed": false,
"object": {
"type": "NewExpression",
"callee": {
"type": "Identifier",
"name": "Date"
},
"arguments": []
},
"property": {
"type": "Identifier",
"name": "getHours"
}
},
"arguments": []
},
"right": {
"type": "Literal",
"value": 2,
"raw": "2"
}
},
"right": {
"type": "Literal",
"value": 0,
"raw": "0"
}
},
"consequent": {
"type": "BlockStatement",
"body": [
{
"type": "ExpressionStatement",
"expression": {
"type": "CallExpression",
"callee": {
"type": "MemberExpression",
"computed": false,
"object": {
"type": "Identifier",
"name": "console"
},
"property": {
"type": "Identifier",
"name": "log"
}
},
"arguments": [
{
"type": "Literal",
"value": "Even hour!",
"raw": "'Even hour!'"
}
]
}
}
]
},
"alternate": {
"type": "BlockStatement",
"body": [
{
"type": "ExpressionStatement",
"expression": {
"type": "CallExpression",
"callee": {
"type": "MemberExpression",
"computed": false,
"object": {
"type": "Identifier",
"name": "console"
},
"property": {
"type": "Identifier",
"name": "log"
}
},
"arguments": [
{
"type": "Literal",
"value": "Less than half and odd hour!",
"raw": "'Less than half and odd hour!'"
}
]
}
}
]
}
}
}
],
"sourceType": "script"
}
if (Math.random() > 0.5) {
console.log('More than half!');
} else if (new Date().getHours() % 2 === 0) {
console.log('Even hour!');
} else {
console.log('Less than half and odd hour!')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment