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 parcel-bundler / parcel / src / scope-hoisting / hoist.js View on Github external
function safeRename(path, asset, from, to) {
  if (from === to) {
    return;
  }

  // If the binding that we're renaming is constant, it's safe to rename it.
  // Otherwise, create a new binding that references the original.
  let binding = path.scope.getBinding(from);
  if (binding && binding.constant) {
    rename(path.scope, from, to);
  } else {
    let [decl] = path.insertAfter(
      t.variableDeclaration('var', [
        t.variableDeclarator(t.identifier(to), t.identifier(from))
      ])
    );

    path.scope.getBinding(from).reference(decl.get('declarations.0.init'));
    path.scope.registerDeclaration(decl);
  }
}
github gajus / flow-runtime / packages / babel-plugin-flow-runtime / src / firstPassVisitors.js View on Github external
Class (path: NodePath) {
      let className = 'AnonymousClass';
      if (path.isClassDeclaration() && path.has('id')) {
        const {name} = path.node.id;
        // have we seen a reference to this class already?
        // if so we should replace it with a `var className = class className {}`
        // to avoid temporal dead zone issues
        if (!path.parentPath.isExportDefaultDeclaration() && path.scope.getData(`seenReference:${name}`)) {
          path.replaceWith(t.variableDeclaration('var', [
            t.variableDeclarator(
              t.identifier(name),
              t.classExpression(
                path.node.id,
                path.node.superClass,
                path.node.body,
                path.node.decorators || []
              )
            )
          ]));
          return;
        }
        className = name;
        context.defineValue(name, path.parentPath);
      }
      context.setClassData(
        path,
github gajus / flow-runtime / packages / babel-plugin-flow-runtime / src / patternMatchVisitors.js View on Github external
}
      }
      else {
        // this is just a normal param
        replacement = t.identifier(name);
      }

      if (param.typeAnnotation) {
        test.push(
          inlineTest(context, param.typeAnnotation, replacement),
        );
      }

      if (param.isPattern) {
        blockPrelude.push(t.variableDeclaration('let', [
          t.variableDeclarator(
            param.path.node,
            replacement
          )
        ]));
      }
      else if (useBinding) {
        const binding = block.scope.getBinding(param.name);
        for (const refPath of binding.referencePaths) {
          if (refPath.isIdentifier()) {
            refPath.replaceWith(replacement);
          }
          else {
            // we've already replaced this, go looking for the reference.
            refPath.traverse({
              Identifier (id: NodePath) {
                if (id.node.name === param.name) {
github salesforce / lwc / packages / @lwc / template-compiler / src / codegen / formatters / function.ts View on Github external
function moduleNameToLookup(name: string): t.VariableDeclaration {
    const localIdentifier = identifierFromComponentName(name);

    return t.variableDeclaration('const', [
        t.variableDeclarator(
            localIdentifier,
            t.memberExpression(
                t.identifier(TEMPLATE_MODULES_PARAMETER),
                t.stringLiteral(name),
                true
            )
        ),
    ]);
}
github gajus / flow-runtime / packages / babel-plugin-flow-runtime / src / convert.js View on Github external
const args = [t.stringLiteral(typeParameter.node.name)];
  if (typeParameter.node.bound) {
    args.push(convert(context, typeParameter.get('bound')));
    if (typeParameter.node.default) {
      args.push(
        convert(context, typeParameter.get('default'))
      );
    }
  }
  else if (typeParameter.node.default) {
    args.push(
      t.identifier('undefined'),
      convert(context, typeParameter.get('default'))
    );
  }
  return t.variableDeclarator(
    t.identifier(typeParameter.node.name),
    t.callExpression(
      t.memberExpression(
        t.identifier(name),
        t.identifier('typeParameter')
      ),
      args
    )
  );
}
github jridgewell / babel-plugin-transform-incremental-dom / src / helpers / inline-expressions.js View on Github external
declarator.replaceWithMultiple(closureVars.map((cv) => {
      return t.variableDeclarator(cv.id, cv.init);
    }));
  }
github peakchen90 / babel-plugin-react-directives / src / directives / model.js View on Github external
t.memberExpression(
              varId,
              t.identifier('splice')
            ),
            [
              node,
              t.numericLiteral(1),
              value
            ]
          )
        )
      ];
    }
    return [
      t.variableDeclaration('let', [
        t.variableDeclarator(
          varId,
          t.objectExpression([
            resolveExp && t.spreadElement(resolveExp),
            t.objectProperty(
              node,
              value
            )
          ].filter(Boolean))
        )
      ])
    ];
  };
github hsiaosiyuan0 / jlua / src / js / code.js View on Github external
);
        this.scope.inClassMethod = true;
        this.scope.inClass = klass.name;
      }
    }
    this.enterScope();
    this.scope.inFnDec = true;
    const params = node.params.map(expr => {
      this.scope.addLocal(expr.name);
      return t.identifier(expr.name);
    });
    const body = t.blockStatement(node.body.map(stmt => this.visitStmt(stmt)));
    if (node.isLocal) {
      return loc(
        t.variableDeclaration("let", [
          t.variableDeclarator(id, t.functionExpression(null, params, body))
        ]),
        node
      );
    }
    this.leaveScope();
    if (id.type === NodeType.Identifier) id = JsCodegen.refJlua(id);
    return loc(
      t.expressionStatement(
        t.assignmentExpression(
          "=",
          id,
          t.functionExpression(null, params, body)
        )
      ),
      node
    );
github remaxjs / remax / packages / remax-cli / src / build / plugins / page.ts View on Github external
ExportDefaultDeclaration: (path: NodePath) => {
      if (t.isExpression(path.node.declaration)) {
        const pageId = path.scope.generateUidIdentifier('page');
        const declaration = path.node.declaration;
        path.replaceWith(
          t.variableDeclaration('const', [
            t.variableDeclarator(pageId, declaration),
          ])
        );
        pageConfigExpression(path, pageId);
        path.stop();
      } else if (
        t.isFunctionDeclaration(path.node.declaration) ||
        t.isClassDeclaration(path.node.declaration)
      ) {
        const declaration = path.node.declaration;
        const pageId = path.scope.generateUidIdentifierBasedOnNode(path.node);
        declaration.id = pageId;
        path.replaceWith(declaration);
        pageConfigExpression(path, pageId);
        path.stop();
      }
    },