How to use the jscodeshift.identifier 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 / document-processor.ts View on Github external
node.body.body.splice(
            0,
            0,
            jsc.methodDefinition(
                'get',
                jsc.identifier('template'),
                jsc.functionExpression(
                    null, [], jsc.blockStatement([jsc.returnStatement(
                                  templateLiteral)])),
                true));
      } else if (node.type === 'CallExpression') {
        // A Polymer hybrid/legacy factory function element
        const arg = node.arguments[0];
        if (arg && arg.type === 'ObjectExpression') {
          arg.properties.unshift(jsc.property(
              'init', jsc.identifier('_template'), templateLiteral));
        }
      } else {
        console.error(`Internal Error, Class or CallExpression expected, got ${
            node.type}`);
      }
    }
    return claimedDomModules;
  }
github Polymer / tools / src / document-processor.ts View on Github external
if (!canDomModuleBeInlined(domModule)) {
        continue;
      }
      claimedDomModules.add(domModule);
      const template = dom5.query(domModule, (e) => e.tagName === 'template');
      if (template === null) {
        continue;
      }

      // It's ok to tag templates with the expression `Polymer.html` without
      // adding an import because `Polymer.html` is re-exported by both
      // polymer.html and polymer-element.html and, crucially, template
      // inlining happens before rewriting references.
      const templateLiteral = jsc.taggedTemplateExpression(
          jsc.memberExpression(
              jsc.identifier('Polymer'), jsc.identifier('html')),
          serializeNodeToTemplateLiteral(
              parse5.treeAdapters.default.getTemplateContent(template)));
      const nodePath = getNodePathInProgram(program, element.astNode);

      if (nodePath === undefined) {
        console.warn(
            new Warning({
              code: 'not-found',
              message: `Can't find recast node for element ${element.tagName}`,
              parsedDocument: this.document.parsedDocument,
              severity: Severity.WARNING,
              sourceRange: element.sourceRange!
            }).toString());
        continue;
      }
github Polymer / tools / src / passes / rewrite-namespace-exports.ts View on Github external
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 / packages / modulizer / src / document-util.ts View on Github external
jsc.expressionStatement(jsc.assignmentExpression(
          '=',
          jsc.memberExpression(
              jsc.identifier(varName), jsc.identifier('innerHTML')),
          templateValue));
  const targetNode = activeInBody ? 'body' : 'head';
  return [
    createElementTemplate,
    setDocumentContainerStatement,
    jsc.expressionStatement(jsc.callExpression(
        jsc.memberExpression(
            jsc.memberExpression(
                jsc.identifier('document'), jsc.identifier(targetNode)),
            jsc.identifier('appendChild')),
        [jsc.memberExpression(
            jsc.identifier(varName), jsc.identifier('content'))]))
  ];
}
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(
          jsc.memberExpression(
github Polymer / tools / src / document-util.ts View on Github external
}
  const setDisplayNoneStatement = jsc.expressionStatement(jsc.callExpression(
      jsc.memberExpression(
          jsc.identifier(varName), jsc.identifier('setAttribute')),
      [jsc.literal('style'), jsc.literal('display: none;')]));
  return [
    createElementDiv,
    setDisplayNoneStatement,
    setDocumentContainerStatement,
    jsc.expressionStatement(jsc.callExpression(
        jsc.memberExpression(
            jsc.memberExpression(
                jsc.identifier('document'), jsc.identifier('head')),
            jsc.identifier('appendChild')),
        [jsc.memberExpression(
            jsc.identifier(varName), jsc.identifier('content'))]))
  ];
}
github Polymer / tools / packages / modulizer / src / document-util.ts View on Github external
export function attachCommentsToEndOfProgram(
    comments: string[],
    statements: Array) {
  if (comments.length === 0) {
    return;
  }
  const message =
      `\n  FIXME(polymer-modulizer): the above comments were extracted\n` +
      `  from HTML and may be out of place here. Review them and\n` +
      `  then delete this comment!\n`;
  comments.push(message);

  const recastComments = getCommentsFromTexts(comments);
  const lastStatement =
      jsc.expressionStatement(jsc.identifier('')) as RecastNode &
      estree.Statement;
  lastStatement.comments =
      (lastStatement.comments || []).concat(recastComments);
  statements.push(lastStatement);
}
github Polymer / tools / packages / modulizer / src / document-util.ts View on Github external
[jsc.literal('template')]))]);
  const setDocumentContainerStatement =
      jsc.expressionStatement(jsc.assignmentExpression(
          '=',
          jsc.memberExpression(
              jsc.identifier(varName), jsc.identifier('innerHTML')),
          templateValue));
  const targetNode = activeInBody ? 'body' : 'head';
  return [
    createElementTemplate,
    setDocumentContainerStatement,
    jsc.expressionStatement(jsc.callExpression(
        jsc.memberExpression(
            jsc.memberExpression(
                jsc.identifier('document'), jsc.identifier(targetNode)),
            jsc.identifier('appendChild')),
        [jsc.memberExpression(
            jsc.identifier(varName), jsc.identifier('content'))]))
  ];
}