How to use the recast.types function in recast

To help you get started, we’ve selected a few recast examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github eveningkid / reacto / src / lib / jscodeshift / src / template.js View on Github external
/*
 *  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';

const recast = require('recast');

const builders = recast.types.builders;
const types = recast.types.namedTypes;

function splice(arr, element, replacement) {
  arr.splice.apply(arr, [arr.indexOf(element), 1].concat(replacement));
}

function cleanLocation(node) {
  delete node.start;
  delete node.end;
  delete node.loc;
  return node;
}

function ensureStatement(node) {
  return types.Statement.check(node)
    ? // Removing the location information seems to ensure that the node is
      // correctly reprinted with a trailing semicolon
github facebook / regenerator / lib / hoist.js View on Github external
/**
 * Copyright (c) 2014, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * https://raw.github.com/facebook/regenerator/master/LICENSE file. An
 * additional grant of patent rights can be found in the PATENTS file in
 * the same directory.
 */

var assert = require("assert");
var types = require("recast").types;
var n = types.namedTypes;
var b = types.builders;
var hasOwn = Object.prototype.hasOwnProperty;

// The hoist function takes a FunctionExpression or FunctionDeclaration
// and replaces any Declaration nodes in its body with assignments, then
// returns a VariableDeclaration containing just the names of the removed
// declarations.
exports.hoist = function(funPath) {
  assert.ok(funPath instanceof types.NodePath);
  n.Function.assert(funPath.value);

  var vars = {};

  function varDeclToExpr(vdec, includeIdentifiers) {
    n.VariableDeclaration.assert(vdec);
github fivetanley / ember-cli-migrator / lib / ember-migrator.js View on Github external
var walk = require('walk-sync');
var path = require('path');
var mkdirp = require('mkdirp');
var recast = require('recast');
var string = require('underscore.string');
var fs = require('fs');
var TypedExport = require('./typed-export');
//var HelperVisitor = require('./helper-visitor');
var execSync = require('child_process').execSync;
var UI = require('ember-cli/lib/ui');
var MigratorVisitor = require('./migrator-visitor');
var namedTypes = recast.types.namedTypes;
var builders = recast.types.builders;
var chalk = require('chalk');
var lodash = require('lodash');

// Recast helpers

function EmberMigrator(options){
  this.testing = options.testing || false;
  this.operationChalkText = "Git Move";
  this.operationText = " Moving ";
  // Directory to process
  this.inputDirectory = options.inputDirectory;
  // Where we will place the ember-cli app
  this.outputDirectory  = options.outputDirectory + '/app';
  // If git mv is forced
  this.moveOrCopyCmd = options.forceOutput ? "git mv -f " : "git mv ";
github facebook / regenerator / test / tests.transform.js View on Github external
/**
 * Copyright (c) 2014-present, Facebook, Inc.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

var assert = require("assert");
var recast = require("recast");
var types = recast.types;
var n = types.namedTypes;
var transform = require("..").transform;
var compile = require("..").compile;

var UglifyJS = require("uglify-js");

describe("_blockHoist nodes", function() {
  it("should be hoisted to the outer body", function() {
    var foo;
    var names = [];
    var ast = recast.parse([
      "function *foo(doNotHoistMe, hoistMe) {",
      "  var sent = yield doNotHoistMe();",
      "  hoistMe();",
      "  names.push(sent);",
      "  return 123;",
github fkling / JSNetworkX / codemods / underscoreToCamelCase.js View on Github external
"use strict";
var fs = require('fs');
var glob = require('glob');
var recast = require('recast');
var types = recast.types;

function compile(filepath) {
  var source = fs.readFileSync(filepath).toString();
  var ast = recast.parse(source);

  types.visit(ast, {
    visitIdentifier: function(path) {
      var name = path.value.name;
      if (/^.+_/.test(name)) {
        name = name.replace(/_(.)/g, function(match, char, offset) {
          return offset === 0 ? match : char.toUpperCase();
        });
        path.get('name').replace(name);
      }
      return false;
    }
github jeromeetienne / better.js / contribs / tmp-to-remove / jsdoc2betterjs / libs / jsdocExpression.js View on Github external
var jsdocExpression	= jsdocExpression	|| {}

if( typeof(window) === 'undefined' )	module.exports	= jsdocExpression;

var recast	= require("recast");
var types	= recast.types;
var namedTypes	= types.namedTypes;
var builders	= types.builders;

var jsdocParse	= require('./jsdocParse.js')

//////////////////////////////////////////////////////////////////////////////////
//		Comment								//
//////////////////////////////////////////////////////////////////////////////////

/**
 * convert a jsdocContent and the function into a callExpression for react
 * 
 * @param  {Object} 		jsdocJson - the jsdoc in json
 * @param  {FunctionExpression} functionExpression - the FunctionExpression from the parser associated with the jsdoc
 * @return {CallExpression}     the resulting call expression
 */
github eveningkid / reacto / src / lib / jscodeshift / src / collections / VariableDeclarator.js View on Github external
*  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';

const _ = require('lodash');
const Collection = require('../Collection');
const NodeCollection = require('./Node');
const recast = require('recast');

const astNodesAreEquivalent = recast.types.astNodesAreEquivalent;
const b = recast.types.builders;
var types = recast.types.namedTypes;

const VariableDeclarator = recast.types.namedTypes.VariableDeclarator;

/**
 * @mixin
 */
const globalMethods = {
  /**
   * Finds all variable declarators, optionally filtered by name.
   *
   * @param {string} name
   * @return {Collection}
   */
  findVariableDeclarators: function(name) {
github adambabik / es6-comprehensions / src / index.js View on Github external
'use strict';

var esprima = require('esprima-fb')
  , recast  = require('recast')
  , astUtil = require('ast-util')
  , types   = recast.types
  , b       = types.builders
  , nt      = types.namedTypes
  , NodePath = types.NodePath
  , es6ForOf = require('es6-for-of');

/**
 * Replaces a comprehension block `for...of` loop
 * with a regular `for` loop.
 *
 * @param  {Object} scope      IIFE scope
 * @param  {Object} block      Comprehension block
 * @param  {Number} idx        Block index
 * @param  {Object} forBody    Body of `for` loop
 * @return {Object}            ForStatement
 */
github abuiles / ember-watson / lib / formulas / helpers / ember-data.js View on Github external
'use strict';

var recast     = require('recast');
var types      = recast.types.namedTypes;

var STORE_METHODS = [
  'all',
  'createRecord',
  'filter',
  'find',
  'fetch',
  'fetchAll',
  'findById',
  'findByIds',
  'findQuery',
  'getById',
  'hasRecordForId',
  'metaForType',
  'modelFor',
  'modelFactoryFor',
github universal-vue / uvue / packages / @uvue / rquery / src / RQuery.ts View on Github external
public static createProperty(key: string, value: any) {
    const builder = recast.types.builders;
    return builder.objectProperty(this.createIdentifier(key), value);
  }
}