How to use the babel-traverse.hasType 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 chpio / babel-plugin-transform-async-to-bluebird / main.es2015.js View on Github external
Function(path: NodePath, state: any) {
				const {node, scope} = path;
				if (!node.async || node.generator) return;
				const hasAwait = traverse.hasType(node.body, scope, 'AwaitExpression', FUNCTION_TYPES);

				traverse(node, {
					blacklist: FUNCTION_TYPES,

					AwaitExpression(path2: NodePath) {
						// eslint-disable-next-line no-param-reassign
						path2.node.type = 'YieldExpression';
						path2.node.argument = t.callExpression(
							state.addImport('bluebird', 'resolve'),
							[path2.node.argument]
						);
					},
				}, scope);

				const isClassOrObjectMethod = path.isClassMethod() || path.isObjectMethod();
				(isClassOrObjectMethod ? classOrObjectMethod : plainFunction)(path, state, hasAwait);
github babel / babel / packages / babel-core / src / transformation / transformers / es6 / block-scoping / index.js View on Github external
var ref = fn;

    if (this.loop) {
      ref = this.scope.generateUidIdentifier("loop");
      this.loopPath.insertBefore(t.variableDeclaration("var", [
        t.variableDeclarator(ref, fn)
      ]));
    }

    // build a call and a unique id that we can assign the return value to
    var call = t.callExpression(ref, args);
    var ret  = this.scope.generateUidIdentifier("ret");

    // handle generators
    var hasYield = traverse.hasType(fn.body, this.scope, "YieldExpression", t.FUNCTION_TYPES);
    if (hasYield) {
      fn.generator = true;
      call = t.yieldExpression(call, true);
    }

    // handlers async functions
    var hasAsync = traverse.hasType(fn.body, this.scope, "AwaitExpression", t.FUNCTION_TYPES);
    if (hasAsync) {
      fn.async = true;
      call = t.awaitExpression(call);
    }

    this.buildClosure(ret, call);
  }
github babel / babel / packages / babel-core / src / transformation / transformers / es6 / block-scoping / index.js View on Github external
]));
    }

    // build a call and a unique id that we can assign the return value to
    var call = t.callExpression(ref, args);
    var ret  = this.scope.generateUidIdentifier("ret");

    // handle generators
    var hasYield = traverse.hasType(fn.body, this.scope, "YieldExpression", t.FUNCTION_TYPES);
    if (hasYield) {
      fn.generator = true;
      call = t.yieldExpression(call, true);
    }

    // handlers async functions
    var hasAsync = traverse.hasType(fn.body, this.scope, "AwaitExpression", t.FUNCTION_TYPES);
    if (hasAsync) {
      fn.async = true;
      call = t.awaitExpression(call);
    }

    this.buildClosure(ret, call);
  }
github babel / babel / packages / babel-core / src / transformation / transformers / es7 / comprehensions.js View on Github external
function array(node, parent, scope) {
  var uid = scope.generateUidIdentifierBasedOnNode(parent);

  var container = util.template("array-comprehension-container", {
    KEY: uid
  });
  container.callee.shadow = true;

  var block = container.callee.body;
  var body  = block.body;

  if (traverse.hasType(node, scope, "YieldExpression", t.FUNCTION_TYPES)) {
    container.callee.generator = true;
    container = t.yieldExpression(container, true);
  }

  var returnStatement = body.pop();

  body.push(buildComprehension(node, function () {
    return util.template("array-push", {
      STATEMENT: node.body,
      KEY:       uid
    }, true);
  }));
  body.push(returnStatement);

  return container;
}