How to use the recast.parse 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 empress / empress-blog / blueprints / empress-blog / index.js View on Github external
}).then(() => {
      const builders = recast.types.builders;

      const code = readFileSync('./ember-cli-build.js');
      const ast = recast.parse(code);

      recast.visit(ast, {
        visitNewExpression: function (path) {
          var node = path.node;

          if (node.callee.name === 'EmberApp'
              || node.callee.name === 'EmberAddon') {
            // console.log(node, node.arguments)
            const configNode = node.arguments.find(element => element.type === 'ObjectExpression');

            let fingerprint = configNode.properties.find(property => property.key.value === 'fingerprint');

            if(!fingerprint) {
              fingerprint = builders.property(
                'init',
                builders.identifier('fingerprint'),
github binaryage / dirac / scripts / migration / refactor-to-es-module.ts View on Github external
function rewriteSource(source: string, refactoringNamespace: string, refactoringFileName: string) {
  const exportedMembers: {prop: IdentifierKind, comments: CommentKind[]}[] = [];
  const ast = parse(source);

  ast.program.body = ast.program.body.map((expression: any) => {
    // UI.ARIAUtils.Foo = 5;
    if (expression.type === 'ExpressionStatement') {
      // UI.ARIAUtils.Foo = 5
      if (expression.expression.type === 'AssignmentExpression') {
        const assignment = expression.expression;

        // UI.ARIAUtils.Foo
        if (assignment.left.type === 'MemberExpression') {
          // UI.ARIAUtils.Foo -> UI.ARIAUtils
          // UI.ARIAUtils.Nested.Foo -> UI.ARIAUtils
          const topLevelAssignment = getTopLevelMemberExpression(assignment.left);

          // If there is a nested export, such as `UI.ARIAUtils.Nested.Field`
          if (topLevelAssignment !== assignment.left.object) {
github polyfills / es6-module-crosspiler / test / index.js View on Github external
read = function read(name) {
  return recast.parse(fixture(name), {
    esprima: esprima
  })
}
github stoplightio / spectral / test-harness / codegen / generate.ts View on Github external
export function generate({ assets, scenario, scenarioName }: Input) {
  const ast = recast.parse(template, {
    parser: require('recast/parsers/typescript'),
  }) as n.File;

  processComments(
    ast,
    {
      SPECTRAL_BIN,
      SCENARIO_NAME: scenarioName,
      SCENARIO_FILE_PATH: join(SCENARIOS_ROOT, scenarioName),
      SCENARIOS_ROOT,
      TEST_NAME: scenario.test,
    },
    scenario,
  );

  populateAssets(ast.program.body, assets);
github 5to6 / 5to6-codemod / test / utils.js View on Github external
function toAST(code) {
  return recast.parse(code).program.body;
}
github cardstack / cardstack / packages / cli / rewriters.ts View on Github external
},
      },
      {
        package: "liquid-fire",
        components: {
          "{{liquid-bind}}": {
            yieldsArguments: ["value"],
          },
        },
      },
    ],
  });
})()
`;

const embroiderAST = recast.parse(embroiderSnippet).program.body[0];

export function rewriteEmberCLIBuild(source: string) {
  let ast = recast.parse(source);
  recast.visit(ast, {
    visitCallExpression(path) {
      if (
        path.node.callee.type === "MemberExpression" &&
        path.node.callee.object.type === "Identifier" &&
        path.node.callee.object.name === "app" &&
        path.node.callee.property.type === "Identifier" &&
        path.node.callee.property.name === 'toTree'
      ) {
        path.replace(embroiderAST);
      }
      return false;
    },
github rambler-digital-solutions / rambler-ui / docs / components / PropTypesTable.js View on Github external
getDeprecatedInfo(type) {
    const deprecatedPropType = 'deprecated(PropTypes.'

    const indexStart = type.raw.indexOf(deprecatedPropType)

    if (indexStart !== -1)
      return {
        propTypes: type.raw.substring(
          indexStart + deprecatedPropType.length,
          type.raw.indexOf(',')
        ),
        explanation: recast.parse(type.raw).program.body[0].expression
          .arguments[1].value
      }

    return false
  }
github sghall / react-compound-slider / docs / src / components / PropsDescription / PropsDescription.js View on Github external
function getDeprecatedInfo(type) {
  const deprecatedPropType = 'deprecated(PropTypes.'

  const indexStart = type.raw.indexOf(deprecatedPropType)

  if (indexStart !== -1) {
    return {
      propTypes: type.raw.substring(
        indexStart + deprecatedPropType.length,
        type.raw.indexOf(','),
      ),
      explanation: recast.parse(type.raw).program.body[0].expression
        .arguments[1].value,
    }
  }

  return false
}
github filipesabella / vscode-live-p5 / src / code-parser.ts View on Github external
function astFromUserCode(userCode: string): any {
  return recast.parse(recast.prettyPrint(recast.parse(userCode)).code);
}