重构:如何替换类型(并更新导入)?

发布于 2025-01-31 15:39:19 字数 1032 浏览 3 评论 0原文

即当前,我们在我们的代码库中使用NPM软件包@old-package type oldType

我们想用自己的类型newType替换它,可以通过@new-package导入。

示例

旧导入

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

应更新为:

新导入

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

我们如何在Intellij中进行重构,以便正确更新导入语句?

注意:

  • 我认为简单的替换式以色符是行不通的,因为旧的导入可能是
  • 我检查 Intellij Migrate 功能,但似乎这仅适用于Java代码,
  • 我检查了结构搜索并替换功能,但不能使它适合我的情况,

因为这是一个开关操作,也可以,如果可以在另一个中完成此操作IDE或通过命令行工具等。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假情假意假温柔 2025-02-07 15:39:19

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文