How to use the babel-types.variableDeclaration function in babel-types

To help you get started, we’ve selected a few babel-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 codemix / babel-plugin-macros / src / Macro.js View on Github external
}
        },
        FunctionDeclaration() {
          // @todo need correct rename. now renames only usages, but not function
          throw new Error('FunctionDeclaration in macros are not supported temporarily');
        },
        Function(subPath) {
          subPath.skip();
        }
      }, scope);


      if (t.isStatement(cloned.body)) {
        const parentBlock = getParentBlock(path);
        parentBlock.insertBefore([
          t.variableDeclaration('let', [
            t.variableDeclarator(uid)
          ])
        ]);

        if (hasMultipleReturn) {
          parentBlock.insertBefore(t.labeledStatement(labelUid, cloned.body));
        }
        else {
          parentBlock.insertBefore(cloned.body.body);
        }
        path.replaceWith(uid);
      }
      else {
        path.replaceWith(cloned.body);
      }
    };
github babel / babel / packages / babel-core / src / transformation / transformers / es6 / block-scoping / index.js View on Github external
// build the closure that we're going to wrap the block with
    var fn = t.functionExpression(null, params, t.blockStatement(block.body));
    fn.shadow = true;

    // continuation
    this.addContinuations(fn);

    // replace the current block body with the one we're going to build
    block.body = this.body;

    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
github forivall / tacoscript / src / scope / lib / renamer.js View on Github external
maybeConvertFromClassFunctionDeclaration(path) {
    return; // TODO

    // retain the `name` of a class/function declaration

    if (!path.isFunctionDeclaration() && !path.isClassDeclaration()) return;
    if (this.binding.kind !== "hoisted") return;

    path.node.id = t.identifier(this.oldName);
    path.node._blockHoist = 3;

    path.replaceWith(t.variableDeclaration("let", [
      t.variableDeclarator(t.identifier(this.newName), t.toExpression(path.node))
    ]));
  }
github babel / babel / packages / babel-plugin-transform-es2015-parameters / src / destructuring.js View on Github external
Function(path) {
    let params = path.get("params");

    // If there's a rest param, no need to loop through it. Also, we need to
    // hoist one more level to get `declar` at the right spot.
    let hoistTweak = t.isRestElement(params[params.length - 1]) ? 1 : 0;
    let outputParamsLength = params.length - hoistTweak;

    for (let i = 0; i < outputParamsLength; i++) {
      let param = params[i];
      if (param.isArrayPattern() || param.isObjectPattern()) {
        let uid = path.scope.generateUidIdentifier("ref");

        let declar = t.variableDeclaration("let", [
          t.variableDeclarator(param.node, uid)
        ]);
        declar._blockHoist = outputParamsLength - i;

        path.ensureBlock();
        path.get("body").unshiftContainer("body", declar);

        param.replaceWith(uid);
      }
    }
  }
};
github ifgyong / demo / React-native / Helloword / node_modules / babel-plugin-transform-regenerator / node_modules / regenerator-transform / src / visit.js View on Github external
t.assertIdentifier(node.id);

    const blockPath = funPath.findParent(function (path) {
        return path.isProgram() || path.isBlockStatement();
    });

    if (!blockPath) {
        return node.id;
    }

    const block = blockPath.node;
    assert.ok(Array.isArray(block.body));

    const info = getMarkInfo(block);
    if (!info.decl) {
        info.decl = t.variableDeclaration("var", []);
        blockPath.unshiftContainer("body", info.decl);
        info.declPath = blockPath.get("body.0");
    }

    assert.strictEqual(info.declPath.node, info.decl);

    // Get a new unique identifier for our marked variable.
    const markedId = blockPath.scope.generateUidIdentifier("marked");
    const markCallExp = t.callExpression(
        util.runtimeProperty("mark"),
        [node.id]
    );

    const index = info.decl.declarations.push(
        t.variableDeclarator(markedId, markCallExp)
    ) - 1;
github babel / babel / packages / babel-core / src / transformation / transformers / es6 / for-of.js View on Github external
var spec = function (node, parent, scope, file) {
  var left = node.left;
  var declar;

  var stepKey   = scope.generateUidIdentifier("step");
  var stepValue = t.memberExpression(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 (var i of test)
    declar = t.variableDeclaration(left.kind, [
      t.variableDeclarator(left.declarations[0].id, stepValue)
    ]);
  } else {
    throw file.errorWithNode(left, messages.get("unknownForHead", left.type));
  }

  //

  var iteratorKey = scope.generateUidIdentifier("iterator");

  var template = util.template("for-of", {
    ITERATOR_HAD_ERROR_KEY: scope.generateUidIdentifier("didIteratorError"),
    ITERATOR_COMPLETION:    scope.generateUidIdentifier("iteratorNormalCompletion"),
    ITERATOR_ERROR_KEY:     scope.generateUidIdentifier("iteratorError"),
    ITERATOR_KEY:           iteratorKey,
    STEP_KEY:               stepKey,
github babel / babel / packages / babel-plugin-transform-es2015-classes / src / vanilla.js View on Github external
// this super call is the last statement in the body so we can just straight up
      // turn it into a return

      if (this.superThises.length || bareSuperAfter.length) {
        bareSuper.scope.push({ id: thisRef });
        call = t.assignmentExpression("=", thisRef, call);
      }

      if (bareSuperAfter.length) {
        call = t.toSequenceExpression([call, ...bareSuperAfter, thisRef]);
      }

      bareSuper.parentPath.replaceWith(t.returnStatement(call));
    } else {
      bareSuper.replaceWithMultiple([
        t.variableDeclaration("var", [
          t.variableDeclarator(thisRef, call)
        ]),
        ...bareSuperAfter,
        t.expressionStatement(thisRef)
      ]);
    }

  }
github Tencent / omi / packages / cax-omip / scripts / taro-transformer-wx / lib / src / utils.js View on Github external
function buildConstVariableDeclaration(variableName, expresion) {
    return t.variableDeclaration('const', [
        t.variableDeclarator(t.identifier(variableName), expresion)
    ]);
}
exports.buildConstVariableDeclaration = buildConstVariableDeclaration;
github NervJS / taro / packages / taro-transformer-wx / src / utils.ts View on Github external
export function buildConstVariableDeclaration (
  variableName: string,
  expresion: t.Expression
) {
  return t.variableDeclaration('const', [
    t.variableDeclarator(t.identifier(variableName), expresion)
  ])
}
github makuga01 / dnsFookup / FE / node_modules / cssstyle / scripts / generate_properties.js View on Github external
externalDependencies.forEach(function(filename, i) {
  var id = t.identifier(
    'external_dependency_' + basename(filename, '.js').replace(/[^A-Za-z]/g, '') + '_' + i
  );
  moduleExportsByPath[filename] = { defaultExports: id };
  var relativePath = path.relative(path.resolve(__dirname + '/../lib'), filename);
  if (relativePath[0] !== '.') {
    relativePath = './' + relativePath;
  }
  statements.push(
    t.variableDeclaration('var', [
      t.variableDeclarator(
        id,
        t.callExpression(t.identifier('require'), [t.stringLiteral(relativePath)])
      ),
    ])
  );
});
function getRequireValue(node, file) {