How to use the @babel/core.types.variableDeclarator 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 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 ark120202 / babel-lua / packages / babel-plugin-lua-function-context / src / index.js View on Github external
FunctionDeclaration(path) {
        const { node } = path;

        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 babel / babel / packages / babel-plugin-proposal-object-rest-spread / src / index.js View on Github external
if (!t.isVariableDeclaration(left)) return;

        const pattern = left.declarations[0].id;
        if (!t.isObjectPattern(pattern)) return;

        const key = scope.generateUidIdentifier("ref");
        node.left = t.variableDeclaration(left.kind, [
          t.variableDeclarator(key, null),
        ]);

        path.ensureBlock();

        node.body.body.unshift(
          t.variableDeclaration(node.left.kind, [
            t.variableDeclarator(pattern, t.cloneNode(key)),
          ]),
        );
      },
      // var a = { ...b, ...c }
github babel / babel / packages / babel-plugin-proposal-decorators / src / transformer-legacy.js View on Github external
function decoratedClassToExpression({ node, scope }) {
  if (!hasClassDecorators(node) && !hasMethodDecorators(node.body.body)) {
    return;
  }

  const ref = node.id
    ? t.cloneNode(node.id)
    : scope.generateUidIdentifier("class");

  return t.variableDeclaration("let", [
    t.variableDeclarator(ref, t.toExpression(node)),
  ]);
}
github babel / babel / packages / babel-plugin-proposal-object-rest-spread / src / index.js View on Github external
return;
    }

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

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

    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);
    }
  }
github babel / babel / packages / babel-plugin-transform-for-of / src / index.js View on Github external
ForOfStatement(path) {
          const { scope } = path;
          const { left, right, body } = path.node;
          const i = scope.generateUidIdentifier("i");
          let array = scope.maybeGenerateMemoised(right, true);

          const inits = [t.variableDeclarator(i, t.numericLiteral(0))];
          if (array) {
            inits.push(t.variableDeclarator(array, right));
          } else {
            array = right;
          }

          const item = t.memberExpression(
            t.cloneNode(array),
            t.cloneNode(i),
            true,
          );
          let assignment;
          if (t.isVariableDeclaration(left)) {
            assignment = left;
            assignment.declarations[0].init = item;
          } else {
            assignment = t.expressionStatement(
              t.assignmentExpression("=", left, item),
github babel / babel / packages / babel-helper-create-class-features-plugin / src / fields.js View on Github external
methodId,
    params,
    body,
    generator,
    async,
  );
  const isGetter = getId && !getterDeclared && params.length === 0;
  const isSetter = setId && !setterDeclared && params.length > 0;

  if (isGetter) {
    privateNamesMap.set(prop.node.key.id.name, {
      ...privateName,
      getterDeclared: true,
    });
    return t.variableDeclaration("var", [
      t.variableDeclarator(getId, methodValue),
    ]);
  }
  if (isSetter) {
    privateNamesMap.set(prop.node.key.id.name, {
      ...privateName,
      setterDeclared: true,
    });
    return t.variableDeclaration("var", [
      t.variableDeclarator(setId, methodValue),
    ]);
  }
  if (isStatic && !loose) {
    return t.variableDeclaration("var", [
      t.variableDeclarator(
        id,
        t.functionExpression(id, params, body, generator, async),
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();
    }
  },
};