-
-
Notifications
You must be signed in to change notification settings - Fork 325
/
Copy pathfix_vdom_constructor_usage.py
361 lines (321 loc) · 11 KB
/
fix_vdom_constructor_usage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
from __future__ import annotations
import ast
import re
import sys
from collections.abc import Sequence
from keyword import kwlist
from pathlib import Path
from textwrap import dedent, indent
from tokenize import COMMENT as COMMENT_TOKEN
from tokenize import generate_tokens
from typing import Iterator
from idom import html
_CAMEL_CASE_SUB_PATTERN = re.compile(r"(?<!^)(?=[A-Z])")
def update_vdom_constructor_usages(source: str, filename: str = "") -> None:
tree = ast.parse(source)
changed: list[Sequence[ast.AST]] = []
for parents, node in walk_with_parent(tree):
if isinstance(node, ast.Call):
func = node.func
match func:
case ast.Attribute():
name = func.attr
case ast.Name(ctx=ast.Load()):
name = func.id
case _:
name = ""
if hasattr(html, name):
match node.args:
case [ast.Dict(keys, values), *_]:
new_kwargs = list(node.keywords)
for k, v in zip(keys, values):
if isinstance(k, ast.Constant) and isinstance(k.value, str):
new_kwargs.append(
ast.keyword(arg=conv_attr_name(k.value), value=v)
)
else:
new_kwargs = [ast.keyword(arg=None, value=node.args[0])]
break
node.args = node.args[1:]
node.keywords = new_kwargs
changed.append((node, *parents))
case [
ast.Call(
func=ast.Name(id="dict", ctx=ast.Load()),
args=args,
keywords=kwargs,
),
*_,
]:
new_kwargs = [
*[ast.keyword(arg=None, value=a) for a in args],
*node.keywords,
]
for kw in kwargs:
if kw.arg is not None:
new_kwargs.append(
ast.keyword(
arg=conv_attr_name(kw.arg), value=kw.value
)
)
else:
new_kwargs.append(kw)
node.args = node.args[1:]
node.keywords = new_kwargs
changed.append((node, *parents))
case _:
pass
if not changed:
return
ast.fix_missing_locations(tree)
lines = source.split("\n")
# find closest parent nodes that should be re-written
nodes_to_unparse: list[ast.AST] = []
for node_lineage in changed:
origin_node = node_lineage[0]
for i in range(len(node_lineage) - 1):
current_node, next_node = node_lineage[i : i + 2]
if (
not hasattr(next_node, "lineno")
or next_node.lineno < origin_node.lineno
or isinstance(next_node, (ast.ClassDef, ast.FunctionDef))
):
nodes_to_unparse.append(current_node)
break
else:
raise RuntimeError("Failed to change code")
# check if an nodes to rewrite contain eachother, pick outermost nodes
current_outermost_node, *sorted_nodes_to_unparse = list(
sorted(nodes_to_unparse, key=lambda n: n.lineno)
)
outermost_nodes_to_unparse = [current_outermost_node]
for node in sorted_nodes_to_unparse:
if node.lineno > current_outermost_node.end_lineno:
current_outermost_node = node
outermost_nodes_to_unparse.append(node)
moved_comment_lines_from_end: list[int] = []
# now actually rewrite these nodes (in reverse to avoid changes earlier in file)
for node in reversed(outermost_nodes_to_unparse):
# make a best effort to preserve any comments that we're going to overwrite
comments = find_comments(lines[node.lineno - 1 : node.end_lineno])
# there may be some content just before and after the content we're re-writing
before_replacement = lines[node.lineno - 1][: node.col_offset].strip()
if node.end_lineno is not None and node.end_col_offset is not None:
after_replacement = lines[node.end_lineno - 1][
node.end_col_offset :
].strip()
else:
after_replacement = ""
replacement = indent(
before_replacement
+ "\n".join([*comments, ast.unparse(node)])
+ after_replacement,
" " * (node.col_offset - len(before_replacement)),
)
if node.end_lineno:
lines[node.lineno - 1 : node.end_lineno] = [replacement]
else:
lines[node.lineno - 1] = replacement
if comments:
moved_comment_lines_from_end.append(len(lines) - node.lineno)
for lineno_from_end in sorted(list(set(moved_comment_lines_from_end))):
print(f"Moved comments to {filename}:{len(lines) - lineno_from_end}")
return "\n".join(lines)
def find_comments(lines: list[str]) -> list[str]:
iter_lines = iter(lines)
return [
token
for token_type, token, _, _, _ in generate_tokens(lambda: next(iter_lines))
if token_type == COMMENT_TOKEN
]
def walk_with_parent(
node: ast.AST, parents: tuple[ast.AST, ...] = ()
) -> Iterator[tuple[tuple[ast.AST, ...], ast.AST]]:
parents = (node,) + parents
for child in ast.iter_child_nodes(node):
yield parents, child
yield from walk_with_parent(child, parents)
def conv_attr_name(name: str) -> str:
new_name = _CAMEL_CASE_SUB_PATTERN.sub("_", name).replace("-", "_").lower()
return f"{new_name}_" if new_name in kwlist else new_name
def run_tests():
cases = [
# simple conversions
(
'html.div({"className": "test"})',
"html.div(class_name='test')",
),
(
'html.div({class_name: "test", **other})',
"html.div(**{class_name: 'test', **other})",
),
(
'html.div(dict(other, className="test"))',
"html.div(**other, class_name='test')",
),
(
'html.div({"className": "outer"}, html.div({"className": "inner"}))',
"html.div(html.div(class_name='inner'), class_name='outer')",
),
(
'html.div({"className": "outer"}, html.div({"className": "inner"}))',
"html.div(html.div(class_name='inner'), class_name='outer')",
),
(
'["before", html.div({"className": "test"}), "after"]',
"['before', html.div(class_name='test'), 'after']",
),
(
"""
html.div(
{"className": "outer"},
html.div({"className": "inner"}),
html.div({"className": "inner"}),
)
""",
"html.div(html.div(class_name='inner'), html.div(class_name='inner'), class_name='outer')",
),
(
'html.div(dict(className="test"))',
"html.div(class_name='test')",
),
# when to not attempt conversion
(
'html.div(ignore, {"className": "test"})',
None,
),
# avoid unnecessary changes
(
"""
def my_function():
x = 1 # some comment
return html.div({"className": "test"})
""",
"""
def my_function():
x = 1 # some comment
return html.div(class_name='test')
""",
),
(
"""
if condition:
# some comment
dom = html.div({"className": "test"})
""",
"""
if condition:
# some comment
dom = html.div(class_name='test')
""",
),
(
"""
[
html.div({"className": "test"}),
html.div({"className": "test"}),
]
""",
"""
[
html.div(class_name='test'),
html.div(class_name='test'),
]
""",
),
(
"""
@deco(
html.div({"className": "test"}),
html.div({"className": "test"}),
)
def func():
# comment
x = [
1
]
""",
"""
@deco(
html.div(class_name='test'),
html.div(class_name='test'),
)
def func():
# comment
x = [
1
]
""",
),
(
"""
@deco(html.div({"className": "test"}), html.div({"className": "test"}))
def func():
# comment
x = [
1
]
""",
"""
@deco(html.div(class_name='test'), html.div(class_name='test'))
def func():
# comment
x = [
1
]
""",
),
# best effort to preserve comments
(
"""
x = 1
html.div(
# comment 1
{"className": "outer"},
# comment 2
html.div({"className": "inner"}),
)
""",
"""
x = 1
# comment 1
# comment 2
html.div(html.div(class_name='inner'), class_name='outer')
""",
),
]
success = True
for source, expected in cases:
actual = update_vdom_constructor_usages(dedent(source).strip(), "test.py")
if isinstance(expected, str):
expected = dedent(expected).strip()
if actual != expected:
if not success:
print("\n" + "-" * 20)
print(
dedent(
f"""
{actual}
▲ actual ▲
▼ expected ▼
{expected}
"""
)
)
success = False
return success
if __name__ == "__main__":
argv = sys.argv[1:]
if not argv:
print("Running tests...")
result = run_tests()
print("Success" if result else "Failed")
sys.exit(0 if result else 0)
for pattern in argv:
for file in Path.cwd().glob(pattern):
result = update_vdom_constructor_usages(
source=file.read_text(),
filename=str(file),
)
if result is not None:
file.write_text(result)