123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <title>JSDoc: Source: core.js</title>
- <script src="scripts/prettify/prettify.js"> </script>
- <script src="scripts/prettify/lang-css.js"> </script>
- <!--[if lt IE 9]>
- <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
- <![endif]-->
- <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
- <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
- </head>
- <body>
- <div id="main">
- <h1 class="page-title">Source: core.js</h1>
-
-
- <section>
- <article>
- <pre class="prettyprint source linenums"><code>/*
- * Copyright (c) 2015-present, Facebook, Inc.
- * All rights reserved.
- *
- * This source code is licensed under the BSD-style license found in the
- * LICENSE file in the root directory of this source tree. An additional grant
- * of patent rights can be found in the PATENTS file in the same directory.
- *
- */
- 'use strict';
- var Collection = require('./Collection');
- var collections = require('./collections');
- var getParser = require('./getParser');
- var matchNode = require('./matchNode');
- var recast = require('recast');
- var template = require('./template');
- var Node = recast.types.namedTypes.Node;
- var NodePath = recast.types.NodePath;
- // Register all built-in collections
- for (var name in collections) {
- collections[name].register();
- }
- /**
- * Main entry point to the tool. The function accepts multiple different kinds
- * of arguments as a convenience. In particular the function accepts either
- *
- * - a string containing source code
- * The string is parsed with Recast
- * - a single AST node
- * - a single node path
- * - an array of nodes
- * - an array of node paths
- *
- * @exports jscodeshift
- * @param {Node|NodePath|Array|string} source
- * @param {Object} options Options to pass to Recast when passing source code
- * @return {Collection}
- */
- function core(source, options) {
- return typeof source === 'string' ?
- fromSource(source, options) :
- fromAST(source);
- }
- /**
- * Returns a collection from a node, node path, array of nodes or array of node
- * paths.
- *
- * @ignore
- * @param {Node|NodePath|Array} source
- * @return {Collection}
- */
- function fromAST(ast) {
- if (Array.isArray(ast)) {
- if (ast[0] instanceof NodePath || ast.length === 0) {
- return Collection.fromPaths(ast);
- } else if (Node.check(ast[0])) {
- return Collection.fromNodes(ast);
- }
- } else {
- if (ast instanceof NodePath) {
- return Collection.fromPaths([ast]);
- } else if (Node.check(ast)) {
- return Collection.fromNodes([ast]);
- }
- }
- throw new TypeError(
- 'Received an unexpected value ' + Object.prototype.toString.call(ast)
- );
- }
- function fromSource(source, options) {
- if (!options) {
- options = {};
- }
- if (!options.parser) {
- options.parser = getParser();
- }
- return fromAST(recast.parse(source, options));
- }
- /**
- * Utility function to match a node against a pattern.
- * @augments core
- * @static
- * @param {Node|NodePath|Object} path
- * @parma {Object} filter
- * @return boolean
- */
- function match(path, filter) {
- if (!(path instanceof NodePath)) {
- if (typeof path.get === 'function') {
- path = path.get();
- } else {
- path = {value: path};
- }
- }
- return matchNode(path.value, filter);
- }
- var plugins = [];
- /**
- * Utility function for registering plugins.
- *
- * Plugins are simple functions that are passed the core jscodeshift instance.
- * They should extend jscodeshift by calling `registerMethods`, etc.
- * This method guards against repeated registrations (the plugin callback will only be called once).
- *
- * @augments core
- * @static
- * @param {Function} plugin
- */
- function use(plugin) {
- if (plugins.indexOf(plugin) === -1) {
- plugins.push(plugin);
- plugin(core);
- }
- }
- /**
- * Returns a version of the core jscodeshift function "bound" to a specific
- * parser.
- *
- * @augments core
- * @static
- */
- function withParser(parser) {
- if (typeof parser === 'string') {
- parser = getParser(parser);
- }
- const newCore = function(source, options) {
- if (options && !options.parser) {
- options.parser = parser;
- } else {
- options = {parser};
- }
- return core(source, options);
- };
- return enrichCore(newCore, parser);
- }
- /**
- * The ast-types library
- * @external astTypes
- * @see {@link https://github.com/benjamn/ast-types}
- */
- function enrichCore(core, parser) {
- // add builders and types to the function for simple access
- Object.assign(core, recast.types.namedTypes);
- Object.assign(core, recast.types.builders);
- core.registerMethods = Collection.registerMethods;
- /**
- * @augments core
- * @type external:astTypes
- */
- core.types = recast.types;
- core.match = match;
- core.template = template(parser);
- // add mappings and filters to function
- core.filters = {};
- core.mappings = {};
- for (var name in collections) {
- if (collections[name].filters) {
- core.filters[name] = collections[name].filters;
- }
- if (collections[name].mappings) {
- core.mappings[name] = collections[name].mappings;
- }
- }
- core.use = use;
- core.withParser = withParser;
- return core;
- }
- module.exports = enrichCore(core, getParser());
- </code></pre>
- </article>
- </section>
- </div>
- <nav>
- <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>
- </nav>
- <br class="clear">
- <footer>
- 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)
- </footer>
- <script> prettyPrint(); </script>
- <script src="scripts/linenumber.js"> </script>
- </body>
- </html>
|