How to use @babel/parser - 10 common examples

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 azu / power-doctest / packages / comment-to-assert / src / comment-to-assert.ts View on Github external
return {
            type: "Resolve",
            node: getExpressionNodeFromCommentValue(match[1])
        };
    } else if (PROMISE_REJECT_COMMENT_PATTERN.test(message)) {
        const match = message.match(PROMISE_REJECT_COMMENT_PATTERN);
        if (!match) {
            throw new Error("Can not Parse: // => Reject: value");
        }
        return {
            type: "Reject",
            node: getExpressionNodeFromCommentValue(match[1])
        };
    }
    try {
        return parseExpression(string);
    } catch (e) {
        console.error(`Can't parse comments // => expression`);
        throw e;
    }
}
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 storybookjs / storybook / addons / docs / mdx-compiler-plugin.js View on Github external
function getExports(node, counter) {
  const { value, type } = node;
  if (type === 'jsx') {
    if (STORY_REGEX.exec(value)) {
      // Single story
      const ast = parser.parseExpression(value, { plugins: ['jsx'] });
      const storyExport = genStoryExport(ast, counter);
      return storyExport && { stories: storyExport };
    }
    if (PREVIEW_REGEX.exec(value)) {
      // Preview, possibly containing multiple stories
      const ast = parser.parseExpression(value, { plugins: ['jsx'] });
      return { stories: genPreviewExports(ast, counter) };
    }
    if (META_REGEX.exec(value)) {
      // Preview, possibly containing multiple stories
      const ast = parser.parseExpression(value, { plugins: ['jsx'] });
      return { meta: genMeta(ast) };
    }
  }
  return null;
}
github storybookjs / storybook / addons / docs / mdx-compiler-plugin.js View on Github external
function getStories(node, counter) {
  const { value, type } = node;
  // Single story
  if (type === 'jsx') {
    if (STORY_REGEX.exec(value)) {
      // Single story
      const ast = parser.parseExpression(value, { plugins: ['jsx'] });
      const storyExport = genStoryExport(ast, value, counter);
      return storyExport && [storyExport];
    }
    if (PREVIEW_REGEX.exec(value)) {
      // Preview, possibly containing multiple stories
      const ast = parser.parseExpression(value, { plugins: ['jsx'] });
      return genPreviewExports(ast, value, counter);
    }
  }
  return null;
}
github storybookjs / storybook / addons / docs / src / mdx / mdx-compiler-plugin.js View on Github external
function getExports(node, counter) {
  const { value, type } = node;
  if (type === 'jsx') {
    if (STORY_REGEX.exec(value)) {
      // Single story
      const ast = parser.parseExpression(value, { plugins: ['jsx'] });
      const storyExport = genStoryExport(ast, counter);
      return storyExport && { stories: storyExport };
    }
    if (PREVIEW_REGEX.exec(value)) {
      // Preview, possibly containing multiple stories
      const ast = parser.parseExpression(value, { plugins: ['jsx'] });
      return { stories: genPreviewExports(ast, counter) };
    }
    if (META_REGEX.exec(value)) {
      // Preview, possibly containing multiple stories
      const ast = parser.parseExpression(value, { plugins: ['jsx'] });
      return { meta: genMeta(ast) };
    }
  }
  return null;
}
github storybookjs / storybook / addons / docs / mdx-compiler-plugin.js View on Github external
function getStories(node, counter) {
  const { value, type } = node;
  // Single story
  if (type === 'jsx') {
    if (STORY_REGEX.exec(value)) {
      // Single story
      const ast = parser.parseExpression(value, { plugins: ['jsx'] });
      const storyExport = genStoryExport(ast, value, counter);
      return storyExport && [storyExport];
    }
    if (PREVIEW_REGEX.exec(value)) {
      // Preview, possibly containing multiple stories
      const ast = parser.parseExpression(value, { plugins: ['jsx'] });
      return genPreviewExports(ast, value, counter);
    }
  }
  return null;
}
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,

@babel/parser

A JavaScript parser

MIT
Latest version published 22 days ago

Package Health Score

95 / 100
Full package analysis