pattern-visitor.js 4.8 KB

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