How to use the @babel/parser.parseExpression 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 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 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 facebook / prepack / src / react / experimental-server-rendering / utils.js View on Github external
let item;

      while (i < length) {
        item = array[i++];
        if (previousWasTextNode === true) {
          str += "" + item;
        } else {
          str += item;
        }
        previousWasTextNode = item[0] !== "<";
      }
      return str;
    }
  `;

  let escapeHelperAst = parseExpression(arrayHelper, { plugins: ["flow"] });
  let helper = new ECMAScriptSourceFunctionValue(realm);
  helper.initialize(escapeHelperAst.params, escapeHelperAst.body);
  return helper;
}
github Heigvd / Wegas / wegas-app / src / main / webapp / 2 / src / Editor / Components / FormView / Script / ExpressionStatement.tsx View on Github external
function valueToAST(value: unknown, type: parameterType) {
  if (value === undefined) {
    return identifier('undefined');
  }
  if (type === 'identifier' && typeof value === 'string') {
    return identifier(value);
  }
  switch (typeof value) {
    case 'string':
      return stringLiteral(value);
    case 'number':
      return numericLiteral(value);
    case 'boolean':
      return booleanLiteral(value);
    case 'object': {
      return parseExpression(JSON.stringify(value));
    }
    default:
      throw Error(`Unknown type ${type}`);
  }
}
github bradlc / babel-plugin-tailwind-components / src / visit.js View on Github external
const css = objToString(styles, isDev)
    const parent = path.parentPath

    if (parent.isTemplateLiteral()) {
      const exprIndex = parent.get('expressions').indexOf(path)
      const before = parent.get('quasis')[exprIndex]
      const after = parent.get('quasis')[exprIndex + 1]

      after.node.value.raw = before.node.value.raw + css + after.node.value.raw
      after.node.value.cooked =
        before.node.value.cooked + css + after.node.value.cooked

      before.remove()
      path.remove()

      const ast = babylon.parseExpression(generate(parent.node).code)
      parent.replaceWith(ast)
    } else {
      const tte = '`' + css + '`'
      path.replaceWith(babylon.parseExpression(tte))
    }
  } else {
    if (path.node.type === 'StringLiteral') {
      path.replaceWith(t.callExpression(cloneNode(cssIdentifier), [styleObj]))
    } else {
      path.replaceWith(styleObj)
    }
  }
}
github jviide / babel-plugin-transform-replace-expressions / index.js View on Github external
asArray(replace).forEach(([key, value]) => {
    const kNode = parseExpression(key);
    const vNode = parseExpression(value);

    const candidates = types.get(kNode.type) || [];
    candidates.push({ key: kNode, value: vNode, originalKey: key });
    types.set(kNode.type, candidates);

    for (let i = 0; i < candidates.length - 1; i++) {
      if (!t.isNodesEquivalent(candidates[i].key, kNode)) {
        continue;
      }

      if (allowConflictingReplacements) {
        candidates[i] = candidates.pop();
        break;
      }
github groupon / swagql / lib / generator / deoperationalize.js View on Github external
function buildSecurityConfig(opSecurity, defaultSecurity, securityDefinitions) {
  let securityConfig = {};
  const security = opSecurity || defaultSecurity;

  if (security && security.length) {
    const auths = new Set();
    security.forEach(schemas =>
      Object.keys(schemas).forEach(auth => auths.add(auth))
    );
    securityConfig = {
      security: security,
      definitions: pick(securityDefinitions, Array.from(auths)),
    };
  }

  return parser.parseExpression(JSON.stringify(securityConfig));
}

@babel/parser

A JavaScript parser

MIT
Latest version published 9 days ago

Package Health Score

95 / 100
Full package analysis