How to use the jscodeshift.variableDeclaration 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 / packages / modulizer / src / passes / rewrite-namespace-exports.ts View on Github external
const exportRecords: {name: string, node: estree.Node}[] = [];

  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(
github Polymer / tools / src / passes / rewrite-namespace-exports.ts View on Github external
const exportRecords: {name: string, node: estree.Node}[] = [];

  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(
github 5to6 / 5to6-codemod / utils / main.js View on Github external
singleVarToExpressions: function(ast) {
    // if ast.value, use that... Sometimes you need that to access the node that we care about directly.
    ast = ast.value || ast;

    var expressions = [];
    var declarations = ast.declarations;
    // safety checks
    // am I a single var statement?
    if (ast.type === 'VariableDeclaration' && ast.declarations.length > 1) {
      for (var i = 0; i < declarations.length; i++) {
        var varStatement = j.variableDeclaration('var', [ast.declarations[i]]);
        expressions.push(varStatement);
      }

      // console.log('expressions', expressions);
      return expressions;
      // console.log('inside');
      // console.log('varStatement', varStatement);
    } else {
      console.warn('ERROR: Expected a single var statement. THat\'s NOT what I got:');
      console.log('ast', ast);
    }
  },
github Polymer / tools / src / passes / rewrite-namespace-exports.ts View on Github external
fullyQualifiedName,
          fullyQualifiedNamePath,
          nodePath);
    } else {
      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';
      }
      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 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});
  }