Skip to content

Commit 759df8a

Browse files
committed
Optimize output on "compilation"
Add a function to remove whitespaces around operators and in calls Reenable array reformatter and dequoter
1 parent 943113d commit 759df8a

1 file changed

Lines changed: 75 additions & 7 deletions

File tree

compiler.py

Lines changed: 75 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -313,12 +313,78 @@ def dequoter(text: str) -> str:
313313

314314
def reformat_array(text: str) -> str:
315315
"""
316-
Remove spaces between array elements, or between hash keys and values
317-
DO NOT remove spaces in {".+"} outside of array or hash
316+
Remove spaces between array/map elements in specific patterns
317+
Targets explicit key:value pairs in maps and array element lists
318318
"""
319-
#
320-
text = sub(r"(?<=\S),\s(?=\S|[^\"\}])", ",", text)
321-
text = sub(r"(?<=\S):\s(?=\S)", ":", text)
319+
# Remove spaces in map key:value patterns like [key: "value", key2: "value2"]
320+
# Only for quoted values to be safe
321+
text = sub(r'([a-zA-Z_]\w*):\s+(["\'])', r"\1:\2", text)
322+
323+
# Remove spaces in map key:value with numeric/variable values
324+
# [key: 123, key2: var] -> [key:123,key2:var]
325+
text = sub(r"([a-zA-Z_]\w*):\s+([a-zA-Z_0-9])", r"\1:\2", text)
326+
327+
# Remove spaces in numeric key:value pairs like {8: 'value', 7: 'value'}
328+
text = sub(r'(\d+):\s+(["\'])', r"\1:\2", text)
329+
330+
# Remove spaces after commas between map entries
331+
# Only when comma is followed by an identifier (map key) or number
332+
text = sub(r",\s+([a-zA-Z_0-9]\w*:)", r",\1", text)
333+
334+
return text
335+
336+
337+
def remove_spaces_around_operators(text: str) -> str:
338+
"""
339+
Remove spaces around operators to shrink code size
340+
341+
Args:
342+
text (str): Text to optimize
343+
344+
Returns:
345+
str: Optimized text
346+
"""
347+
# Remove spaces around comparison and assignment operators
348+
operators = [
349+
(r"\s*==\s*", "=="),
350+
(r"\s*!=\s*", "!="),
351+
(r"\s*<=\s*", "<="),
352+
(r"\s*>=\s*", ">="),
353+
(r"\s*<\s*", "<"),
354+
(r"\s*>\s*", ">"),
355+
(r"\s*\?\s*", "?"),
356+
(r"\s*\|\|\s*", "||"),
357+
(r"\s*&&\s*", "&&"),
358+
(r"\s*\+=\s*", "+="),
359+
(r"\s*-=\s*", "-="),
360+
(r"\s*\*=\s*", "*="),
361+
(r"\s*/=\s*", "/="),
362+
]
363+
364+
for pattern, replacement in operators:
365+
text = sub(pattern, replacement, text)
366+
367+
return text
368+
369+
370+
def remove_spaces_in_calls(text: str) -> str:
371+
"""
372+
Remove unnecessary spaces in function calls and parentheses
373+
374+
Args:
375+
text (str): Text to optimize
376+
377+
Returns:
378+
str: Optimized text
379+
"""
380+
# Remove spaces after opening parentheses and before closing
381+
text = sub(r"\(\s+", "(", text)
382+
text = sub(r"\s+\)", ")", text)
383+
384+
# Remove spaces in square brackets
385+
text = sub(r"\[\s+", "[", text)
386+
text = sub(r"\s+\]", "]", text)
387+
322388
return text
323389

324390

@@ -335,8 +401,10 @@ def main():
335401
script = array_stringify(remove_leading_whitespace(script))
336402
script = clean_characters(script)
337403
script = obfuscate_variables(script, use_json=not args.forget)
338-
# script = reformat_array(script)
339-
# script = dequoter(script)
404+
script = remove_spaces_around_operators(script)
405+
script = remove_spaces_in_calls(script)
406+
script = reformat_array(script)
407+
script = dequoter(script)
340408

341409
out.parent.mkdir(parents=True, exist_ok=True)
342410
out.write_text(script, encoding="utf8")

0 commit comments

Comments
 (0)