更改多次使用的函数签名的最简单方法?



我正在寻找我可以用来完成这个任务的任何工具的建议。

我想改变这个类的构造函数如下

class Arc{
center: Point;
radius: number;
sweep: Sweep;
// from this
constructor(center: Point, radius: number, startAngle: number, sweepAngle: number) {
this.center = center;
this.radius = radius;
this.sweep = new Sweep(startAngle, sweepAngle);
}
// to this
constructor(center: Point, radius: number, sweep: Sweep) {
this.center = center;
this.radius = radius;
this.sweep = sweep;
}
}

比起让Arc知道Sweep的构造函数,我更愿意在这里使用依赖注入。它使测试更容易,而且我认为它更有意义,因为我也使用工厂模式,我有几个调用构造函数的工厂。

这个更改的问题是,在我的代码库中有超过200个对构造函数的引用。它们几乎都在我写得很差的测试中。

无论如何,我可以改变所有这些调用构造函数,同时保持属性?我使用VSCode作为我的IDE,我对一些unix命令很满意。

像这样:

new Arc(new Point(0, 0), 1, 0, Math.PI);
// I want to carry through 0 and Math.PI to the Sweep constructor.
new Arc(new Point(0, 0), 1, new Sweep(0, Math.PI));

WebStorm有一些Typescript重构。我没有任何具体的经验,但我会尝试这个:

  1. 执行全局搜索并替换以删除空格,使new Arc(new Point(0, 0), 1, 0, Math.PI);变为newArc(new Point(0, 0), 1, 0, Math.PI);
  2. 引入一个全局函数newArc来调用首选构造函数。
  3. 使用"内联方法"重构以移除newArc,这将(希望)用新代码替换所有的调用者。

确保你有一些备份(或源代码控制),看看你是如何进行的。