How to use the recast.prettyPrint 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 JRigotti / escl / templates / _default.js View on Github external
module.exports = (inheritOptions = {}) => {
    // the abstract syntax tree
    const ast = recast.parse(code);

    // add cli options to object tree
    const result = buildObject(inheritOptions);

    // put newly created object as right side of the ast
    ast.program.body[0].expression.right = result;

    return recast.prettyPrint(ast).code;
};
github ChromeDevTools / devtools-frontend / scripts / migrate_test / migrate_helpers.js View on Github external
function print(ast) {
  /**
   * Not using clang-format because certain tests look bad when formatted by it.
   * Recast pretty print is smarter about preserving existing spacing.
   */
  let code = recast.prettyPrint(ast, {tabWidth: 2, wrapColumn: 120, quote: 'single'}).code;
  code = code.replace(/(\/\/\#\s*sourceURL=[\w-]+)\.html/, '$1.js');
  code = code.replace(/\s*\$\$SECRET_IDENTIFIER_FOR_LINE_BREAK\$\$\(\);/g, '\n');
  return code + '\n';
}
github standard-things / esm / test / transform-tests.js View on Github external
Object.keys(files).forEach(function (absPath) {
      if (typeof reject === "function" &&
          reject(relative(__dirname, absPath))) {
        return;
      }

      const code = files[absPath];
      options.moduleAlias = makeUniqueId("module", code);

      const ast = options.parse(code);
      transform(ast, options);

      assert.strictEqual(
        prettyPrint(compile(code, options).ast).code,
        prettyPrint(ast).code,
        "Transform mismatch for " + absPath
      );
    });
  }
github esnext / es6-arrow-function / test / test_helper.js View on Github external
function normalize(source) {
  return recast.prettyPrint(recast.parse(source, recastOptions), recastOptions).code;
}
github rainforestapp / decaf / src / parser.js View on Github external
function fallback(node: CNode, meta: Object) {
  const compiled = node.compile(meta);
  return parse(prettyPrint(
    parse(compiled), meta.options)).program.body[0].expression;
}
github contentful / contentful-cli / lib / cmds / space_cmds / generate_cmds / migration.js View on Github external
const generateMigrationScript = async function(environment, contentTypes) {
  const migrationContents = await Promise.all(
    _.map(contentTypes, async contentType => {
      return generateContentTypeMigration(environment, contentType)
    })
  )
  const migration = wrapMigrationWithBase(
    b.blockStatement(_.flatten(migrationContents))
  )

  const output = recast.prettyPrint(migration, { tabWidth: 2 }).code

  return prettier.format(output, { parser: 'babel' })
}
github filipesabella / vscode-live-p5 / src / code-parser.ts View on Github external
path.replace(
          typeBuilders.memberExpression(
            typeBuilders.identifier(AllVarsVariableName),
            typeBuilders.identifier(key)));

        return false;
      }
    });

    const modifiedUserCode = recast.prettyPrint(ast).code;

    previousCode = astFromUserCode(userCode);

    return `const ${AllVarsVariableName} = ${JSON.stringify(vars)}; ${modifiedUserCode}`;
  } catch (e) {
    return parseCode(recast.prettyPrint(previousCode));
  }
}
github storybookjs / storybook / lib / codemod / src / transforms / csf-to-mdx.js View on Github external
.map(n => {
      const { code } = recast.prettyPrint(n, options);
      if (n.type === 'JSXElement') {
        return `${code}\n`;
      }
      return code;
    })
    .join('\n');