Repository: nginxinc/crossplane

Issue:
feat: keep certain newlines and comments
Keeping comments is great as it allows users to understand complex changes quickly, while newlines in key positions help with readability.

Discussion:

Patch:
diff --git a/crossplane/formatter.py b/crossplane/formatter.py
index fef2749..80c3b21 100644
--- a/crossplane/formatter.py
+++ b/crossplane/formatter.py
@@ -5,7 +5,13 @@ from .parser import parse
 
 
 def format(filename, indent=4, tabs=False):
-    payload = parse(filename, single=True, check_ctx=False, check_args=False)
+    payload = parse(
+        filename,
+        comments=True,
+        single=True,
+        check_ctx=False,
+        check_args=False
+    )
 
     if payload['status'] != 'ok':
         e = payload['errors'][0]


Test patch:
diff --git a/tests/test_format.py b/tests/test_format.py
index 3c32bda..d116572 100644
--- a/tests/test_format.py
+++ b/tests/test_format.py
@@ -11,10 +11,12 @@ def test_format_messy_config():
     output = crossplane.format(config)
     assert output == '\n'.join([
         'user nobody;',
+        r'# hello\n\\n\\\n worlddd  \#\\#\\\# dfsf\n \\n \\\n ',
         'events {',
         '    worker_connections 2048;',
         '}',
-        'http {',
+        'http { #forteen',
+        '    # this is a comment',
         '    access_log off;',
         '    default_type text/plain;',
         '    error_log off;',
@@ -33,7 +35,7 @@ def test_format_messy_config():
         '        location /bar {',
         '        }',
         '        location /\{\;\}\ #\ ab {',
-        '        }',
+        '        } # hello',
         '        if ($request_method = P\{O\)\###\;ST) {',
         '        }',
         '        location /status.html {',
@@ -75,3 +77,25 @@ def test_format_args_not_analyzed():
         'http {',
         '}'
     ])
+
+
+def test_format_with_comments():
+    dirname = os.path.join(here, 'configs', 'with-comments')
+    config = os.path.join(dirname, 'nginx.conf')
+    output = crossplane.format(config)
+    assert output == '\n'.join([
+        'events {',
+        '    worker_connections 1024;',
+        '}',
+        '#comment',
+        'http {',
+        '    server {',
+        '        listen 127.0.0.1:8080; #listen',
+        '        server_name default_server;',
+        '        location / { ## this is brace',
+        '            # location /',
+        "            return 200 'foo bar baz';",
+        '        }',
+        '    }',
+        '}'
+    ])