fix_camel_break.py 814 B

123456789101112131415161718192021222324
  1. # -*- coding: utf-8 -*-
  2. """修复 migrate_camel_java.py 第二轮造成的损坏:
  3. put("x", y) 被写成了 put("x"), y。
  4. 特征:.put( / .putIfAbsent( / .getOrDefault( 后的 "key") 后紧跟 `, `,
  5. 而这些方法必然带第二参数,正常代码不存在 `"key"), ` 形态,可安全还原。
  6. """
  7. import re
  8. from pathlib import Path
  9. from _root import ROOT
  10. SRC = ROOT / "yoshop-java" / "src" / "main" / "java"
  11. BAD = re.compile(r'\.(put|putIfAbsent|getOrDefault)\("([A-Za-z0-9]+)"\), ')
  12. fixed = []
  13. for f in sorted(SRC.rglob("*.java")):
  14. text = f.read_text(encoding="utf-8")
  15. new = BAD.sub(r'.\1("\2", ', text)
  16. if new != text:
  17. f.write_text(new, encoding="utf-8")
  18. fixed.append(str(f.relative_to(SRC)))
  19. print("修复 %d 个文件" % len(fixed))
  20. for n in fixed:
  21. print(" ", n)