Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

171 linhas
5.0 KiB

há 3 anos
  1. ### Esrecurse [![Build Status](https://travis-ci.org/estools/esrecurse.svg?branch=master)](https://travis-ci.org/estools/esrecurse)
  2. Esrecurse ([esrecurse](https://github.com/estools/esrecurse)) is
  3. [ECMAScript](https://www.ecma-international.org/publications/standards/Ecma-262.htm)
  4. recursive traversing functionality.
  5. ### Example Usage
  6. The following code will output all variables declared at the root of a file.
  7. ```javascript
  8. esrecurse.visit(ast, {
  9. XXXStatement: function (node) {
  10. this.visit(node.left);
  11. // do something...
  12. this.visit(node.right);
  13. }
  14. });
  15. ```
  16. We can use `Visitor` instance.
  17. ```javascript
  18. var visitor = new esrecurse.Visitor({
  19. XXXStatement: function (node) {
  20. this.visit(node.left);
  21. // do something...
  22. this.visit(node.right);
  23. }
  24. });
  25. visitor.visit(ast);
  26. ```
  27. We can inherit `Visitor` instance easily.
  28. ```javascript
  29. class Derived extends esrecurse.Visitor {
  30. constructor()
  31. {
  32. super(null);
  33. }
  34. XXXStatement(node) {
  35. }
  36. }
  37. ```
  38. ```javascript
  39. function DerivedVisitor() {
  40. esrecurse.Visitor.call(/* this for constructor */ this /* visitor object automatically becomes this. */);
  41. }
  42. util.inherits(DerivedVisitor, esrecurse.Visitor);
  43. DerivedVisitor.prototype.XXXStatement = function (node) {
  44. this.visit(node.left);
  45. // do something...
  46. this.visit(node.right);
  47. };
  48. ```
  49. And you can invoke default visiting operation inside custom visit operation.
  50. ```javascript
  51. function DerivedVisitor() {
  52. esrecurse.Visitor.call(/* this for constructor */ this /* visitor object automatically becomes this. */);
  53. }
  54. util.inherits(DerivedVisitor, esrecurse.Visitor);
  55. DerivedVisitor.prototype.XXXStatement = function (node) {
  56. // do something...
  57. this.visitChildren(node);
  58. };
  59. ```
  60. The `childVisitorKeys` option does customize the behaviour of `this.visitChildren(node)`.
  61. We can use user-defined node types.
  62. ```javascript
  63. // This tree contains a user-defined `TestExpression` node.
  64. var tree = {
  65. type: 'TestExpression',
  66. // This 'argument' is the property containing the other **node**.
  67. argument: {
  68. type: 'Literal',
  69. value: 20
  70. },
  71. // This 'extended' is the property not containing the other **node**.
  72. extended: true
  73. };
  74. esrecurse.visit(
  75. ast,
  76. {
  77. Literal: function (node) {
  78. // do something...
  79. }
  80. },
  81. {
  82. // Extending the existing traversing rules.
  83. childVisitorKeys: {
  84. // TargetNodeName: [ 'keys', 'containing', 'the', 'other', '**node**' ]
  85. TestExpression: ['argument']
  86. }
  87. }
  88. );
  89. ```
  90. We can use the `fallback` option as well.
  91. If the `fallback` option is `"iteration"`, `esrecurse` would visit all enumerable properties of unknown nodes.
  92. Please note circular references cause the stack overflow. AST might have circular references in additional properties for some purpose (e.g. `node.parent`).
  93. ```javascript
  94. esrecurse.visit(
  95. ast,
  96. {
  97. Literal: function (node) {
  98. // do something...
  99. }
  100. },
  101. {
  102. fallback: 'iteration'
  103. }
  104. );
  105. ```
  106. If the `fallback` option is a function, `esrecurse` calls this function to determine the enumerable properties of unknown nodes.
  107. Please note circular references cause the stack overflow. AST might have circular references in additional properties for some purpose (e.g. `node.parent`).
  108. ```javascript
  109. esrecurse.visit(
  110. ast,
  111. {
  112. Literal: function (node) {
  113. // do something...
  114. }
  115. },
  116. {
  117. fallback: function (node) {
  118. return Object.keys(node).filter(function(key) {
  119. return key !== 'argument'
  120. });
  121. }
  122. }
  123. );
  124. ```
  125. ### License
  126. Copyright (C) 2014 [Yusuke Suzuki](https://github.com/Constellation)
  127. (twitter: [@Constellation](https://twitter.com/Constellation)) and other contributors.
  128. Redistribution and use in source and binary forms, with or without
  129. modification, are permitted provided that the following conditions are met:
  130. * Redistributions of source code must retain the above copyright
  131. notice, this list of conditions and the following disclaimer.
  132. * Redistributions in binary form must reproduce the above copyright
  133. notice, this list of conditions and the following disclaimer in the
  134. documentation and/or other materials provided with the distribution.
  135. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  136. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  137. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  138. ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  139. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  140. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  141. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  142. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  143. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  144. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.