How to use the babel-types.variableDeclarator 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 plasma-umass / Stopify / stopify / src / common / desugarLoop.ts View on Github external
DoWhileStatement: function DoWhileStatement(path: NodePath): void {
    const node = path.node;
    let { test, body } = node;

    // Add flag to run the while loop at least once
    const runOnce = fastFreshId.fresh('runOnce');
    const runOnceInit = t.variableDeclaration('let',
      [t.variableDeclarator(runOnce, t.booleanLiteral(true))]);
    const runOnceSetFalse =
    t.expressionStatement(
      t.assignmentExpression('=', runOnce, t.booleanLiteral(false)));
    body = h.flatBodyStatement([runOnceSetFalse, body]);

    test = t.logicalExpression('||', runOnce, test);

    bh.replaceWithStatements(path,runOnceInit, t.whileStatement(test, body));
  },
github parcel-bundler / parcel / src / scope-hoisting / hoist.js View on Github external
EXPORT_ASSIGN_TEMPLATE({
        EXPORTS: getExportsIdentifier(asset, path.scope),
        NAME: t.identifier('default'),
        LOCAL: t.clone(identifier)
      })
    );

    if (t.isIdentifier(declaration)) {
      // Rename the variable being exported.
      safeRename(path, asset, declaration.name, identifier.name);
      path.remove();
    } else if (t.isExpression(declaration) || !declaration.id) {
      // Declare a variable to hold the exported value.
      path.replaceWith(
        t.variableDeclaration('var', [
          t.variableDeclarator(identifier, t.toExpression(declaration))
        ])
      );

      path.scope.registerDeclaration(path);
    } else {
      // Rename the declaration to the exported name.
      safeRename(path, asset, declaration.id.name, identifier.name);
      path.replaceWith(declaration);
    }

    if (!asset.cacheData.exports['default']) {
      asset.cacheData.exports['default'] = identifier.name;
    }

    // Mark the asset as an ES6 module, so we handle imports correctly in the packager.
    asset.cacheData.isES6Module = true;
github trivago / melody / packages / melody-compiler / src / convert / template.js View on Github external
),
                                [
                                    t.identifier(this.templateVariableName),
                                    t.identifier(path.scope.contextName),
                                ]
                            )
                        )
                    );
                }
            } else {
                this.program.body.splice(
                    0,
                    0,
                    t.exportNamedDeclaration(
                        t.variableDeclaration('const', [
                            t.variableDeclarator(
                                t.identifier(this.templateVariableName),
                                t.objectExpression([])
                            ),
                        ]),
                        []
                    )
                );
            }
            path.replaceWith(path.node);
        },
        exit(path) {
github babel / babel / packages / babel-core / src / transformation / helpers / remap-async-to-generator.js View on Github external
export default function (path, callId) {
  let node = path.node;

  node.async = false;
  node.generator = true;

  path.traverse(awaitVisitor, state);

  let call = t.callExpression(callId, [node]);

  let id = node.id;
  node.id = null;

  if (t.isFunctionDeclaration(node)) {
    let declar = t.variableDeclaration("let", [
      t.variableDeclarator(id, call)
    ]);
    declar._blockHoist = true;
    return declar;
  } else {
    node.type = "FunctionExpression";

    if (id) {
      let state = { id };
      path.traverse(referenceVisitor, state);

      if (state.ref) {
        path.scope.parent.push({ id: state.ref });
        return t.assignmentExpression("=", state.ref, call);
      }
    }
github parcel-bundler / parcel / src / packagers / js / src / JSConcatPackager.js View on Github external
t.expressionStatement(
            t.assignmentExpression(
              '=',
              t.identifier(node.id.name),
              t.toExpression(node)
            )
          )
        );
      } else {
        body.push(node);
      }
    }

    let executed = getName(asset, 'executed');
    decls.push(
      t.variableDeclarator(t.identifier(executed), t.booleanLiteral(false))
    );

    let init = t.functionDeclaration(
      getIdentifier(asset, 'init'),
      [],
      t.blockStatement([
        t.ifStatement(t.identifier(executed), t.returnStatement()),
        t.expressionStatement(
          t.assignmentExpression(
            '=',
            t.identifier(executed),
            t.booleanLiteral(true)
          )
        ),
        ...body
      ])
github roman01la / html-to-react-components / lib / module.js View on Github external
function getCJSImportDeclaration(variable, moduleName) {
  return t.variableDeclaration("const", [
    t.variableDeclarator(
      t.identifier(variable),
      t.callExpression(t.identifier("require"), [t.stringLiteral(moduleName)])
    )
  ]);
}
github Tencent / omi / packages / mps / _scripts / jsx2wxml / functional.js View on Github external
throw utils_1.codeFrameError(id, '函数式组件的参数最多只能传入一个');
                    }
                    else if (params.length === 1) {
                        arg = params[0];
                    }
                    const cloneBody = lodash_1.cloneDeep(body);
                    if (!initialIsCapital(id.name)) {
                        throw utils_1.codeFrameError(id, '组件命名规则请遵守帕斯卡命名法(Pascal Case)');
                    }
                    if (arg) {
                        if (t.isIdentifier(arg)) {
                            cloneBody.body.push(utils_1.buildConstVariableDeclaration(arg.name, t.memberExpression(t.thisExpression(), t.identifier('props'))));
                        }
                        else if (t.isObjectPattern(arg)) {
                            cloneBody.body.push(t.variableDeclaration('const', [
                                t.variableDeclarator(arg, t.memberExpression(t.thisExpression(), t.identifier('props')))
                            ]));
                        }
                        else {
                            throw utils_1.codeFrameError(arg, '函数式组件只支持传入一个简单标识符或使用对象结构');
                        }
                    }
                    const classDecl = t.classDeclaration(id, t.memberExpression(t.identifier('Taro'), t.identifier('Component')), t.classBody([
                        t.classMethod('method', t.identifier('render'), [], cloneBody)
                    ]), []);
                    functionDecl.replaceWith(classDecl);
                }
            }
        }
github jamietre / babel-plugin-transform-es2015-modules-commonjs-simple / src / index.js View on Github external
for (let source in imports) {
            let {specifiers, maxBlockHoist} = imports[source];
            if (specifiers.length) {
              let uid = addRequire(source, maxBlockHoist);

              let wildcard;

              for (let i = 0; i < specifiers.length; i++) {
                let specifier = specifiers[i];
                if (t.isImportNamespaceSpecifier(specifier)) {
                  if (strict) {
                    remaps[specifier.local.name] = uid;
                  } else {
                    const varDecl = t.variableDeclaration("var", [
                      t.variableDeclarator(
                        specifier.local,
                        t.callExpression(
                          this.addHelper("interopRequireWildcard"),
                          [uid]
                        )
                      )
                    ]);

                    if (maxBlockHoist > 0) {
                      varDecl._blockHoist = maxBlockHoist;
                    }

                    topNodes.push(varDecl);
                  }
                  wildcard = specifier.local;
                } else if (t.isImportDefaultSpecifier(specifier)) {
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 babel / babel / packages / babel-helper-explode-assignable-expression / src / index.js View on Github external
function getPropRef(node, nodes, file, scope) {
  let prop = node.property;
  let key = t.toComputedKey(node, prop);
  if (t.isLiteral(key)) return key;

  let temp = scope.generateUidIdentifierBasedOnNode(prop);
  nodes.push(t.variableDeclaration("var", [
    t.variableDeclarator(temp, prop)
  ]));
  return temp;
}