-
Notifications
You must be signed in to change notification settings - Fork 0
Ast Lego Bricks
1227am 7/23/2022
A list of common code pieces in ast form. Shoutout to btmills. More info
A complete program source tree.
{
type: "Program";
body: [ Statement ];
}
A function declaration or expression.
{
type: "FunctionDeclaration" | "FunctionExpression"
id: Identifier | null;
params: [ Pattern ];
defaults: [ Expression ];
rest: Identifier | null;
body: BlockStatement
}
Syntax:
function <id>(<params>){
<body>
}
Any statement.
{
type: "BlockStatement";
body: [ Statement ];
}
Syntax:
function e(){
<body>
}
{
type: "ExpressionStatement";
expression: Expression;
}
{
type: "IfStatement";
test: Expression;
consequent: Statement;
alternate: Statement | null;
}
Syntax:
if (<test>){
<consequent>
} else if (<test>){
<consequent>
} else {
<alternate>
}
if(<test>){<consequent>}
{
type: "BreakStatement";
}
Syntax:
break;
{
type: "ContinueStatement";
}
Syntax:
continue;
The lexical flag is metadata indicating whether the switch statement contains any unnested let declarations (and therefore introduces a new lexical scope).
{
type: "SwitchStatement";
discriminant: Expression;
cases: [ SwitchCase ];
}
Syntax:
switch(<discriminant>){
<cases>
}
{
type: "ReturnStatement";
argument: Expression | null;
}
Syntax:
return <argument>
{
type: "ThrowStatement";
argument: Expression;
}
{
type: "TryStatement";
block: BlockStatement;
handler: CatchClause | null;
finalizer: BlockStatement | null;
}
{
type: "WhileStatement";
test: Expression;
body: Statement;
}
Syntax:
while(<test>){
<body>
}
{
type: "DoWhileStatement";
body: Statement;
test: Expression;
}
{
type: "ForStatement";
init: VariableDeclaration | Expression | null;
test: Expression | null;
update: Expression | null;
body: Statement;
}
Syntax:
for(<init>;<test>;<update>){
<body>
}
{
type: "ForInStatement";
left: VariableDeclaration | Expression;
right: Expression;
body: Statement;
each: boolean;
}
interface ForOfStatement <: Statement {
type: "ForOfStatement";
left: VariableDeclaration | Expression;
right: Expression;
body: Statement;
}
Any declaration node.
{
type: "FunctionDeclaration";
id: Identifier;
params: [ Pattern ];
defaults: [ Expression ];
rest: Identifier | null;
body: BlockStatement
}
{
type: "VariableDeclaration";
declarations: [ VariableDeclarator ];
kind: "var" | "let" | "const";
}
{
type: "VariableDeclarator";
id: Pattern;
init: Expression | null;
}
Any expression node.
{
type: "ThisExpression";
}
{
type: "ArrayExpression";
elements: [ Expression | null ];
}
A literal property in an object expression can have either a string or number as its value.
Paired with the property key init
{
type: "ObjectExpression";
properties: [ Property ];
}
Syntax:
var obj = <properties>
{
type: "FunctionExpression";
id: Identifier | null;
params: [ Pattern ];
defaults: [ Expression ];
rest: Identifier | null;
body: BlockStatement
}
{
type: "ArrowExpression";
params: [ Pattern ];
defaults: [ Expression ];
rest: Identifier | null;
body: BlockStatement | Expression;
generator: boolean;
expression: boolean;
}
{
type: "SequenceExpression";
expressions: [ Expression ];
}
{
type: "UnaryExpression";
operator: UnaryOperator;
prefix: boolean;
argument: Expression;
}
UnaryOperators: "-" | "+" | "!" | "~" | "typeof" | "void" | "delete"
{
type: "BinaryExpression";
operator: BinaryOperator;
left: Expression;
right: Expression;
}
Syntax:
(<left> <operator> <right>)
BinaryOperators: "==" | "!=" | "===" | "!==" | "<" | "<=" | ">" | ">=" | "<<" | ">>" | ">>>" | "+" | "-" | "*" | "/" | "%" | "&" | "|" | "^" | "in" | "instanceof"
{
type: "AssignmentExpression";
operator: AssignmentOperator;
left: Expression;
right: Expression;
}
Syntax:
(<left> <operator> <right>)
AssignmentOperators: "=" | "+=" | "-=" | "*=" | "/=" | "%=" | "<<=" | ">>=" | ">>>=" | "|=" | "^=" | "&="
An update (increment or decrement) operator expression. prefix === true if operator comes before the expression; prefix === false if operator comes after
{
type: "UpdateExpression";
operator: UpdateOperator;
argument: Expression;
prefix: boolean;
}
Syntax:
<operator><argument> | <argument><operator>
UpdateOperators: "++" | "--"
{
type: "LogicalExpression";
operator: LogicalOperator;
left: Expression;
right: Expression;
}
LogicalOperator: "||" | "&&"
Syntax:
(<left> <operator> <right>)
{
type: "ConditionalExpression";
test: Expression;
alternate: Expression;
consequent: Expression;
}
Syntax:
(<test>) ? <consequent> : <alternate>
{
type: "NewExpression";
callee: Expression;
arguments: [ Expression | null ];
}
{
type: "CallExpression";
callee: Expression;
arguments: [ Expression | null ];
}
Syntax:
<callee>(<arguments>)
{
type: "MemberExpression";
object: Expression;
property: Identifier | Expression;
computed: boolean;
}
A literal property in an object pattern can have either a string or number as its value.
{
type: "ObjectPattern";
properties: [ PropertyPattern ];
}
{
type: "PropertyPattern";
key: Literal | Identifier;
value: Pattern;
}
{
type: "ArrayPattern";
elements: [ Pattern | null ];
}
A case (if test is an Expression) or default (if test === null) clause in the body of a switch statement.
{
type: "SwitchCase";
test: Expression | null;
consequent: [ Statement ];
}
Syntax:
case <test>:
<consequent>
<type BreakStmt/ReturnStmt optional>
default:
<consequent>
{
type: "Identifier";
name: string;
}
A literal token. For number literal tokens, decimal numbers and scientific notation inputs are also accepted
{
type: "Literal";
value: string | boolean | null | number | RegExp;
}
{
type: "Property";
key: Literal | Identifier;
value: Expression;
kind: "init" | "get" | "set";
}
Syntax:
var obj2 = {
<key>:<value>
}