How to use the jscodeshift.importDeclaration 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 5to6 / 5to6-codemod / utils / main.js View on Github external
createImportStatement: function(moduleName, variableName, propName, comments) {
    var declaration, variable, idIdentifier, nameIdentifier;
    // console.log('variableName', variableName);
    // console.log('moduleName', moduleName);

    // if no variable name, return `import 'jquery'`
    if (!variableName) {
      declaration = j.importDeclaration([], j.literal(moduleName) );
      declaration.comments = comments
      return declaration;
    }

    // multiple variable names indicates a destructured import
    if (Array.isArray(variableName)) {
      var variableIds = variableName.map(function(v, i) {
        var prop = Array.isArray(propName) && propName[i] ? propName[i] : v
        return j.importSpecifier(j.identifier(v), j.identifier(prop));
      });

      declaration = j.importDeclaration(variableIds, j.literal(moduleName));
    } else {
      // else returns `import $ from 'jquery'`
      nameIdentifier = j.identifier(variableName); //import var name
      variable = j.importDefaultSpecifier(nameIdentifier);
github dvajs / dva-ast / src / api / router.js View on Github external
// create & import component
  let relativePath;
  const componentFilePath = join(payload.sourcePath, component.filePath);
  if (existsSync(componentFilePath)) {
    relativePath = relative(filePath, componentFilePath);
    if (relativePath.charAt(0) !== '.') {
      relativePath = './' + relativePath;
    }
    relativePath = relativePath.split(sep).join('/');  // workaround for windows
  }

  const imports = root.find(j.ImportDeclaration);
  const lastImport = imports.at(imports.size() - 1);
  lastImport.insertAfter(
    j.importDeclaration(
      [j.importDefaultSpecifier(
        j.identifier(component.componentName)
      )],
      j.literal(relativePath)
    )
  );
  writeFile(filePath, root.toSource());
}
github Polymer / tools / src / document-converter.ts View on Github external
const namespaceImport = namespaceImports[0];
  if (namespaceImport) {
    const alias = assignAlias(namespaceImport, getModuleId(specifierUrl));

    importDeclarations.push(jsc.importDeclaration(
        [jsc.importNamespaceSpecifier(jsc.identifier(alias))],
        jsc.literal(specifierUrl)));
  }

  // If any named imports were referenced, create a new import for all named
  // members. If `namedSpecifiers` is empty but a namespace wasn't imported
  // either, then still add an empty importDeclaration to trigger the load.
  if (namedSpecifiers.length > 0 || namespaceImport === undefined) {
    importDeclarations.push(
        jsc.importDeclaration(namedSpecifiers, jsc.literal(specifierUrl)));
  }

  // Replace all references to all imports with the assigned name for each
  // import.
  for (const {target, path} of importReferences) {
    const assignedName = assignedNames.get(target);
    if (!assignedName) {
      throw new Error(
          `The import '${target.name}' was not assigned an identifier.`);
    }

    path.replace(jsc.identifier(assignedName));
  }

  return importDeclarations;
}
github Polymer / tools / src / document-converter.ts View on Github external
const importDeclarations: estree.ImportDeclaration[] = [];

  // If a module namespace was referenced, create a new namespace import
  const namespaceImports =
      namedImportsArray.filter((import_) => import_.name === '*');
  if (namespaceImports.length > 1) {
    throw new Error(
        `More than one namespace import was given for '${specifierUrl}'.`);
  }

  const namespaceImport = namespaceImports[0];
  if (namespaceImport) {
    const alias = assignAlias(namespaceImport, getModuleId(specifierUrl));

    importDeclarations.push(jsc.importDeclaration(
        [jsc.importNamespaceSpecifier(jsc.identifier(alias))],
        jsc.literal(specifierUrl)));
  }

  // If any named imports were referenced, create a new import for all named
  // members. If `namedSpecifiers` is empty but a namespace wasn't imported
  // either, then still add an empty importDeclaration to trigger the load.
  if (namedSpecifiers.length > 0 || namespaceImport === undefined) {
    importDeclarations.push(
        jsc.importDeclaration(namedSpecifiers, jsc.literal(specifierUrl)));
  }

  // Replace all references to all imports with the assigned name for each
  // import.
  for (const {target, path} of importReferences) {
    const assignedName = assignedNames.get(target);
github Astrocoders / old-astroman / lib / utils / js / appendImport.js View on Github external
function getNewImportDeclaration({
  name,
  local,
}) {
  return j.importDeclaration([
    j.importDefaultSpecifier(j.identifier(name)),
  ], j.literal(local))
}
github Amwam / js-import-sort / transform.js View on Github external
const name = propName.name;
    let identifier;
    if (name) {
      identifier = jscodeshift.identifier(name);
      variableIds.unshift(
        jscodeshift.importDefaultSpecifier(identifier, identifier)
      );
    } else if (namespace) {
      identifier = jscodeshift.identifier(namespace);
      variableIds.unshift(
        jscodeshift.importNamespaceSpecifier(identifier, identifier)
      );
    }
  }

  declaration = jscodeshift.importDeclaration(
    variableIds,
    jscodeshift.literal(moduleName)
  );
  declaration.importKind = kind;

  return declaration;
}