How to use the babylon.parseExpression function in babylon

To help you get started, we’ve selected a few babylon 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 lingui / js-lingui / packages / cli / src / api / compile.js View on Github external
if (locale === pseudoLocale) {
    pluralRules = plurals["en"]
  }

  const compiledMessages = R.keys(messages).map(key => {
    let translation = messages[key] || (!strict ? key : "")
    if (locale === pseudoLocale) {
      translation = pseudoLocalize(translation)
    }
    return t.objectProperty(t.stringLiteral(key), compile(translation))
  })

  const languageData = [
    t.objectProperty(
      t.stringLiteral("plurals"),
      parseExpression(pluralRules.toString())
    )
  ]

  const ast = buildExportStatement(
    t.objectExpression([
      // language data
      t.objectProperty(
        t.identifier("languageData"),
        t.objectExpression(languageData)
      ),
      // messages
      t.objectProperty(
        t.identifier("messages"),
        t.objectExpression(compiledMessages)
      )
    ]),
github alibaba / rax / packages / mp-loader / src / transpiler / expression.js View on Github external
}
        });
        let callArgs = [t.arrayExpression(args)];
        if (parent.callee === node) {
          callArgs.push(t.numericLiteral(1));
        }
        let newNode = t.callExpression(t.identifier(memberFn), callArgs);
        newNode.callee.__rmlSkipped = 1;
        // will process a.v of x.y[a.v]
        path.replaceWith(newNode);
        // path.skip();
      }
    }
  };

  let expression = parseExpression(codeStr, babylonConfig);
  let start = expression.start,
    end = expression.end;
  let ast = {
    type: 'File',
    start: start,
    end: end,
    program: {
      start: start,
      end: end,
      type: 'Program',
      body: [{
        start: start,
        end: end,
        type: 'ExpressionStatement',
        expression: expression
      }]
github wssgcg1213 / babel-plugin-inline-replace-variables / index.js View on Github external
if (type === 'string' || type === 'boolean') {
          replacementDescriptor = {
            type: type,
            replacement: replacementDescriptor
          }
        } else if (t.isNode(replacementDescriptor)) {
          replacementDescriptor = {
            type: 'node',
            replacement: replacementDescriptor
          }
        } else if (type === 'object'
          && replacementDescriptor.type === 'node'
          && typeof replacementDescriptor.replacement === 'string') {
          replacementDescriptor.replacement = parseMap[replacementDescriptor.replacement]
            ? parseMap[replacementDescriptor.replacement]
            : babylon.parseExpression(replacementDescriptor.replacement)
        }

        const replacement = replacementDescriptor.replacement
        switch (replacementDescriptor.type) {
          case 'boolean':
            path.replaceWith(t.booleanLiteral(replacement))
            break
          case 'node':
            if (t.isNode(replacement)) {
              path.replaceWith(replacement)
            }
            break
          default:
            // treat as string
            const str = String(replacement)
            path.replaceWith(t.stringLiteral(str))
github alibaba / rax / packages / mp-loader / src / transpiler / __tests__ / weixin-template-transpiler.js View on Github external
function checkValidJavaScriptStr(str) {
  try {
    babylon.parseExpression(str, {
      allowImportExportEverywhere: true,
      plugins: ['objectRestSpread'],
    });
  } catch (err) {
    return false;
  }
  return true;
}
github alibaba / rax / packages / mp-loader / src / transpiler / __tests__ / template-transpiler.js View on Github external
function checkValidJavaScriptStr(str) {
  try {
    babylon.parseExpression(str, {
      allowImportExportEverywhere: true,
      plugins: ['objectRestSpread'],
    });
  } catch (err) {
    return false;
  }
  return true;
}
github mapbox / jsxtreme-markdown / packages / babel-plugin-transform-jsxtreme-markdown / index.js View on Github external
const applyTransform = (templateExpressionPath, text, options) => {
  const jsx = jsxtremeMarkdown.toJsx(text, options);
  const ast = babylon.parseExpression(jsx, { plugins: ['jsx'] });
  templateExpressionPath.replaceWith(ast);
};
github alibaba / rax / packages / mp-loader / src / transpiler / expression.js View on Github external
function isValidJSExp(exp) {
  try {
    parseExpression(exp, babylonConfig);
  } catch (err) {
    return false;
  }
  return true;
}
github erukiti / ast-book-sample / chapter3 / babel-plugin-di.js View on Github external
VariableDeclarator: (nodePath, state) => {
      const {kind} = nodePath.parent

      if (t.isIdentifier(nodePath.node.id)) {
        const replaceCode =
          state.replacers[`${kind} ${nodePath.node.id.name} =`]
        if (replaceCode) {
          const newAst = parseExpression(replaceCode)
          nodePath.get('init').replaceWith(newAst)
        }
      }
    },
    'FunctionDeclaration|ClassDeclaration': (nodePath, state) => {