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

shfmt should repair tab indented here docs. #758

Closed
docwhat opened this issue Nov 16, 2021 · 4 comments
Closed

shfmt should repair tab indented here docs. #758

docwhat opened this issue Nov 16, 2021 · 4 comments

Comments

@docwhat
Copy link

docwhat commented Nov 16, 2021

From the bash man page:

If the redirection operator is <<-, then all leading tab characters are stripped from input lines and the line containing delimiter. This allows here-documents within shell scripts to be indented in a natural fashion.

Given this:

# spaces not tab.
fun() {
    cat <<-DOC
        some important text
    DOC
}

... I would like shfmt to repair this... because a lot of editors have a hard time with this if you're using spaces for indentation overall.

@mvdan
Copy link
Owner

mvdan commented Nov 16, 2021

Can you clarify what you mean by "repair"? Perhaps give me an input file and how you run shfmt on it, and what you expect the output to be.

@docwhat
Copy link
Author

docwhat commented Nov 17, 2021

Given

# spaces not tab.
fun() {
    cat <<-DOC
        some important text
    DOC
}

for i in {1..10}; do
    echo "run $i"
    fun
done

When you run:

shfmt -i 4 -s script-with-spaces.bash   

I would expect:

# spaces not tab.
fun() {
    cat <<-DOC
	  some important text
	DOC
}

for i in {1..10}; do
    echo "run $i"
    fun
done

But I get:

/tmp/foo.bash:3:9: unclosed here-document 'DOC'

Basically, shfmt would detect that DOC isn't closed and try to repair it by replacing the first n spaces with a tab. n would be calculated by looking at the stop line.

@mvdan
Copy link
Owner

mvdan commented Dec 2, 2021

Basically, shfmt would detect that DOC isn't closed and try to repair it by replacing the first n spaces with a tab. n would be calculated by looking at the stop line.

Hmm. shfmt is just a formatter, though - if its input is invalid Bash, it generally shouldn't try to fix it automatically. In other words, the parser should error if the input is invalid. We don't have a middle ground where we may return warnings or fix the program for the user. See #765 as another example, for instance.

I think your suggestion falls under the kind of tool to spot bugs or auto-fix common shell mistakes. You could certainly build a tool like that on top of our Go syntax package. For instance, if that tool runs the parser and encounters /tmp/foo.bash:3:9: unclosed here-document 'DOC', it could look at the following lines to see if one could be the closing heredoc line by replacing leading spaces with tabs.

@mvdan
Copy link
Owner

mvdan commented Dec 16, 2021

I'm going to close this as per the last comment. If someone wants to coordinate writing such a "fix common mistakes" tool, please feel free to open an umbrella issue or discussion on this repository, like #730. Thanks!

@mvdan mvdan closed this as completed Dec 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants