How to use the recast.types.builders 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 binaryage / dirac / scripts / migration / refactor-to-es-module.ts View on Github external
import { parse, print, types } from 'recast';
import fs from 'fs';
import path from 'path';
import { promisify } from 'util';
import { IdentifierKind, MemberExpressionKind, ExpressionKind, CommentKind } from 'ast-types/gen/kinds';

const readFile = promisify(fs.readFile);
const writeFile = promisify(fs.writeFile);

const FRONT_END_FOLDER = path.join(__dirname, '..', '..', 'front_end')

function capitalizeFirstLetter(string: string) {
  return string.charAt(0).toUpperCase() + string.slice(1);
}

const b = types.builders;

function createReplacementDeclaration(propertyName: IdentifierKind, declaration: any): any {
  // UI.ARIAUtils.Foo = class {} -> export class Foo {}
  if (declaration.type === 'ClassExpression') {
    return b.exportDeclaration(false, b.classDeclaration(propertyName, declaration.body));
  }
  // UI.ARIAUtils.Foo = expression; -> export const Foo = expression;
  if (declaration.type === 'Literal' || declaration.type.endsWith('Expression')) {
    return b.exportNamedDeclaration(b.variableDeclaration("const", [b.variableDeclarator(propertyName, declaration)]));
  }
  console.error(`Unable to refactor declaration of type "${declaration.type}" named "${propertyName.name}"`);
}

function getTopLevelMemberExpression(expression: MemberExpressionKind): any {
  while (expression.object.type === 'MemberExpression') {
    expression = expression.object;
github facebook / regenerator / lib / emit.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 isArray = types.builtInTypes.array;
var b = types.builders;
var n = types.namedTypes;
var leap = require("./leap");
var meta = require("./meta");
var util = require("./util");
var runtimeProperty = util.runtimeProperty;
var hasOwn = Object.prototype.hasOwnProperty;

function Emitter(contextId) {
  assert.ok(this instanceof Emitter);
  n.Identifier.assert(contextId);

  // Used to generate unique temporary names.
  this.nextTempId = 0;

  Object.defineProperties(this, {
    // In order to make sure the context object does not collide with
github Heigvd / Wegas / wegas-app / src / main / webapp / wegas-react-form / src / Script / parser.js View on Github external
function Parsed(props) {
        const { value, onChange, ...restProps } = props;
        let error = '';
        let ast;
        try {
            ast = parse(value);
        } catch (e) {
            error = e.description;
            // should show code string instead of falling back to an empty program
            ast = types.builders.file(types.builders.program([]));
        }
        return (
            
                
                     {
                            ast.program.body = v;
                            onChange(print(ast).code);
                        }}
                    />
                
            
        );
    }
github Caltech-IPAC / firefly / node_modules / babel-core / node_modules / regenerator / lib / emit.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 isArray = types.builtInTypes.array;
var b = types.builders;
var n = types.namedTypes;
var leap = require("./leap");
var meta = require("./meta");
var util = require("./util");
var runtimeProperty = util.runtimeProperty;
var hasOwn = Object.prototype.hasOwnProperty;

function Emitter(contextId) {
  assert.ok(this instanceof Emitter);
  n.Identifier.assert(contextId);

  // Used to generate unique temporary names.
  this.nextTempId = 0;

  Object.defineProperties(this, {
    // In order to make sure the context object does not collide with
github Heigvd / Wegas / wegas-app / src / main / webapp / wegas-react-form / src / Script / condition.js View on Github external
exp => !types.namedTypes.EmptyStatement.check(exp)
    );
    let st;
    switch (ast.length) {
        case 0:
            return [types.builders.emptyStatement()];
        case 1:
            return [types.builders.expressionStatement(ast[0])];
        default:
            st = types.builders.logicalExpression(
                AND,
                ast.shift(),
                ast.shift()
            );
            while (ast.length) {
                st = types.builders.logicalExpression(AND, st, ast.shift());
            }
            return join([st]);
    }
}
/**
github facebook / regenerator / lib / leap.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 inherits = require("util").inherits;
var hasOwn = Object.prototype.hasOwnProperty;

function Entry() {
  assert.ok(this instanceof Entry);
}

function FunctionEntry(returnLoc) {
  Entry.call(this);
  n.Literal.assert(returnLoc);
  this.returnLoc = returnLoc;
}

inherits(FunctionEntry, Entry);
exports.FunctionEntry = FunctionEntry;
github Heigvd / Wegas / wegas-app / src / main / webapp / wegas-react-form / src / Script / condition.js View on Github external
function join(statements) {
    const ast = statements.filter(
        exp => !types.namedTypes.EmptyStatement.check(exp)
    );
    let st;
    switch (ast.length) {
        case 0:
            return [types.builders.emptyStatement()];
        case 1:
            return [types.builders.expressionStatement(ast[0])];
        default:
            st = types.builders.logicalExpression(
                AND,
                ast.shift(),
                ast.shift()
            );
            while (ast.length) {
                st = types.builders.logicalExpression(AND, st, ast.shift());
            }
            return join([st]);
    }
}
/**
github facebook / regenerator / lib / util.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 types = require("recast").types;
var n = types.namedTypes;
var b = types.builders;
var hasOwn = Object.prototype.hasOwnProperty;

exports.defaults = function(obj) {
  var len = arguments.length;
  var extension;

  for (var i = 1; i < len; ++i) {
    if ((extension = arguments[i])) {
      for (var key in extension) {
        if (hasOwn.call(extension, key) && !hasOwn.call(obj, key)) {
          obj[key] = extension[key];
        }
      }
    }
  }
github Heigvd / Wegas / wegas-app / src / main / webapp / wegas-react-form / src / Script / modules / Condition.js View on Github external
import PropTypes from 'prop-types';
import React from 'react';
import { types } from 'recast';
import Impact, { ErrorCatcher } from './Impact';
import { methodDescriptor, extractMethod } from './method';
import { methodDescriptor as globalMethodDescriptor } from './globalMethod';
import ConditionOperator from './ConditionOperator';
import { renderForm, valueToAST } from './args';
import { containerStyle } from '../Views/conditionImpactStyle';

const b = types.builders;
const isCallExpression = types.namedTypes.CallExpression.check;
const isExpressionStatement = types.namedTypes.ExpressionStatement.check;
/**
 * return a default value for the given type
 * @param {string} type the type for which a default value is required
 * @returns {string|undefined|boolean} the default value
 */
function defaultValue(type) {
    switch (type) {
        case 'string':
            return '';
        case 'number':
            return undefined;
        case 'boolean':
            return true;
        default: