TypeScript Add flip operator refactoring

0kjbasz6  于 9个月前  发布在  TypeScript
关注(0)|答案(1)|浏览(142)

建议

🔍 搜索词

翻转操作符,翻转操作符,重构

✅ 可实现性检查清单

我的建议满足以下准则:

  • 这不会对现有的TypeScript/JavaScript代码造成破坏性的更改
  • 这不会改变现有JavaScript代码的运行时行为
  • 这可以在不根据表达式的类型发出不同的JS的情况下实现
  • 这不是一个运行时特性(例如库功能,带有JavaScript输出的非ECMAScript语法,JS的新语法糖等)
  • 这个特性将与TypeScript's Design Goals的其他部分保持一致。

⭐ 建议

为交换左右操作数和更新操作符(如果需要)实现翻转操作符重构。
这个小功能将使一些需要复制粘贴操作数和操作符的重构变得更加容易。

📃 动机示例

之前:

  1. // harder to understand
  2. if (5 < position) {
  3. // ...
  4. }

之后:

  1. // much easier
  2. if (position > 5) {
  3. // ...
  4. }
yr9zkbsy

yr9zkbsy1#

@RyanCavanaugh Just to clarify, the motivation here is to help developer flip pieces of code without having to copy/paste and rewriting the code. For example we used to write comparisings in the following way: variable > literal value , because it is easier to understand. Code example:

  1. // harder to understnad
  2. if (5 < position) {
  3. // ...
  4. }
  5. // much easier
  6. if (position > 5) {
  7. // ...
  8. }

The refactoring activation range is operator only ( > ), so it shouldn't be annoying for existing users. So this refactoring can be small, but really nice addition to TypeScript IDE possibilities.

相关问题