From a2bfb27fda6a779b00f1d5d57ef4c278f749de0d Mon Sep 17 00:00:00 2001 From: Jan Max Meyer Date: Wed, 17 Nov 2021 20:37:36 +0100 Subject: [PATCH] Replace literal-statements when __doc__ is used This improves the remove_literal_statements-feature and replaces doc-strings in case the module uses __doc__ by the string 'doc-string stripped by python-minifier', which is much shorter in most cases. --- .../transforms/remove_literal_statements.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/python_minifier/transforms/remove_literal_statements.py b/src/python_minifier/transforms/remove_literal_statements.py index 62910e86..265e800e 100644 --- a/src/python_minifier/transforms/remove_literal_statements.py +++ b/src/python_minifier/transforms/remove_literal_statements.py @@ -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): @@ -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):