0.1.1 • Published 9 years ago

esvisit v0.1.1

Weekly downloads
4
License
MIT
Repository
github
Last release
9 years ago

Esvisit

Esvisit is a simple npm module for visiting JavaScript abstract syntax trees that are compatible with Mozilla parser API as described in https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/Parser_API#Programs. This module supports exclusvely ECMAScript5 as described in http://www.ecma-international.org/ecma-262/5.1/. Benefits of using Esvisit includes:

  • Process iteratively and avoid recursion.
  • Get an alternative type that better reflects JavaScript semantic.
  • Visit only actual statements and expressions.

Demonstration

var Esvisit = require('esvisit');
var Esprima = require('esprima');

function indent () { return (new Array(depth+1)).join("  ") }
function visitStatement (type, stmt) { visit('Statement', type, stmt) }
function visitExpression (type, expr) { visit('Expression', type, expr) }
function visit (kind, type, node) {
  var id = ++counter;
  console.log(indent()+'Begin'+kind+id+': '+type+'('+Esvisit.ExtractInformation(type, node).join(", ")+')')
  depth++
  push(function () {
    depth--
    console.log(indent()+'End'+id)
  })
}

var code = 'o.a = eval("2*"+x);' // Your JS code here...
var ast = Esprima.parse(code)
var counter = 0;
var depth = 0;
var push = Esvisit.Prepare(visitStatement, visitExpression)

push(ast)

Output:

BeginStatement1: Expression()
  BeginExpression2: MemberAssignment(a, =)
    BeginExpression3: EvalCall(1)
      BeginExpression4: Binary(+)
        BeginExpression5: Literal(2*)
        End5
        BeginExpression6: Identifier(x)
        End6
      End4
    End3
    BeginExpression7: Identifier(o)
    End7
  End2
End1

API

This module exposes four functions:

  • Prepare(StatementCallback, ExpressionCallBack): prepare upcoming visits ; returns a function to push something on the worker list. Callbacks are called with: the Esvisit type of the current node and the current node itself. Esvisit types are described in the sections 'Statement Types' and 'Expression Types'
  • ExtractInformation(EsvisitType, Node)
  • ExtractStatementInformation(EsvisitType, Statement)
  • ExtractExpressionInformation(EsvisitType, Expression)

Statement Types

TypeInterpretationInformation
Emptyalias for EmptyStatement[]
Strict'use strict';[]
Block*[Length]
Expression*[]
If*[HasAlternate]
Label*[Label]
Break*[MaybeLabel]
Continue*[MaybeLabel]
With*[]
Switch*[Cases::[IsDefault, Length]]
Return*[HasValue]
Throw*[]
Try*[BodyLength, MaybeCatchParameter, MaybeCatchLength, MaybeFinalizerLength]
While*[]
DoWhile*[]
DeclarationForfor (VAR_DECL; EXPR; EXPR) STMT[Declarators::[Name, HasInit], HasTest, HasUpdate]
Forfor (EXPR; EXPR; EXPR) STMT[HasInit, HasTest, HasUpdate]
IdentifierForInfor (ID in EXPR) STMT[Name]
MemberForInfor (MEMBER in EXPR) STMT[MaybeProperty]
DeclarationForInfor (var ID [=EXPR] in EXPR) STMT[Name, HasInit]
Definitionalias for FunctionDeclaration[Name, Parameters::[Name], BodyLength]
Declarationalias for VariableDeclaration[Declarators::[Name, HasInit]]

(*): Add Statement to the type to obtain the Mozilla alias.

Expression Types

TypeInterpretationInformation
Thisalias for ThisExpression[]
Array**[Elements::[IsInitialized]]
Object**[Properties::[KeyValue, Kind, MaybeParameter, MaybeBodyLength]]
Function**[MaybeName, Parameters::[Name], BodyLength]
Sequence**[Length]
IdentifierTypeoftypeof ID[Name]
IdentifierDeletedelete ID[Name]
MemberDeletedelete MEMBER[MaybeProperty]
Unary**[Operator]
Binary**[Operator]
IdentifierAssignmentID ASS_OP EXPR[Name, Operator]
MemberAssignmentMEMBER ASS_OP EXPR[MaybeProperty, Operator]
IdentifierUpdate++ID, --ID, ID++, ID--[IsPrefix, Operator, Name]
MemberUpdate++MEMBER, --MEMBER, MEMBER++, MEMBER--[IsPrefix, Operator, MaybeProperty]
Logical**[Operator]
Conditional**[]
New**[ArgumentsLength]
MemberCallMEMBER(EXPRS)[MaybeProperty, ArgumentsLength]
EvalCalleval(EXPRS)[ArgumentsLength]
CallEXPR(EXPRS)[ArgumentsLength]
Member**[MaybeProperty]
Identifieralias for Identifier[Name]
Literalalias for Literal[Value]

(**): Add Expression to the type to obtain the Mozilla alias.

0.1.1

9 years ago

0.1.0

9 years ago

0.0.6

9 years ago

0.0.4

9 years ago

0.0.3

9 years ago

0.0.2

9 years ago

0.0.1

9 years ago

0.0.0

9 years ago