项目作者: estools

项目描述 :
AST recursive visitor
高级语言: JavaScript
项目地址: git://github.com/estools/esrecurse.git
创建时间: 2014-12-02T11:38:00Z
项目社区:https://github.com/estools/esrecurse

开源协议:

下载


Esrecurse Build Status

Esrecurse (esrecurse) is
ECMAScript
recursive traversing functionality.

Example Usage

The following code will output all variables declared at the root of a file.

  1. esrecurse.visit(ast, {
  2. XXXStatement: function (node) {
  3. this.visit(node.left);
  4. // do something...
  5. this.visit(node.right);
  6. }
  7. });

We can use Visitor instance.

  1. var visitor = new esrecurse.Visitor({
  2. XXXStatement: function (node) {
  3. this.visit(node.left);
  4. // do something...
  5. this.visit(node.right);
  6. }
  7. });
  8. visitor.visit(ast);

We can inherit Visitor instance easily.

  1. class Derived extends esrecurse.Visitor {
  2. constructor()
  3. {
  4. super(null);
  5. }
  6. XXXStatement(node) {
  7. }
  8. }
  1. function DerivedVisitor() {
  2. esrecurse.Visitor.call(/* this for constructor */ this /* visitor object automatically becomes this. */);
  3. }
  4. util.inherits(DerivedVisitor, esrecurse.Visitor);
  5. DerivedVisitor.prototype.XXXStatement = function (node) {
  6. this.visit(node.left);
  7. // do something...
  8. this.visit(node.right);
  9. };

And you can invoke default visiting operation inside custom visit operation.

  1. function DerivedVisitor() {
  2. esrecurse.Visitor.call(/* this for constructor */ this /* visitor object automatically becomes this. */);
  3. }
  4. util.inherits(DerivedVisitor, esrecurse.Visitor);
  5. DerivedVisitor.prototype.XXXStatement = function (node) {
  6. // do something...
  7. this.visitChildren(node);
  8. };

The childVisitorKeys option does customize the behaviour of this.visitChildren(node).
We can use user-defined node types.

  1. // This tree contains a user-defined `TestExpression` node.
  2. var tree = {
  3. type: 'TestExpression',
  4. // This 'argument' is the property containing the other **node**.
  5. argument: {
  6. type: 'Literal',
  7. value: 20
  8. },
  9. // This 'extended' is the property not containing the other **node**.
  10. extended: true
  11. };
  12. esrecurse.visit(
  13. ast,
  14. {
  15. Literal: function (node) {
  16. // do something...
  17. }
  18. },
  19. {
  20. // Extending the existing traversing rules.
  21. childVisitorKeys: {
  22. // TargetNodeName: [ 'keys', 'containing', 'the', 'other', '**node**' ]
  23. TestExpression: ['argument']
  24. }
  25. }
  26. );

We can use the fallback option as well.
If the fallback option is "iteration", esrecurse would visit all enumerable properties of unknown nodes.
Please note circular references cause the stack overflow. AST might have circular references in additional properties for some purpose (e.g. node.parent).

  1. esrecurse.visit(
  2. ast,
  3. {
  4. Literal: function (node) {
  5. // do something...
  6. }
  7. },
  8. {
  9. fallback: 'iteration'
  10. }
  11. );

If the fallback option is a function, esrecurse calls this function to determine the enumerable properties of unknown nodes.
Please note circular references cause the stack overflow. AST might have circular references in additional properties for some purpose (e.g. node.parent).

  1. esrecurse.visit(
  2. ast,
  3. {
  4. Literal: function (node) {
  5. // do something...
  6. }
  7. },
  8. {
  9. fallback: function (node) {
  10. return Object.keys(node).filter(function(key) {
  11. return key !== 'argument'
  12. });
  13. }
  14. }
  15. );

License

See LICENSE.md.