Skip to content

Instantly share code, notes, and snippets.

@popomore
Created February 27, 2014 08:36
Show Gist options
  • Save popomore/9246464 to your computer and use it in GitHub Desktop.
Save popomore/9246464 to your computer and use it in GitHub Desktop.
Ast node type parsed by esprima, install esprima first
var esprima = require('esprima');
var code = [];
// Literal
code.push('1;');
code.push('true;');
code.push('"a";');
code.push('/a/g;');
code.push('null;');
// Identifier
code.push('undefined;');
// AssignmentExpression in ExpressionStatement
code.push('a = 1;');
// NewExpression
code.push('new Array()');
// ArrayExpression
code.push('[1, 2]');
// CallExpression
code.push('fun()');
// ConditionalExpression
code.push('a ? 1 : 2;');
// LogicalExpression
code.push('a && b;');
// VariableDeclarator right is ObjectExpression
code.push('var a = {b: 1};');
// VariableDeclarator right is FunctionExpression
code.push('var a1 = function () {}');
// UpdateExpression
code.push('++a');
code.push('a++');
// UnaryExpression + - ~ ! delete void typeof
code.push('!a');
code.push('typeof a');
// SequenceExpression
// BinaryExpression
// VariableDeclaration
code.push('var a1;'); // VariableDeclarator
code.push('var a1 = 1;'); // VariableDeclarator
code.push(
'var a1 = 1,' + // VariableDeclarator
'a2 = 1;' // VariableDeclarator
);
// FunctionDeclaration
code.push('function a() {'); // BlockStatement in {}
code.push(' return;'); // ReturnStatement
code.push('}');
// ForStatement
code.push('for(var i = 0; i < l; i++) {}');
// ForInStatement
code.push('for (var i in obj) {');
code.push(' if (a) {'); // IfStatement
code.push(' break;'); // BreakStatement
code.push(' } else {');
code.push(' continue;'); // ContinueStatement
code.push(' }');
code.push('}');
// WhileStatement
code.push('while(a){}');
// DoWhileStatement
code.push('do{}while(a)');
// TryStatement
code.push('try{}');
// CatchClause
code.push('catch(e){}');
// DebuggerStatement
code.push('debugger');
// EmptyStatement
code.push(';');
// SwitchStatement
code.push('switch(a) {');
code.push(' case 1:'); // SwitchCase
code.push('}');
// ThrowStatement
code.push('throw "a"');
// WithStatement
code.push('with(o){}');
// BlockStatement
// LabeledStatement as key a
code.push('{a: 1}');
var ast = esprima.parse(code.join('\n'));
console.log(ast);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment