mvdan / sh

A shell parser, formatter, and interpreter with bash support; includes shfmt
https://pkg.go.dev/mvdan.cc/sh/v3
BSD 3-Clause "New" or "Revised" License
6.97k stars 332 forks source link

syntax: insert single space before and after line comment `#` #1031

Open j-lakeman opened 9 months ago

j-lakeman commented 9 months ago

I'd expect this

#!/usr/bin/env bash

true#test

to be formatted to that

#!/usr/bin/env bash

true # test

to increase readability and to be consistent to other programming languages.

Rairden commented 9 months ago

Yes me too. I look forward to when shfmt adds a space for commented lines.

#!/bin/bash

foo() {
#   echo 1
    #echo 2
    # echo 3
    echo 4
}

foo

true #test

I expect shfmt to turn the echo 1 and echo 2 lines to look like echo 3. No matter what flags I run with shfmt, the output is something like this:

shfmt -d format-me.sh
@@ -1,7 +1,7 @@
 #!/bin/bash

 foo() {
-#   echo 1
+   #   echo 1
    #echo 2
    # echo 3
    echo 4
mvdan commented 8 months ago

shfmt heavily borrows from Go's gofmt, which has never forced a leading space in comments, and I personally think that's fine. Most people will prefer comments leading with one space, but nothing is particularly wrong about comments that don't.

For example, what about comments starting with a tab character, what should we do with them? Or what about empty comments?

j-lakeman commented 7 months ago

Please see this:

https://chat.openai.com/share/2fa42d22-4344-485d-acab-4c9a7847ce95

I know the tool is questionable. In this case however I used it for a quick summary.

mvdan commented 1 month ago

I won't make technical decisions based on what an AI thinks. My questions above remain.

theclapp commented 1 month ago

That openai link is now 404.