Repository: sql-formatter-org/sql-formatter (ts)

Issue:
First comment in file gets indented by single space
**Describe the Feature**
It would be  nice to have an option which configures the amount of whitespace inserted before comments.

**Why do you want this feature?**
Certain tools rely on comments for metadata. I'm using `dbmate`, which uses [comments](https://github.com/amacneil/dbmate#creating-migrations) to annotate where `up` and `down` migrations start and end. It currently breaks while parsing SQL formatted by this tool.

Pull request description:
Fix indentation of first comment in file
Fixes #481 

Patch:
diff --git a/src/formatter/ExpressionFormatter.ts b/src/formatter/ExpressionFormatter.ts
index 610d38b6..67188464 100644
--- a/src/formatter/ExpressionFormatter.ts
+++ b/src/formatter/ExpressionFormatter.ts
@@ -306,8 +306,11 @@ export default class ExpressionFormatter {
   private formatLineComment(node: LineCommentNode) {
     if (isMultiline(node.precedingWhitespace || '')) {
       this.layout.add(WS.NEWLINE, WS.INDENT, node.text, WS.MANDATORY_NEWLINE, WS.INDENT);
-    } else {
+    } else if (this.layout.getLayoutItems().length > 0) {
       this.layout.add(WS.NO_NEWLINE, WS.SPACE, node.text, WS.MANDATORY_NEWLINE, WS.INDENT);
+    } else {
+      // comment is the first item in code - no need to add preceding spaces
+      this.layout.add(node.text, WS.MANDATORY_NEWLINE, WS.INDENT);
     }
   }
 


Test patch:
diff --git a/test/features/comments.ts b/test/features/comments.ts
index 9f269f9f..a5064290 100644
--- a/test/features/comments.ts
+++ b/test/features/comments.ts
@@ -116,6 +116,20 @@ export default function supportsComments(format: FormatFn, opts: CommentsConfig
     `);
   });
 
+  // Regression test for #481
+  it('formats first line comment in a file', () => {
+    expect(format('-- comment1\n-- comment2\n')).toBe(dedent`
+      -- comment1
+      -- comment2
+    `);
+  });
+  it('formats first block comment in a file', () => {
+    expect(format('/*comment1*/\n/*comment2*/\n')).toBe(dedent`
+      /*comment1*/
+      /*comment2*/
+    `);
+  });
+
   it('preserves single-line comments at the end of lines', () => {
     expect(
       format(`