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