重构:如何替换类型(并更新导入)?
即当前,我们在我们的代码库中使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
目前不支持/实施结构性搜索导入(相关功能请求:)。
您可以尝试在网上可用的一些解决方案,请参阅 htttps .com/package/rebactor-imports?activetab = readme
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