How to use the ast-types.builders.blockStatement function in ast-types

To help you get started, we’ve selected a few ast-types 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 stoplightio / spectral / test-harness / codegen / generate.ts View on Github external
function evalExpression(expr: k.ExpressionKind, scope: object, context: vm.Context) {
  return vm.compileFunction(
    recast.print(b.withStatement(b.identifier('scope'), b.blockStatement([b.returnStatement(expr)]))).code,
    ['scope'],
    {
      parsingContext: context,
    },
  )(scope);
}
github probmods / webppl / src / transforms / linearize.js View on Github external
clause(Syntax.IfStatement, function(test, consequent, alternate) {
        if (alternate === null) {
          alternate = build.emptyStatement();
        }

        if (hasReturn(consequent) || hasReturn(alternate)) {
          return [build.ifStatement(
              test,
              build.blockStatement(linearizeSequence([consequent], 0, ks)),
              build.blockStatement(linearizeSequence([alternate], 0, ks)))];
        }
        else {
          return [build.ifStatement(test, consequent, alternate)].concat(ks);
        }
      }),
      clause(Syntax.ReturnStatement, function(argument) {
github rainforestapp / decaf / src / parser.js View on Github external
function mapSwitchExpression(node: CNode, meta: Object) {
  return b.callExpression(
    b.arrowFunctionExpression(
      [],
      b.blockStatement([addReturnStatementsToSwitch(
        mapSwitchStatement(node, meta)
      )])
    ),
    []
  );
}
github rainforestapp / decaf / src / parser.js View on Github external
function mapElseBlock(node: CNode, meta: Object) {
  const type = node.constructor.name;

  if (type === 'If') {
    const conditional = mapIfStatement(node, meta);
    if (n.IfStatement.check(conditional)) {
      return conditional;
    }
    return b.blockStatement([conditional]);
  } else if (type === 'Block') {
    return mapBlockStatement(node, meta);
  }

  return mapBlockStatement({expressions: [node]}, meta);
}
github rainforestapp / decaf / src / parser.js View on Github external
function conditionalStatementAsExpression(node: CNode, meta: Object) {
  const conditionalStatement = mapConditionalStatement(node, meta);

  if (conditionalStatement.type === 'IfStatement') {
    return b.callExpression(
      b.arrowFunctionExpression(
        [],
        b.blockStatement(
          [addReturnStatementToIfBlocks(conditionalStatement)]
        )
      ),
      []
    );
  }

  return conditionalStatement.expression;
}
github babel / babel / lib / 6to5 / transformers / block-binding.js View on Github external
return node;
  });

  //

  var argumentsId = util.aliasArguments(generateUid, node);

  if (argumentsId) {
    nodes.push(b.variableDeclaration("var", [
      b.variableDeclarator(argumentsId, b.identifier("arguments"))
    ]));
  }

  //

  var block = b.blockStatement([]);
  block.body = node;

  var func = b.functionExpression(null, [], block, false);

  //

  var templateName = "function-call";
  if (traverse.hasType(node, "ThisExpression")) templateName += "-this";
  if (traverse.hasType(node, "ReturnStatement", traverse.FUNCTION_TYPES)) templateName += "-return";

  nodes.push(util.template(templateName, {
    FUNCTION: func
  }, true));

  return {
    node: nodes,
github paeckchen / paeckchen / packages / paeckchen-core / src / plugins / es6-export.ts View on Github external
identifier,
              b.identifier('exports'),
              false
            )
          ]
        ),
        b.identifier('forEach'),
        false
      ),
      [
        b.functionExpression(
          null,
          [
            b.identifier('key')
          ],
          b.blockStatement([
            b.expressionStatement(
              b.assignmentExpression(
                '=',
                b.memberExpression(
                  b.memberExpression(
                    b.identifier('module'),
                    b.identifier('exports'),
                    false
                  ),
                  b.identifier('key'),
                  true
                ),
                b.memberExpression(
                  b.memberExpression(
                    identifier,
                    b.identifier('exports'),
github probmods / webppl / src / syntax.js View on Github external
clause(Syntax.BlockStatement, function(body) {
        return build.blockStatement(returnify(body));
      }),
      clause(Syntax.EmptyStatement, function() {
github probmods / webppl / src / syntax.js View on Github external
clause(Syntax.IfStatement, function(test, consequent, alternate) {
        return build.ifStatement(
            test,
            build.blockStatement(returnify(consequent.body)),
            alternate === null ? null : build.blockStatement(returnify(alternate.body)));
      }),
      clause(Syntax.ReturnStatement, function(argument) {
github probmods / webppl / src / transforms / stack.js View on Github external
function transformContinuation(node) {
  var saveAddress = build.expressionStatement(
      build.callExpression(
      build.memberExpression(
      build.identifier(saveAddressFn[0]),
      build.identifier(saveAddressFn[1])
      ), [
        build.identifier(globalVarName),
        build.identifier(localVarName)]));

  var expr = build.functionExpression(
      node.id,
      node.params,
      build.blockStatement([saveAddress].concat(node.body.body))
      );
  expr.loc = node.loc;
  return expr;
}