How to use the @babel/core.types.variableDeclaration function in @babel/core

To help you get started, we’ve selected a few @babel/core 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 babel / babel / packages / babel-plugin-transform-for-of / src / index.js View on Github external
t.identifier(stepKey),
      t.identifier("value"),
    );

    if (
      t.isIdentifier(left) ||
      t.isPattern(left) ||
      t.isMemberExpression(left)
    ) {
      // for (i of test), for ({ i } of test)
      declar = t.expressionStatement(
        t.assignmentExpression("=", left, stepValue),
      );
    } else if (t.isVariableDeclaration(left)) {
      // for (let i of test)
      declar = t.variableDeclaration(left.kind, [
        t.variableDeclarator(left.declarations[0].id, stepValue),
      ]);
    } else {
      throw file.buildCodeFrameError(
        left,
        `Unknown node type ${left.type} in ForStatement`,
      );
    }

    const template = buildForOf({
      ITERATOR_HAD_ERROR_KEY: scope.generateUidIdentifier("didIteratorError"),
      ITERATOR_COMPLETION: scope.generateUidIdentifier(
        "iteratorNormalCompletion",
      ),
      ITERATOR_ERROR_KEY: scope.generateUidIdentifier("iteratorError"),
      ITERATOR_KEY: scope.generateUidIdentifier("iterator"),
github babel / babel / packages / babel-plugin-transform-block-scoped-functions / src / index.js View on Github external
function statementList(key, path) {
    const paths: Array = path.get(key);

    for (const path of paths) {
      const func = path.node;
      if (!path.isFunctionDeclaration()) continue;

      const declar = t.variableDeclaration("let", [
        t.variableDeclarator(func.id, t.toExpression(func)),
      ]);

      // hoist it up above everything else
      declar._blockHoist = 2;

      // todo: name this
      func.id = null;

      path.replaceWith(declar);
    }
  }
github babel / babel / packages / babel-plugin-transform-parameters / src / rest.js View on Github external
export default function convertFunctionRest(path) {
  const { node, scope } = path;
  if (!hasRest(node)) return false;

  let rest = node.params.pop().argument;

  const argsId = t.identifier("arguments");

  if (t.isPattern(rest)) {
    const pattern = rest;
    rest = scope.generateUidIdentifier("ref");

    const declar = t.variableDeclaration("let", [
      t.variableDeclarator(pattern, rest),
    ]);
    node.body.body.unshift(declar);
  }

  // check and optimise for extremely common cases
  const state = {
    references: [],
    offset: node.params.length,

    argumentsNode: argsId,
    outerBinding: scope.getBindingIdentifier(rest.name),

    // candidate member expressions we could optimise if there are no other references
    candidates: [],
github ark120202 / babel-lua / packages / babel-plugin-lua-generator-to-coroutine / src / index.js View on Github external
FunctionDeclaration(path) {
        const { node } = path;
        if (!node.generator) return;

        /* path.remove();
        path.scope.parent.push({ id: node.id, init: t.toExpression(node) }); */

        const declaration = t.variableDeclaration('const', [
          t.variableDeclarator(node.id, t.toExpression(node)),
        ]);
        node.id = null;

        // TODO: FunctionDeclaration in Lua has different scoping rules from JS, so hoisting it breaks helper
        // declaration._blockHoist = 2;

        path.replaceWith(declaration);
      },
github dosentmatter / babel-plugin-const-enum / src / const-object.js View on Github external
TSEnumDeclaration(path) {
    if (path.node.const) {
      path.replaceWith(
        types.variableDeclaration('const', [
          types.variableDeclarator(
            types.identifier(path.node.id.name),
            types.objectExpression(
              TSEnumMembersToObjectProperties(path.get('members')),
            ),
          ),
        ]),
      );
      path.skip();
    }
  },
};
github babel / babel / packages / babel-plugin-transform-for-of / src / index.js View on Github external
let assignment;
          if (t.isVariableDeclaration(left)) {
            assignment = left;
            assignment.declarations[0].init = item;
          } else {
            assignment = t.expressionStatement(
              t.assignmentExpression("=", left, item),
            );
          }

          const block = t.toBlock(body);
          block.body.unshift(assignment);

          path.replaceWith(
            t.forStatement(
              t.variableDeclaration("let", inits),
              t.binaryExpression(
                "<",
                t.cloneNode(i),
                t.memberExpression(t.cloneNode(array), t.identifier("length")),
              ),
              t.updateExpression("++", t.cloneNode(i)),
              block,
            ),
          );
        },
      },
github babel / babel / packages / babel-plugin-transform-destructuring / src / index.js View on Github external
buildVariableDeclaration(id, init) {
      const declar = t.variableDeclaration("var", [
        t.variableDeclarator(t.cloneNode(id), t.cloneNode(init)),
      ]);
      declar._blockHoist = this.blockHoist;
      return declar;
    }
github danya / react-local / src / index.js View on Github external
function createCreateElementRef(path) {
    const reference = path.scope.generateUidIdentifier('createElement')
    const declaration = t.variableDeclaration('const', [
      t.variableDeclarator(
        reference,
        t.memberExpression(t.identifier('React'), t.identifier('createElement'))
      )
    ])
    path.insertAfter(declaration)
    path.scope.registerDeclaration(path.getNextSibling())
    return reference
  }
github babel / babel / packages / babel-plugin-proposal-object-rest-spread / src / index.js View on Github external
replaceRestElement(parentPath, paramPath.get("left"));
      return;
    }

    if (paramPath.isArrayPattern() && hasRestElement(paramPath)) {
      const elements = paramPath.get("elements");

      for (let i = 0; i < elements.length; i++) {
        replaceRestElement(parentPath, elements[i]);
      }
    }

    if (paramPath.isObjectPattern() && hasRestElement(paramPath)) {
      const uid = parentPath.scope.generateUidIdentifier("ref");

      const declar = t.variableDeclaration("let", [
        t.variableDeclarator(paramPath.node, uid),
      ]);

      parentPath.ensureBlock();
      parentPath.get("body").unshiftContainer("body", declar);
      paramPath.replaceWith(t.cloneNode(uid));
    }
  }
github babel / babel / packages / babel-plugin-transform-classes / src / transformClass.js View on Github external
function insertProtoAliasOnce() {
    if (classState.protoAlias === null) {
      setState({ protoAlias: classState.scope.generateUidIdentifier("proto") });
      const classProto = t.memberExpression(
        classState.classRef,
        t.identifier("prototype"),
      );
      const protoDeclaration = t.variableDeclaration("var", [
        t.variableDeclarator(classState.protoAlias, classProto),
      ]);

      classState.body.push(protoDeclaration);
    }
  }