How to use the babel-traverse.default function in babel-traverse

To help you get started, we’ve selected a few babel-traverse 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 node-cube / cube / lib / cube / ext_api_process.js View on Github external
try {
      ast = babylon.parse(code, {
        // sourceType: 'module',
        filename: filepath
      });
    } catch (e) {
      e.code = 'Js_Parse_Error';
      e.file = filepath;
      e.line = e.lineNumber;
      return callback(e);
    }
    debug('parse ast ok', filepath);
    let wait = 0;
    let traverseFlag = false;
    //////////////////// walker start //////////////////////
    babelTraverse(ast, {
      CallExpression: {
        enter: function (nodePath) {
          let node = nodePath.node;
          let scope = nodePath.scope;
          // filter other property calls, like `require[test]();`, `require.resolve()`
          if (!babelType.isIdentifier(node.callee)) {
            return;
          }

          let callFuncName = node.callee.name;
          // filter other calls
          if (callFuncName !== 'require' && callFuncName !== 'load') {
            return;
          }

          let methodOverride = false;
github vicwang163 / react-to-vue / src / index.js View on Github external
})
    }
  })
  // traverse module
  let result = {
    "import": [],
    "declaration": [],
    "class": {},
    "functional": [],
    "propTypes": {},
    "defaultProps": {},
    // there exists incompatibility
    "caveats": [],
    "source": fileContent
  }
  babelTraverse(ast, {
    Program (path) {
      let nodeLists = path.node.body
      let classDefineCount = 0
      for (let i = 0; i < nodeLists.length; i++) {
        let node = nodeLists[i]
        let cPath = path.get(`body.${i}`)
        // get prop-types
        if (cPath.isExpressionStatement() && node.expression.type === 'AssignmentExpression') {
          let leftNode = node.expression.left
          if (leftNode.type === 'MemberExpression' && ["defaultProps", "propTypes"].includes(leftNode.property.name)) {
            let className = node.expression.left.object.name
            getProps(className, leftNode.property.name, node.expression.right, result)
          }
        } else if (cPath.isClassDeclaration()) {
          classDefineCount ++
          if (classDefineCount > 1) {
github parcel-bundler / parcel / src / visitors / dependencies.js View on Github external
function evaluateExpression(node) {
  // Wrap the node in a standalone program so we can traverse it
  node = types.file(types.program([types.expressionStatement(node)]));

  // Find the first expression and evaluate it.
  let res = null;
  traverse(node, {
    Expression(path) {
      res = path.evaluate();
      path.stop();
    }
  });

  return res;
}
github NervJS / taro / packages / taro-cli / src / weapp.js View on Github external
function parseComponentExportAst (ast, componentName, componentPath, componentType) {
  let componentRealPath = null
  let importExportName
  ast = babel.transformFromAst(ast, '', {
    plugins: [
      [require('babel-plugin-transform-define').default, constantsReplaceList]
    ]
  }).ast
  traverse(ast, {
    ExportNamedDeclaration (astPath) {
      const node = astPath.node
      const specifiers = node.specifiers
      const source = node.source
      if (source && source.type === 'StringLiteral') {
        specifiers.forEach(specifier => {
          const exported = specifier.exported
          if (_.kebabCase(exported.name) === componentName) {
            componentRealPath = Util.resolveScriptPath(path.resolve(path.dirname(componentPath), source.value))
          }
        })
      } else {
        specifiers.forEach(specifier => {
          const exported = specifier.exported
          if (_.kebabCase(exported.name) === componentName) {
            importExportName = exported.name
github material-components / material-components-web / scripts / check-pkg-for-release.js View on Github external
function checkAutoInitAddedInMDCPackage(ast) {
  let nameCamel = camelCase(pkg.name.replace('@material/', ''));
  if (nameCamel === 'textfield') {
    nameCamel = 'textField';
  } else if (nameCamel === 'switch') {
    nameCamel = 'switchControl';
  }
  let autoInitedCount = 0;
  traverse(ast, {
    'ExpressionStatement'({node}) {
      const callee = node.expression.callee;
      const args = node.expression.arguments;
      if (callee.object.name === 'autoInit' && callee.property.name === 'register') {
        const expression = args.find((value) => {
          return value.type === 'MemberExpression';
        });
        if (expression.object.name === nameCamel) {
          autoInitedCount++;
        }
      }
    },
  });
  return autoInitedCount;
}
github FE-Kits / Ueact / packages / ueact-tiga / packages / taro-cli / src / h5.js View on Github external
function parseAst(code) {
  const styleFiles = [];
  const ast = babylon.parse(code, babylonConfig);
  let taroImportDefaultName;
  let componentClassName;
  traverse(ast, {
    enter(astPath) {
      const node = astPath.node;
      if (node.type === 'ClassProperty' && node.key.name === 'config') {
        astPath.remove();
      } else if (node.type === 'ImportDeclaration') {
        const source = node.source;
        const value = source.value;
        if (Util.isNpmPkg(value)) {
          if (value === taroJsFramework) {
            const specifiers = node.specifiers;
            let defaultSpecifier = null;
            let idx = -1;
            specifiers.forEach((item, index) => {
              if (item.type === 'ImportDefaultSpecifier') {
                defaultSpecifier = item.local.name;
                idx = index;
github Tencent / omi / packages / omip / taro-cli / src / convertor.js View on Github external
parseAst ({ ast, sourceFilePath, outputFilePath, importStylePath, depComponents, imports = [], isApp = false }) {
    const scriptFiles = new Set()
    const self = this
    let componentClassName = null
    let needInsertImportTaro = false
    traverse(ast, {
      Program: {
        enter (astPath) {
          astPath.traverse({
            ClassDeclaration (astPath) {
              const node = astPath.node
              let isTaroComponent = false
              if (node.superClass) {
                astPath.traverse({
                  ClassMethod (astPath) {
                    if (astPath.get('key').isIdentifier({ name: 'render' })) {
                      astPath.traverse({
                        JSXElement () {
                          isTaroComponent = true
                        }
                      })
                    }
github Tencent / omi / packages / omip / my-app / scripts / taro-cli / src / weapp.js View on Github external
const jsonFiles = []
  const mediaFiles = []
  let configObj = {}
  let componentClassName = null
  let taroJsReduxConnect = null
  let taroMiniAppFramework = `@tarojs/taro-${buildAdapter}`
  let taroImportDefaultName
  let needExportDefault = false
  let exportTaroReduxConnected = null
  ast = babel.transformFromAst(ast, '', {
    plugins: [
      [require('babel-plugin-danger-remove-unused-import'), { ignore: ['@tarojs/taro', 'react', 'nervjs'] }],
      [require('babel-plugin-transform-define').default, constantsReplaceList]
    ]
  }).ast
  traverse(ast, {
    ClassDeclaration (astPath) {
      const node = astPath.node
      let hasCreateData = false
      if (node.superClass) {
        astPath.traverse({
          ClassMethod (astPath) {
            if (astPath.get('key').isIdentifier({ name: '_createData' })) {
              hasCreateData = true
            }
          }
        })
        if (hasCreateData) {
          needExportDefault = true
          astPath.traverse({
            ClassMethod (astPath) {
              const node = astPath.node
github rekit / rekit / packages / rekit-core / core2 / app.js View on Github external
function getRootRoutePath() {
  const targetPath = utils.mapSrcFile('common/routeConfig.js');
  const ast = vio.getAst(targetPath);
  let rootPath = '';
  traverse(ast, {
    ObjectExpression(path) {
      const node = path.node;
      const props = node.properties;
      if (!props.length) return;
      const obj = {};
      props.forEach(p => {
        if (_.has(p, 'key.name') && !p.computed) {
          obj[p.key.name] = p;
        }
      });
      if (obj.path && obj.childRoutes && !rootPath) {
        rootPath = _.get(obj.path, 'value.value');
      }
    },
  });
  return rootPath;