collections_VariableDeclarator.js.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: collections/VariableDeclarator.js</title>
  6. <script src="scripts/prettify/prettify.js"> </script>
  7. <script src="scripts/prettify/lang-css.js"> </script>
  8. <!--[if lt IE 9]>
  9. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  10. <![endif]-->
  11. <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
  12. <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
  13. </head>
  14. <body>
  15. <div id="main">
  16. <h1 class="page-title">Source: collections/VariableDeclarator.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>/*
  20. * Copyright (c) 2015-present, Facebook, Inc.
  21. * All rights reserved.
  22. *
  23. * This source code is licensed under the BSD-style license found in the
  24. * LICENSE file in the root directory of this source tree. An additional grant
  25. * of patent rights can be found in the PATENTS file in the same directory.
  26. *
  27. */
  28. 'use strict';
  29. var _ = require('lodash');
  30. var Collection = require('../Collection');
  31. var NodeCollection = require('./Node');
  32. var matchNode = require('../matchNode');
  33. var recast = require('recast');
  34. var astNodesAreEquivalent = recast.types.astNodesAreEquivalent;
  35. var b = recast.types.builders;
  36. var types = recast.types.namedTypes;
  37. var VariableDeclarator = recast.types.namedTypes.VariableDeclarator;
  38. /**
  39. * @mixin
  40. */
  41. var globalMethods = {
  42. /**
  43. * Finds all variable declarators, optionally filtered by name.
  44. *
  45. * @param {string} name
  46. * @return {Collection}
  47. */
  48. findVariableDeclarators: function(name) {
  49. var filter = name ? {id: {name: name}} : null;
  50. return this.find(VariableDeclarator, filter);
  51. }
  52. };
  53. var filterMethods = {
  54. /**
  55. * Returns a function that returns true if the provided path is a variable
  56. * declarator and requires one of the specified module names.
  57. *
  58. * @param {string|Array} names A module name or an array of module names
  59. * @return {Function}
  60. */
  61. requiresModule: function(names) {
  62. if (names &amp;&amp; !Array.isArray(names)) {
  63. names = [names];
  64. }
  65. var requireIdentifier = b.identifier('require');
  66. return function(path) {
  67. var node = path.value;
  68. if (!VariableDeclarator.check(node) ||
  69. !types.CallExpression.check(node.init) ||
  70. !astNodesAreEquivalent(node.init.callee, requireIdentifier)) {
  71. return false;
  72. }
  73. return !names ||
  74. names.some(
  75. n => astNodesAreEquivalent(node.init.arguments[0], b.literal(n))
  76. );
  77. };
  78. }
  79. };
  80. /**
  81. * @mixin
  82. */
  83. var transformMethods = {
  84. /**
  85. * Renames a variable and all its occurrences.
  86. *
  87. * @param {string} newName
  88. * @return {Collection}
  89. */
  90. renameTo: function(newName) {
  91. // TODO: Include JSXElements
  92. return this.forEach(function(path) {
  93. var node = path.value;
  94. var oldName = node.id.name;
  95. var rootScope = path.scope;
  96. var rootPath = rootScope.path;
  97. Collection.fromPaths([rootPath])
  98. .find(types.Identifier, {name: oldName})
  99. .filter(function(path) { // ignore non-variables
  100. var parent = path.parent.node;
  101. if (
  102. types.MemberExpression.check(parent) &amp;&amp;
  103. parent.property === path.node &amp;&amp;
  104. !parent.computed
  105. ) {
  106. // obj.oldName
  107. return false;
  108. }
  109. if (
  110. types.Property.check(parent) &amp;&amp;
  111. parent.key === path.node &amp;&amp;
  112. !parent.computed
  113. ) {
  114. // { oldName: 3 }
  115. return false;
  116. }
  117. if (
  118. types.MethodDefinition.check(parent) &amp;&amp;
  119. parent.key === path.node &amp;&amp;
  120. !parent.computed
  121. ) {
  122. // class A() { oldName() {} }
  123. return false;
  124. }
  125. return true;
  126. })
  127. .forEach(function(path) {
  128. var scope = path.scope;
  129. while (scope &amp;&amp; scope !== rootScope) {
  130. if (scope.declares(oldName)) {
  131. return;
  132. }
  133. scope = scope.parent;
  134. }
  135. if (scope) { // identifier must refer to declared variable
  136. path.get('name').replace(newName);
  137. }
  138. });
  139. });
  140. }
  141. };
  142. function register() {
  143. NodeCollection.register();
  144. Collection.registerMethods(globalMethods);
  145. Collection.registerMethods(transformMethods, VariableDeclarator);
  146. }
  147. exports.register = _.once(register);
  148. exports.filters = filterMethods;
  149. </code></pre>
  150. </article>
  151. </section>
  152. </div>
  153. <nav>
  154. <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-jscodeshift.html">jscodeshift</a></li></ul><h3>Externals</h3><ul><li><a href="external-astTypes.html">astTypes</a></li></ul><h3>Classes</h3><ul><li><a href="Collection.html">Collection</a></li></ul><h3>Mixins</h3><ul><li><a href="globalMethods.html">globalMethods</a></li><li><a href="mutationMethods.html">mutationMethods</a></li><li><a href="transformMethods.html">transformMethods</a></li><li><a href="traversalMethods.html">traversalMethods</a></li></ul><h3>Global</h3><ul><li><a href="global.html#registerMethods">registerMethods</a></li></ul>
  155. </nav>
  156. <br class="clear">
  157. <footer>
  158. Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.1</a> on Wed Sep 21 2016 16:53:09 GMT-0400 (EDT)
  159. </footer>
  160. <script> prettyPrint(); </script>
  161. <script src="scripts/linenumber.js"> </script>
  162. </body>
  163. </html>