pattern-visitor.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. Copyright (C) 2015 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. import estraverse from "estraverse";
  22. import esrecurse from "esrecurse";
  23. const { Syntax } = estraverse;
  24. /**
  25. * Get last array element
  26. * @param {Array} xs array
  27. * @returns {any} Last elment
  28. */
  29. function getLast(xs) {
  30. return xs.at(-1) || null;
  31. }
  32. /**
  33. * Visitor for destructuring patterns.
  34. */
  35. class PatternVisitor extends esrecurse.Visitor {
  36. static isPattern(node) {
  37. const nodeType = node.type;
  38. return (
  39. nodeType === Syntax.Identifier ||
  40. nodeType === Syntax.ObjectPattern ||
  41. nodeType === Syntax.ArrayPattern ||
  42. nodeType === Syntax.SpreadElement ||
  43. nodeType === Syntax.RestElement ||
  44. nodeType === Syntax.AssignmentPattern
  45. );
  46. }
  47. constructor(options, rootPattern, callback) {
  48. super(null, options);
  49. this.rootPattern = rootPattern;
  50. this.callback = callback;
  51. this.assignments = [];
  52. this.rightHandNodes = [];
  53. this.restElements = [];
  54. }
  55. Identifier(pattern) {
  56. const lastRestElement = getLast(this.restElements);
  57. this.callback(pattern, {
  58. topLevel: pattern === this.rootPattern,
  59. rest: lastRestElement !== null && lastRestElement !== void 0 && lastRestElement.argument === pattern,
  60. assignments: this.assignments
  61. });
  62. }
  63. Property(property) {
  64. // Computed property's key is a right hand node.
  65. if (property.computed) {
  66. this.rightHandNodes.push(property.key);
  67. }
  68. // If it's shorthand, its key is same as its value.
  69. // If it's shorthand and has its default value, its key is same as its value.left (the value is AssignmentPattern).
  70. // If it's not shorthand, the name of new variable is its value's.
  71. this.visit(property.value);
  72. }
  73. ArrayPattern(pattern) {
  74. for (let i = 0, iz = pattern.elements.length; i < iz; ++i) {
  75. const element = pattern.elements[i];
  76. this.visit(element);
  77. }
  78. }
  79. AssignmentPattern(pattern) {
  80. this.assignments.push(pattern);
  81. this.visit(pattern.left);
  82. this.rightHandNodes.push(pattern.right);
  83. this.assignments.pop();
  84. }
  85. RestElement(pattern) {
  86. this.restElements.push(pattern);
  87. this.visit(pattern.argument);
  88. this.restElements.pop();
  89. }
  90. MemberExpression(node) {
  91. // Computed property's key is a right hand node.
  92. if (node.computed) {
  93. this.rightHandNodes.push(node.property);
  94. }
  95. // the object is only read, write to its property.
  96. this.rightHandNodes.push(node.object);
  97. }
  98. //
  99. // ForInStatement.left and AssignmentExpression.left are LeftHandSideExpression.
  100. // By spec, LeftHandSideExpression is Pattern or MemberExpression.
  101. // (see also: https://github.com/estree/estree/pull/20#issuecomment-74584758)
  102. // But espree 2.0 parses to ArrayExpression, ObjectExpression, etc...
  103. //
  104. SpreadElement(node) {
  105. this.visit(node.argument);
  106. }
  107. ArrayExpression(node) {
  108. node.elements.forEach(this.visit, this);
  109. }
  110. AssignmentExpression(node) {
  111. this.assignments.push(node);
  112. this.visit(node.left);
  113. this.rightHandNodes.push(node.right);
  114. this.assignments.pop();
  115. }
  116. CallExpression(node) {
  117. // arguments are right hand nodes.
  118. node.arguments.forEach(a => {
  119. this.rightHandNodes.push(a);
  120. });
  121. this.visit(node.callee);
  122. }
  123. }
  124. export default PatternVisitor;
  125. /* vim: set sw=4 ts=4 et tw=80 : */