fix_ws_comma.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. """Fixer that changes 'a ,b' into 'a, b'.
  2. This also changes '{a :b}' into '{a: b}', but does not touch other
  3. uses of colons. It does not touch other uses of whitespace.
  4. """
  5. from .. import pytree
  6. from ..pgen2 import token
  7. from .. import fixer_base
  8. class FixWsComma(fixer_base.BaseFix):
  9. explicit = True # The user must ask for this fixers
  10. PATTERN = """
  11. any<(not(',') any)+ ',' ((not(',') any)+ ',')* [not(',') any]>
  12. """
  13. COMMA = pytree.Leaf(token.COMMA, ",")
  14. COLON = pytree.Leaf(token.COLON, ":")
  15. SEPS = (COMMA, COLON)
  16. def transform(self, node, results):
  17. new = node.clone()
  18. comma = False
  19. for child in new.children:
  20. if child in self.SEPS:
  21. prefix = child.prefix
  22. if prefix.isspace() and "\n" not in prefix:
  23. child.prefix = ""
  24. comma = True
  25. else:
  26. if comma:
  27. prefix = child.prefix
  28. if not prefix:
  29. child.prefix = " "
  30. comma = False
  31. return new