Whole file
t3rn0/ast-comments
The author described this change as “fix: put comments to every statement in line”. It counts as a record because the check below fails on the code as it stood at bb4d9582d and passes on fcbd83c83, with nothing else changed between the two runs.
Projectt3rn0/ast-comments
Fix saved2022-11-19
Sharing licenceMIT · LICENSE
Change size+19 −6
What the code was meant to do, written into the code itself as a save note
fix: put comments to every statement in line
The change
| 36 | 36 | comment_tokens = sorted( | |
| 37 | 37 | (x.start[0], x) for x in tokens if x.type == tokenize.COMMENT | |
| 38 | 38 | ) | |
| 39 | - | nodes = sorted( | |
| 40 | - | (x.lineno, x.col_offset, x) for x in ast.walk(tree) if isinstance(x, ast.stmt) | |
| 41 | - | ) | |
| 39 | + | ||
| 40 | + | if not comment_tokens: | |
| 41 | + | return | |
| 42 | + | ||
| 43 | + | nodes = {} | |
| 44 | + | for node in ast.walk(tree): | |
| 45 | + | if not isinstance(node, ast.stmt): | |
| 46 | + | continue | |
| 47 | + | if node.lineno not in nodes: | |
| 48 | + | nodes[node.lineno] = [node] | |
| 49 | + | else: | |
| 50 | + | nodes[node.lineno].append(node) | |
| 51 | + | ||
| 52 | + | node_lines = sorted(nodes) | |
| 53 | + | ||
| 42 | 54 | i = j = 0 | |
| 43 | - | while i < len(comment_tokens) and j < len(nodes): | |
| 55 | + | while i < len(comment_tokens) and j < len(node_lines): | |
| 44 | 56 | t_lineno, token = comment_tokens[i] | |
| 45 | - | n_lineno, _, node = nodes[j] | |
| 57 | + | n_lineno = node_lines[j] | |
| 46 | 58 | if t_lineno <= n_lineno: | |
| 47 | - | node.comments += (format_comment(token.string),) | |
| 59 | + | for node in nodes[n_lineno]: | |
| 60 | + | node.comments += (format_comment(token.string),) | |
| 48 | 61 | i += 1 | |
| 49 | 62 | else: | |
| 50 | 63 | j += 1 |
The check that tells the two apart
fail→pass·test_ast_commets.py::test_comment_to_multiple_statements
Check file test_ast_commets.py, taken without changes from the fix and copied onto the older code, so the exact same check runs against both versions.
Origin and history
The code before itbb4d9582d39bf746ac1be18bb995c8a162677eb4
Broken version dated2022-11-13
Moduleast_comments
Units changed_enrich
Fingerprint4a23e3e664ccdc54
Checked2026-08-18 by goldset/0.1
Every field above is generated by our program. None of it is written by hand.