How to use the jscodeshift.variableDeclarator function in jscodeshift

To help you get started, we’ve selected a few jscodeshift 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 Polymer / tools / src / passes / rewrite-namespace-exports.ts View on Github external
for (const propNode of namespace.properties) {
    const {key, value} = propNode;
    if (key.type !== 'Identifier') {
      console.warn(`unsupported namespace property type ${key.type}`);
      continue;
    }
    const name = key.name;
    const fullName = `${namespaceName}.${name}`;
    // The expression for an internal `this.` reference to a namespace member
    const thisName = `this.${name}`;
    const isMutable = mutableNames.has(fullName) || mutableNames.has(thisName);
    if (value.type === 'ObjectExpression' || value.type === 'ArrayExpression' ||
        value.type === 'Literal') {
      const node = jsc.exportNamedDeclaration(jsc.variableDeclaration(
          isMutable ? 'let' : 'const', [jsc.variableDeclarator(key, value)]));
      (node as NodeWithComments).comments = getCommentsFromNode(propNode);
      exportRecords.push({name, node});
    } else if (value.type === 'FunctionExpression') {
      const func = value;
      const node = jsc.exportNamedDeclaration(jsc.functionDeclaration(
          key,  // id
          func.params,
          func.body,
          func.generator));
      (node as NodeWithComments).comments = getCommentsFromNode(propNode);
      exportRecords.push({name, node});
    } else if (value.type === 'ArrowFunctionExpression') {
      const isMutable =
          mutableNames.has(fullName) || mutableNames.has(thisName);
      const node = jsc.exportNamedDeclaration(jsc.variableDeclaration(
          isMutable ? 'let' : 'const', [jsc.variableDeclarator(key, value)]));
github Polymer / tools / packages / modulizer / src / passes / rewrite-namespace-exports.ts View on Github external
for (const propNode of namespace.properties) {
    const {key, value} = propNode;
    if (key.type !== 'Identifier') {
      console.warn(`unsupported namespace property type ${key.type}`);
      continue;
    }
    const name = key.name;
    const fullName = `${namespaceName}.${name}`;
    // The expression for an internal `this.` reference to a namespace member
    const thisName = `this.${name}`;
    const isMutable = mutableNames.has(fullName) || mutableNames.has(thisName);
    if (value.type === 'ObjectExpression' || value.type === 'ArrayExpression' ||
        value.type === 'Literal') {
      const node = jsc.exportNamedDeclaration(jsc.variableDeclaration(
          isMutable ? 'let' : 'const', [jsc.variableDeclarator(key, value)]));
      (node as NodeWithComments).comments = getCommentsFromNode(propNode);
      exportRecords.push({name, node});
    } else if (value.type === 'FunctionExpression') {
      const func = value;
      const node = jsc.exportNamedDeclaration(jsc.functionDeclaration(
          key,  // id
          func.params,
          func.body,
          func.generator));
      (node as NodeWithComments).comments = getCommentsFromNode(propNode);
      exportRecords.push({name, node});
    } else if (value.type === 'ArrowFunctionExpression') {
      const isMutable =
          mutableNames.has(fullName) || mutableNames.has(thisName);
      const node = jsc.exportNamedDeclaration(jsc.variableDeclaration(
          isMutable ? 'let' : 'const', [jsc.variableDeclarator(key, value)]));
github cpojer / js-codemod / extensions / imports / addImport.js View on Github external
function reprintNode(node) {
  if (j.ExpressionStatement.check(node)) {
    return statement`${node.expression}`;
  }

  if (j.VariableDeclaration.check(node)) {
    const declaration = node.declarations[0];
    return j.variableDeclaration(node.kind, [
      j.variableDeclarator(declaration.id, declaration.init),
    ]);
  }

  if (j.ImportDeclaration.check(node) && node.importKind === 'type') {
    // TODO: Properly remove new lines from the node.
    return node;
  }

  return node;
}
github Polymer / tools / src / passes / rewrite-namespace-exports.ts View on Github external
private rewriteLocalDeclaration(
      path: NodePath, value: estree.Expression, memberPath: string[]) {
    const nameExportedAs = memberPath[memberPath.length - 1];
    const fullyQualifiedName = memberPath.join('.');
    this.namespaceNames.add(fullyQualifiedName);

    // Note: in other place in this file where we decide between let and const
    // exports, we check if the "this." name is mutable as well, but in this
    // case it's very unlikely that a name assigned imperatively, like NS.foo =
    // is otherwise accessed via "this."
    replacePreservingComments(
        path,
        jsc.exportNamedDeclaration(jsc.variableDeclaration(
            this.mutableNames.has(fullyQualifiedName) ? 'let' : 'const',
            [jsc.variableDeclarator(jsc.identifier(nameExportedAs), value)])));
    this.exportMigrationRecords.push(
        {oldNamespacedName: fullyQualifiedName, es6ExportName: nameExportedAs});
  }
github Polymer / tools / packages / modulizer / src / passes / rewrite-namespace-exports.ts View on Github external
private rewriteLocalDeclaration(
      path: NodePath, value: estree.Expression, memberPath: string[]) {
    const nameExportedAs = memberPath[memberPath.length - 1];
    const fullyQualifiedName = memberPath.join('.');
    this.namespaceNames.add(fullyQualifiedName);

    // Note: in other place in this file where we decide between let and const
    // exports, we check if the "this." name is mutable as well, but in this
    // case it's very unlikely that a name assigned imperatively, like NS.foo =
    // is otherwise accessed via "this."
    replacePreservingComments(
        path,
        jsc.exportNamedDeclaration(jsc.variableDeclaration(
            this.mutableNames.has(fullyQualifiedName) ? 'let' : 'const',
            [jsc.variableDeclarator(jsc.identifier(nameExportedAs), value)])));
    this.exportMigrationRecords.push(
        {oldNamespacedName: fullyQualifiedName, es6ExportName: nameExportedAs});
  }
github Polymer / tools / packages / modulizer / src / passes / rewrite-namespace-exports.ts View on Github external
let name = fullyQualifiedNamePath[fullyQualifiedNamePath.length - 1];
      // Special Polymer workaround: Register & rewrite the
      // `Polymer._polymerFn` export as if it were the `Polymer()`
      // namespace function.
      let correctedNamespaceName = fullyQualifiedName;
      if (fullyQualifiedName === 'Polymer._polymerFn') {
        correctedNamespaceName = 'Polymer';
        name = 'Polymer';
      } else if (fullyQualifiedName === 'Polymer.Element') {
        name = 'PolymerElement';
      }
      const variableKind =
          this.mutableNames.has(correctedNamespaceName) ? 'let' : 'const';
      const newExportNode = jsc.exportNamedDeclaration(jsc.variableDeclaration(
          variableKind,
          [jsc.variableDeclarator(jsc.identifier(name), exportedExpression)]));
      replacePreservingComments(nodePath, newExportNode);
      this.exportMigrationRecords.push(
          {oldNamespacedName: correctedNamespaceName, es6ExportName: name});
    }
  }
github Astrocoders / old-astroman / lib / utils / js / reduxAction / appendConstant.js View on Github external
export default function appendConstant({ast, name, value}) {
  const newVariableDeclaration = j.variableDeclaration('const', [
    j.variableDeclarator(j.identifier(name), j.literal(value)),
  ])
  const newExport = j.exportNamedDeclaration(newVariableDeclaration, [], null)
  const exports = ast
    .find(j.ExportNamedDeclaration)

  return exports
    .at(exports.length - 1)
    .insertAfter(newExport)
}
github Polymer / tools / src / document-util.ts View on Github external
export function createDomNodeInsertStatements(
    nodes: parse5.ASTNode[], activeInBody = false): estree.Statement[] {
  const varName = `$_documentContainer`;
  const fragment = {
    nodeName: '#document-fragment',
    attrs: [],
    childNodes: nodes,
  };
  const templateValue = serializeNodeToTemplateLiteral(fragment as any, false);

  const createElementDiv = jsc.variableDeclaration(
      'const',
      [jsc.variableDeclarator(
          jsc.identifier(varName),
          jsc.callExpression(
              jsc.memberExpression(
                  jsc.identifier('document'), jsc.identifier('createElement')),
              [jsc.literal('template')]))]);
  const setDocumentContainerStatement =
      jsc.expressionStatement(jsc.assignmentExpression(
          '=',
          jsc.memberExpression(
              jsc.identifier(varName), jsc.identifier('innerHTML')),
          templateValue));
  if (activeInBody) {
    return [
      createElementDiv,
      setDocumentContainerStatement,
      jsc.expressionStatement(jsc.callExpression(