Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace literal-statements by short string in case __doc__ is used when remove_literal_statements is wanted #39

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/python_minifier/transforms/remove_literal_statements.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ class RemoveLiteralStatements(SuiteTransformer):
This includes docstrings
"""

def __init__(self):
super().__init__()
self.doc_in_module = False

def __call__(self, node):
if _doc_in_module(node):
return node
self.doc_in_module = _doc_in_module(node)
return self.visit(node)

def visit_Module(self, node):
Expand All @@ -50,7 +53,15 @@ def is_literal_statement(self, node):
return is_ast_node(node.value, (ast.Num, ast.Str, 'NameConstant', 'Bytes'))

def suite(self, node_list, parent):
without_literals = [self.visit(n) for n in node_list if not self.is_literal_statement(n)]
without_literals = []

for node in node_list:
if self.is_literal_statement(node):
if self.doc_in_module and is_ast_node(node.value, ast.Str):
node.value.value = 'doc-string stripped by python-minifier'
without_literals.append(node)
else:
without_literals.append(self.visit(node))

if len(without_literals) == 0:
if isinstance(parent, ast.Module):
Expand Down