How to use the @babel/parser.parse function in @babel/parser

To help you get started, we’ve selected a few @babel/parser 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 umijs / umi / packages / umi-build-dev / src / plugins / commands / block / insertComponent.js View on Github external
export default function(content, { relativePath, identifier }) {
  const ast = parser.parse(content, {
    sourceType: 'module',
    plugins: ['jsx', 'decorators-legacy', 'typescript'],
  });
  traverse(ast, {
    Program({ node }) {
      // add import
      const { body } = node;
      const lastImportSit = findLastIndex(body, item => {
        return t.isImportDeclaration(item);
      });
      const newImport = t.ImportDeclaration(
        [t.ImportDefaultSpecifier(t.identifier(upperCamelCase(identifier)))],
        t.stringLiteral(relativePath),
      );
      body.splice(lastImportSit + 1, 0, newImport);
    },
github finom / check-imports / api / getImportsFromFile.js View on Github external
const parse = (script) => {
  try {
    return babelParser.parse(script, {
      sourceType: 'module',
      plugins: defaultBabelPlugins,
    });
  } catch (e) {
    // try to parse with flow syntax
    return babelParser.parse(script, {
      sourceType: 'module',
      plugins: flowBabelPlugins,
    });
  }
};
github didi / chameleon / packages / mvvm-interface-parser / lib / check.js View on Github external
enter(path) {
      if (path.node.type === 'ExportDefaultDeclaration') {
        // 拿到export ddefault new Method(); 这一行代码
        let exportCode = generate["default"](path.node);
        // 拿到 new Method(); 这一段代码
        let declarationCode = generate["default"](path.node.declaration);
        // 得到 export default __OBJECT__WARPPER__(new Method());
        let codeSeg = exportCode.code.replace(declarationCode.code, '__OBJECT__WRAPPER__(' + declarationCode.code + ', __CML_ERROR__, __enableTypes__, __CHECK__DEFINES__ )');
        // 转成ast
        let replacement = parser.parse(codeSeg, {
          plugins: parsePlugins,
          sourceType: 'module'
        });
        traverse["default"].removeProperties(replacement);
        // 替换
        path.replaceWith(replacement.program.body[0]);
        path.stop();
      }
    }
  });
github salesforce / lwc / packages / @lwc / template-compiler / src / codegen / codegen.ts View on Github external
genInlineStyles(src: string | undefined): void {
        if (src) {
            // We get back a AST module which may have three pieces:
            // 1) import statements
            // 2) the inline function
            // 3) default export
            // We need to separate the imports and change the default export for a correct inlining
            const importDeclarations: t.ImportDeclaration[] = [];
            const styleBody: t.Statement[] = [];

            // Parse the generated module code and return it's body.
            const parsed = babylon.parse(src, { sourceType: 'module' });
            const inlineStylesAst = parsed.program.body;

            inlineStylesAst.forEach(node => {
                if (t.isImportDeclaration(node)) {
                    importDeclarations.push(node);
                } else if (t.isExportDefaultDeclaration(node)) {
                    const stylesheetDeclaration = t.variableDeclaration('const', [
                        t.variableDeclarator(
                            t.identifier('stylesheets'),
                            node.declaration as t.ArrayExpression
                        ),
                    ]);

                    styleBody.push(stylesheetDeclaration);
                } else {
                    styleBody.push(node);
github uber / baseweb / scripts / cheat-sheet-generator.js View on Github external
function parseFileToOutline(code) {
  const types = [];
  const ast = parse(code, {sourceType: 'module', plugins: ['flow']});
  traverse(ast, {
    ExportNamedDeclaration(path) {
      if (t.isTypeAlias(path.node.declaration)) {
        const typeNode = {
          name: path.node.declaration.id.name,
          lineStart: path.node.declaration.id.loc.start.line,
          children: [],
        };

        if (t.isObjectTypeAnnotation(path.node.declaration.right)) {
          typeNode.children = path.node.declaration.right.properties.map(
            property => {
              if (t.isObjectTypeProperty(property)) {
                if (t.isLiteral(property.key)) {
                  return {
                    name: property.key.value,
github facebookincubator / fbt / packages / fb-babel-plugin-utils / TestUtil.js View on Github external
function parse(code) {
  return babelParser.parse(code, {
    sourceType: 'module',
    plugins: ['flow', 'jsx'],
  });
}
github facebook / relay / packages / relay-compiler / language / javascript / FindGraphQLTags.js View on Github external
function find(text: string): $ReadOnlyArray {
  const result: Array = [];
  const ast = babylon.parse(text, BABYLON_OPTIONS);

  const visitors = {
    CallExpression: node => {
      const callee = node.callee;
      if (
        !(
          (callee.type === 'Identifier' &&
            CREATE_CONTAINER_FUNCTIONS[callee.name]) ||
          (callee.kind === 'MemberExpression' &&
            callee.object.type === 'Identifier' &&
            callee.object.value === 'Relay' &&
            callee.property.type === 'Identifier' &&
            CREATE_CONTAINER_FUNCTIONS[callee.property.name])
        )
      ) {
        traverse(node, visitors);
github alibaba / rax / packages / jsx-compiler / src / parser / index.js View on Github external
function parseCode(code) {
  return babelParser.parse(code, parserOption);
}
github crubier / code-to-graph / index.js View on Github external
function transformJsStringToJsAst(string) {
  return babelParser.parse(string, {
    sourceType: "module",
    plugins: ["classProperties", "asyncGenerators", "jsx", "typescript"]
  });
}

@babel/parser

A JavaScript parser

MIT
Latest version published 9 days ago

Package Health Score

95 / 100
Full package analysis