ImportDeclaration()

in packages/eslint-plugin-baseui/src/deprecated-component-api.js [95:174]


      ImportDeclaration(node) {
        if (!node.source.value.startsWith('baseui/')) {
          return;
        }

        function isImporting(node, importName, importPath) {
          if (node.imported.name === importName && node.parent.source.value === importPath) {
            importState[importName] = node.local.name;
            return true;
          } else {
            return false;
          }
        }

        for (let x = 0; x < node.specifiers.length; x++) {
          const specifier = node.specifiers[x];
          if (
            specifier.type !== 'ImportNamespaceSpecifier' &&
            specifier.type !== 'ImportDefaultSpecifier'
          ) {
            // These can be referenced later on by instances of components.
            if (isImporting(specifier, 'Accordion', 'baseui/accordion')) return;
            if (isImporting(specifier, 'Modal', 'baseui/modal')) return;
            if (isImporting(specifier, 'Checkbox', 'baseui/checkbox')) return;
            if (isImporting(specifier, 'Button', 'baseui/button')) return;

            // removes return statement since these can be imported together
            isImporting(specifier, 'Radio', 'baseui/radio');
            isImporting(specifier, 'RadioGroup', 'baseui/radio');
          }
        }

        if (node.source.value !== 'baseui/typography') {
          return;
        }

        const existingImports = {};

        // Map existing imports (newName: localName), preference given to first renamed import.
        node.specifiers.forEach((specifier) => {
          if (specifier.type === 'ImportNamespaceSpecifier') {
            return;
          }
          const currentImportedName = specifier.imported.name;
          if (existingImports[currentImportedName]) {
            if (
              currentImportedName !== specifier.local.name &&
              existingImports[currentImportedName] === currentImportedName
            ) {
              existingImports[currentImportedName] = specifier.local.name;
            }
          } else {
            existingImports[currentImportedName] = specifier.local.name;
          }
        });

        const specifiers = node.specifiers || [];
        specifiers.forEach((specifier, specifierIndex) => {
          if (specifier.type === 'ImportNamespaceSpecifier') {
            return;
          }
          const deprecatedComponent = specifier.imported.name;
          const newComponent = mapDeprecatedTypographyComponents[deprecatedComponent];

          if (newComponent) {
            const isAlreadyImported = Boolean(existingImports[newComponent]);
            const isRenamed = specifier.local.name !== specifier.imported.name;

            if (isAlreadyImported) {
              removeImport(node, specifierIndex, deprecatedComponent, newComponent);
              identifiersToRename[specifier.local.name] = existingImports[newComponent];
            } else {
              fixImport(specifier, deprecatedComponent, newComponent);
              if (!isRenamed) {
                identifiersToRename[specifier.local.name] = newComponent;
              }
            }
          }
        });
      },