ast.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. Copyright (C) 2013 Yusuke Suzuki <utatane.tea@gmail.com>
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. * Redistributions of source code must retain the above copyright
  6. notice, this list of conditions and the following disclaimer.
  7. * Redistributions in binary form must reproduce the above copyright
  8. notice, this list of conditions and the following disclaimer in the
  9. documentation and/or other materials provided with the distribution.
  10. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS'
  11. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  12. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  13. ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  14. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  15. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  16. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  17. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  18. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  19. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  20. */
  21. (function () {
  22. 'use strict';
  23. function isExpression(node) {
  24. if (node == null) { return false; }
  25. switch (node.type) {
  26. case 'ArrayExpression':
  27. case 'AssignmentExpression':
  28. case 'BinaryExpression':
  29. case 'CallExpression':
  30. case 'ConditionalExpression':
  31. case 'FunctionExpression':
  32. case 'Identifier':
  33. case 'Literal':
  34. case 'LogicalExpression':
  35. case 'MemberExpression':
  36. case 'NewExpression':
  37. case 'ObjectExpression':
  38. case 'SequenceExpression':
  39. case 'ThisExpression':
  40. case 'UnaryExpression':
  41. case 'UpdateExpression':
  42. return true;
  43. }
  44. return false;
  45. }
  46. function isIterationStatement(node) {
  47. if (node == null) { return false; }
  48. switch (node.type) {
  49. case 'DoWhileStatement':
  50. case 'ForInStatement':
  51. case 'ForStatement':
  52. case 'WhileStatement':
  53. return true;
  54. }
  55. return false;
  56. }
  57. function isStatement(node) {
  58. if (node == null) { return false; }
  59. switch (node.type) {
  60. case 'BlockStatement':
  61. case 'BreakStatement':
  62. case 'ContinueStatement':
  63. case 'DebuggerStatement':
  64. case 'DoWhileStatement':
  65. case 'EmptyStatement':
  66. case 'ExpressionStatement':
  67. case 'ForInStatement':
  68. case 'ForStatement':
  69. case 'IfStatement':
  70. case 'LabeledStatement':
  71. case 'ReturnStatement':
  72. case 'SwitchStatement':
  73. case 'ThrowStatement':
  74. case 'TryStatement':
  75. case 'VariableDeclaration':
  76. case 'WhileStatement':
  77. case 'WithStatement':
  78. return true;
  79. }
  80. return false;
  81. }
  82. function isSourceElement(node) {
  83. return isStatement(node) || node != null && node.type === 'FunctionDeclaration';
  84. }
  85. function trailingStatement(node) {
  86. switch (node.type) {
  87. case 'IfStatement':
  88. if (node.alternate != null) {
  89. return node.alternate;
  90. }
  91. return node.consequent;
  92. case 'LabeledStatement':
  93. case 'ForStatement':
  94. case 'ForInStatement':
  95. case 'WhileStatement':
  96. case 'WithStatement':
  97. return node.body;
  98. }
  99. return null;
  100. }
  101. function isProblematicIfStatement(node) {
  102. var current;
  103. if (node.type !== 'IfStatement') {
  104. return false;
  105. }
  106. if (node.alternate == null) {
  107. return false;
  108. }
  109. current = node.consequent;
  110. do {
  111. if (current.type === 'IfStatement') {
  112. if (current.alternate == null) {
  113. return true;
  114. }
  115. }
  116. current = trailingStatement(current);
  117. } while (current);
  118. return false;
  119. }
  120. module.exports = {
  121. isExpression: isExpression,
  122. isStatement: isStatement,
  123. isIterationStatement: isIterationStatement,
  124. isSourceElement: isSourceElement,
  125. isProblematicIfStatement: isProblematicIfStatement,
  126. trailingStatement: trailingStatement
  127. };
  128. }());
  129. /* vim: set sw=4 ts=4 et tw=80 : */