Home > Mobile >  Refactoring: How to replace a type (and update imports)?
Refactoring: How to replace a type (and update imports)?

Time:05-26

i.e. currently we use a type OldType from an npm package @old-package in our code base.

We want to replace it with our own type NewType, which can be imported via @new-package.

Example

Old imports

import {
  AnyImport,
  OldType,
  AnotherImport
} from '@old-package';

should be updated to:

New imports

import {
  AnyImport,
  AnotherImport
} from '@old-package';
import { NewType } from '@new-package';

How can we do this refactoring in IntelliJ, so that the import statements are correctly updated?

Notes:

  • I think a simple replace RegEx will not work, because the old import may be one of many on multiple lines
  • I checked the IntelliJ Migrate feature, but it seems this only works for Java code
  • I checked the Structural search and replace feature, but cannot make it work for my case

Since this is a on-off operation it's also okay, if this can be done in another IDE or via a command-line tool, etc.

CodePudding user response:

Structural searching for imports is not supported/implemented at this time (related feature request: IDEA-285199). You can try some solutions available on the web, see https://www.npmjs.com/package/refactor-imports?activeTab=readme, for example

  • Related