core.js.html 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: core.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: core.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 Collection = require('./Collection');
  30. var collections = require('./collections');
  31. var getParser = require('./getParser');
  32. var matchNode = require('./matchNode');
  33. var recast = require('recast');
  34. var template = require('./template');
  35. var Node = recast.types.namedTypes.Node;
  36. var NodePath = recast.types.NodePath;
  37. // Register all built-in collections
  38. for (var name in collections) {
  39. collections[name].register();
  40. }
  41. /**
  42. * Main entry point to the tool. The function accepts multiple different kinds
  43. * of arguments as a convenience. In particular the function accepts either
  44. *
  45. * - a string containing source code
  46. * The string is parsed with Recast
  47. * - a single AST node
  48. * - a single node path
  49. * - an array of nodes
  50. * - an array of node paths
  51. *
  52. * @exports jscodeshift
  53. * @param {Node|NodePath|Array|string} source
  54. * @param {Object} options Options to pass to Recast when passing source code
  55. * @return {Collection}
  56. */
  57. function core(source, options) {
  58. return typeof source === 'string' ?
  59. fromSource(source, options) :
  60. fromAST(source);
  61. }
  62. /**
  63. * Returns a collection from a node, node path, array of nodes or array of node
  64. * paths.
  65. *
  66. * @ignore
  67. * @param {Node|NodePath|Array} source
  68. * @return {Collection}
  69. */
  70. function fromAST(ast) {
  71. if (Array.isArray(ast)) {
  72. if (ast[0] instanceof NodePath || ast.length === 0) {
  73. return Collection.fromPaths(ast);
  74. } else if (Node.check(ast[0])) {
  75. return Collection.fromNodes(ast);
  76. }
  77. } else {
  78. if (ast instanceof NodePath) {
  79. return Collection.fromPaths([ast]);
  80. } else if (Node.check(ast)) {
  81. return Collection.fromNodes([ast]);
  82. }
  83. }
  84. throw new TypeError(
  85. 'Received an unexpected value ' + Object.prototype.toString.call(ast)
  86. );
  87. }
  88. function fromSource(source, options) {
  89. if (!options) {
  90. options = {};
  91. }
  92. if (!options.parser) {
  93. options.parser = getParser();
  94. }
  95. return fromAST(recast.parse(source, options));
  96. }
  97. /**
  98. * Utility function to match a node against a pattern.
  99. * @augments core
  100. * @static
  101. * @param {Node|NodePath|Object} path
  102. * @parma {Object} filter
  103. * @return boolean
  104. */
  105. function match(path, filter) {
  106. if (!(path instanceof NodePath)) {
  107. if (typeof path.get === 'function') {
  108. path = path.get();
  109. } else {
  110. path = {value: path};
  111. }
  112. }
  113. return matchNode(path.value, filter);
  114. }
  115. var plugins = [];
  116. /**
  117. * Utility function for registering plugins.
  118. *
  119. * Plugins are simple functions that are passed the core jscodeshift instance.
  120. * They should extend jscodeshift by calling `registerMethods`, etc.
  121. * This method guards against repeated registrations (the plugin callback will only be called once).
  122. *
  123. * @augments core
  124. * @static
  125. * @param {Function} plugin
  126. */
  127. function use(plugin) {
  128. if (plugins.indexOf(plugin) === -1) {
  129. plugins.push(plugin);
  130. plugin(core);
  131. }
  132. }
  133. /**
  134. * Returns a version of the core jscodeshift function "bound" to a specific
  135. * parser.
  136. *
  137. * @augments core
  138. * @static
  139. */
  140. function withParser(parser) {
  141. if (typeof parser === 'string') {
  142. parser = getParser(parser);
  143. }
  144. const newCore = function(source, options) {
  145. if (options &amp;&amp; !options.parser) {
  146. options.parser = parser;
  147. } else {
  148. options = {parser};
  149. }
  150. return core(source, options);
  151. };
  152. return enrichCore(newCore, parser);
  153. }
  154. /**
  155. * The ast-types library
  156. * @external astTypes
  157. * @see {@link https://github.com/benjamn/ast-types}
  158. */
  159. function enrichCore(core, parser) {
  160. // add builders and types to the function for simple access
  161. Object.assign(core, recast.types.namedTypes);
  162. Object.assign(core, recast.types.builders);
  163. core.registerMethods = Collection.registerMethods;
  164. /**
  165. * @augments core
  166. * @type external:astTypes
  167. */
  168. core.types = recast.types;
  169. core.match = match;
  170. core.template = template(parser);
  171. // add mappings and filters to function
  172. core.filters = {};
  173. core.mappings = {};
  174. for (var name in collections) {
  175. if (collections[name].filters) {
  176. core.filters[name] = collections[name].filters;
  177. }
  178. if (collections[name].mappings) {
  179. core.mappings[name] = collections[name].mappings;
  180. }
  181. }
  182. core.use = use;
  183. core.withParser = withParser;
  184. return core;
  185. }
  186. module.exports = enrichCore(core, getParser());
  187. </code></pre>
  188. </article>
  189. </section>
  190. </div>
  191. <nav>
  192. <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>
  193. </nav>
  194. <br class="clear">
  195. <footer>
  196. 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)
  197. </footer>
  198. <script> prettyPrint(); </script>
  199. <script src="scripts/linenumber.js"> </script>
  200. </body>
  201. </html>