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 parcel-bundler / parcel / packages / core / parcel-bundler / 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 facebook / prepack / src / react / utils.js View on Github external
additionalFunctionEffects.transforms.push((body: Array) => {
    // as we've converted a functional component to a complex one, we are going to have issues with
    // "props" and "context" references, as they're now going to be "this.props" and "this.context".
    // we simply need a to add to vars to beginning of the body to get around this
    // if they're not used, any DCE tool post-Prepack (GCC or Uglify) will remove them
    body.unshift(
      t.variableDeclaration("var", [
        t.variableDeclarator(t.identifier("props"), t.memberExpression(t.thisExpression(), t.identifier("props"))),
        t.variableDeclarator(t.identifier("context"), t.memberExpression(t.thisExpression(), t.identifier("context"))),
      ])
    );
  });
}
github gajus / flow-runtime / packages / babel-plugin-flow-runtime / src / transformVisitors.js View on Github external
convert(context, typeParameter.get('default'))
          );
        }
        else if (typeParameter.has('bound')) {
          args.push(
            convert(context, typeParameter.get('bound'))
          );
        }
        else if (typeParameter.has('default')) {
          args.push(
            t.identifier('undefined'), // make sure we don't confuse bound with default
            convert(context, typeParameter.get('default'))
          );
        }

        definitions.push(t.variableDeclaration('const', [
          t.variableDeclarator(
            t.identifier(name),
            context.call('typeParameter', ...args)
          )
        ]));
      }

      for (let param of params) {
        const argumentIndex = +param.key;
        let assignmentRight;
        if (param.isAssignmentPattern()) {
          assignmentRight = param.get('right');
          param = param.get('left');
        }

        if (!param.has('typeAnnotation')) {
github teleporthq / teleport-code-generators / packages / teleport-react-app-routing / src / utils.ts View on Github external
export const makePureComponent = (params: { name: string; jsxTagTree: t.JSXElement }) => {
  const { name, jsxTagTree } = params
  const returnStatement = t.returnStatement(jsxTagTree)
  const arrowFunction = t.arrowFunctionExpression(
    [t.identifier('props')],
    t.blockStatement([returnStatement] || [])
  )

  const declarator = t.variableDeclarator(t.identifier(name), arrowFunction)
  const component = t.variableDeclaration('const', [declarator])

  return component
}
github gajus / flow-runtime / packages / babel-plugin-flow-runtime / src / transformVisitors.js View on Github external
convert(context, typeParameter)
                );
              }))
            )
          ));
        }
      }


      if (hasTypeParameters) {
        const staticMethods = body.get('body').filter(
          item => item.isClassMethod() && item.node.static
        );

        for (const method of staticMethods) {
          method.get('body').unshiftContainer('body', t.variableDeclaration('const', [
            t.variableDeclarator(
              typeParametersUid,
              t.objectExpression(typeParameters.map(typeParameter => {
                return t.objectProperty(
                  t.identifier(typeParameter.node.name),
                  convert(context, typeParameter)
                );
              }))
            )
          ]));
        }
      }
    }},
github gajus / flow-runtime / packages / babel-plugin-flow-runtime / src / transformVisitors.js View on Github external
Class: { exit (path: NodePath) {
      if (context.shouldSuppressPath(path)) {
        path.skip();
        return;
      }
      else if (!shouldCheck) {
        return;
      }

      const body = path.get('body');

      const typeParametersSymbolUid = context.getClassData(path, 'typeParametersSymbolUid');

      if (typeParametersSymbolUid) {
        path.getStatementParent().insertBefore(
          t.variableDeclaration('const', [
            t.VariableDeclarator(
              t.identifier(typeParametersSymbolUid),
              t.callExpression(
                t.identifier('Symbol'),
                [t.stringLiteral(
                  `${context.getClassData(path, 'currentClassName')}TypeParameters`
                )]
              )
            )
          ])
        );

        const staticProp = t.classProperty(
          context.symbol('TypeParameters'),
          t.identifier(typeParametersSymbolUid),
          null,
github cube-js / cube.js / packages / cubejs-server-core / dev / templates / SourceSnippet.js View on Github external
handleExistingMerge(existingDefinition, newDefinition, allHistoryDefinitions) {
    const historyDefinitions = allHistoryDefinitions.map(definitions => (
      definitions.find(
        d => d.get('id').node.type === 'Identifier' &&
          existingDefinition.get('id').node.type === 'Identifier' &&
          existingDefinition.get('id').node.name === d.get('id').node.name
      )
    )).filter(d => !!d);
    const lastHistoryDefinition = historyDefinitions.length && historyDefinitions[historyDefinitions.length - 1];
    const newVariableDeclaration = t.variableDeclaration('const', [newDefinition.node]);
    if (
      !this.compareDefinitions(existingDefinition, lastHistoryDefinition) &&
      !this.compareDefinitions(existingDefinition, newDefinition)
    ) {
      t.addComment(newVariableDeclaration, 'leading', `\n${this.generateCode(existingDefinition, false)}`);
    }
    if (existingDefinition.node.type === 'VariableDeclarator') {
      existingDefinition = existingDefinition.parentPath;
    }
    existingDefinition.replaceWith(newVariableDeclaration);
  }
github benjycui / bisheng / packages / bisheng-plugin-react / lib / transformer.js View on Github external
function requireGenerator(varName, moduleName) {
  return types.variableDeclaration('var', [
    types.variableDeclarator(
      types.identifier(varName),
      types.callExpression(types.identifier('require'), [
        types.stringLiteral(moduleName),
      ]),
    ),
  ]);
}
const defaultBabelConfig = {
github ismail-codar / fidan / packages / deprecated-babel-plugin-transform-jsx / src / modify.ts View on Github external
const insertPragma = (body: t.BaseNode[], pragma: string, start: number) => {
  body.splice(
    start,
    0,
    t.variableDeclaration("const", [
      t.variableDeclarator(
        t.identifier(pragma),
        t.memberExpression(t.identifier("fidan"), t.identifier("createElement"))
      )
    ])
  );
};
github parcel-bundler / parcel / src / packagers / JSConcatPackager.js View on Github external
getIdentifier(asset, 'init'),
      [],
      t.blockStatement([
        t.ifStatement(t.identifier(executed), t.returnStatement()),
        t.expressionStatement(
          t.assignmentExpression(
            '=',
            t.identifier(executed),
            t.booleanLiteral(true)
          )
        ),
        ...body
      ])
    );

    return [t.variableDeclaration('var', decls), ...fns, init];
  }