You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
4.8 KiB

4 years ago
  1. ### Estraverse [![Build Status](https://secure.travis-ci.org/estools/estraverse.svg)](http://travis-ci.org/estools/estraverse)
  2. Estraverse ([estraverse](http://github.com/estools/estraverse)) is
  3. [ECMAScript](http://www.ecma-international.org/publications/standards/Ecma-262.htm)
  4. traversal functions from [esmangle project](http://github.com/estools/esmangle).
  5. ### Documentation
  6. You can find usage docs at [wiki page](https://github.com/estools/estraverse/wiki/Usage).
  7. ### Example Usage
  8. The following code will output all variables declared at the root of a file.
  9. ```javascript
  10. estraverse.traverse(ast, {
  11. enter: function (node, parent) {
  12. if (node.type == 'FunctionExpression' || node.type == 'FunctionDeclaration')
  13. return estraverse.VisitorOption.Skip;
  14. },
  15. leave: function (node, parent) {
  16. if (node.type == 'VariableDeclarator')
  17. console.log(node.id.name);
  18. }
  19. });
  20. ```
  21. We can use `this.skip`, `this.remove` and `this.break` functions instead of using Skip, Remove and Break.
  22. ```javascript
  23. estraverse.traverse(ast, {
  24. enter: function (node) {
  25. this.break();
  26. }
  27. });
  28. ```
  29. And estraverse provides `estraverse.replace` function. When returning node from `enter`/`leave`, current node is replaced with it.
  30. ```javascript
  31. result = estraverse.replace(tree, {
  32. enter: function (node) {
  33. // Replace it with replaced.
  34. if (node.type === 'Literal')
  35. return replaced;
  36. }
  37. });
  38. ```
  39. By passing `visitor.keys` mapping, we can extend estraverse traversing functionality.
  40. ```javascript
  41. // This tree contains a user-defined `TestExpression` node.
  42. var tree = {
  43. type: 'TestExpression',
  44. // This 'argument' is the property containing the other **node**.
  45. argument: {
  46. type: 'Literal',
  47. value: 20
  48. },
  49. // This 'extended' is the property not containing the other **node**.
  50. extended: true
  51. };
  52. estraverse.traverse(tree, {
  53. enter: function (node) { },
  54. // Extending the existing traversing rules.
  55. keys: {
  56. // TargetNodeName: [ 'keys', 'containing', 'the', 'other', '**node**' ]
  57. TestExpression: ['argument']
  58. }
  59. });
  60. ```
  61. By passing `visitor.fallback` option, we can control the behavior when encountering unknown nodes.
  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. estraverse.traverse(tree, {
  75. enter: function (node) { },
  76. // Iterating the child **nodes** of unknown nodes.
  77. fallback: 'iteration'
  78. });
  79. ```
  80. When `visitor.fallback` is a function, we can determine which keys to visit on each node.
  81. ```javascript
  82. // This tree contains a user-defined `TestExpression` node.
  83. var tree = {
  84. type: 'TestExpression',
  85. // This 'argument' is the property containing the other **node**.
  86. argument: {
  87. type: 'Literal',
  88. value: 20
  89. },
  90. // This 'extended' is the property not containing the other **node**.
  91. extended: true
  92. };
  93. estraverse.traverse(tree, {
  94. enter: function (node) { },
  95. // Skip the `argument` property of each node
  96. fallback: function(node) {
  97. return Object.keys(node).filter(function(key) {
  98. return key !== 'argument';
  99. });
  100. }
  101. });
  102. ```
  103. ### License
  104. Copyright (C) 2012-2016 [Yusuke Suzuki](http://github.com/Constellation)
  105. (twitter: [@Constellation](http://twitter.com/Constellation)) and other contributors.
  106. Redistribution and use in source and binary forms, with or without
  107. modification, are permitted provided that the following conditions are met:
  108. * Redistributions of source code must retain the above copyright
  109. notice, this list of conditions and the following disclaimer.
  110. * Redistributions in binary form must reproduce the above copyright
  111. notice, this list of conditions and the following disclaimer in the
  112. documentation and/or other materials provided with the distribution.
  113. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  114. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  115. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  116. ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  117. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  118. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  119. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  120. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  121. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  122. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.