-
Notifications
You must be signed in to change notification settings - Fork 21
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
use a shell parser to format commands #2
Comments
please do :) |
Instead of just ignoring &&'s that are inside quotes, I started implementing mvdan/sh/syntax last night for the full shell parser. I got it working but then had some concerns:
I'm leaning towards a shell parser being out of scope of what dockfmt is for, but I may be wrong and don't have a problem solving those ^ issues if you disagree and would like to have it. Just let me know if you'd like to have the shell parser and if I should include anything to address those ^ . On the other hand, if you just want to stick with ignoring quoted &&'s, let me know if you have an issue with the regex solution; I can handle criticism :) |
I personally think that using regular expressions is not a great solution for this - it's just a slightly smarter version of the old code :) Reminds me of https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags To name a few possible edge cases that aren't handled by the regex:
You also don't need to enforce the formatting that the
I'm not sure I understand - my point is that anything that is quoted should never be split in multiple lines. For example, this can result in the string having extra whitespace due to the indentation. And as you say, one can never know if the string is really a shell program. Ultimately this won't matter for most people as this issue is about edge cases, but I tend to prefer correctness when it is possible :) |
@mvdan I agree that regex is rarely the perfect answer and needs to be used cautiously. The first example you gave parses correctly for me with the regex though, but you're right about the last two since quotes aren't involved. Using the parser by itself might be a good idea then. I got carried away implementing full command parsing which is what I started to question being in scope. I'll try to take another look at your syntax parser sometime this week. |
I've just played with @mvdan's shell parser / |
@tianon Oh without a doubt. My hacked up PR a few months ago wasn't meant to discredit @mvdan's work. I agree that his syntax parser needs to be used here. I dropped the ball on "taking a look this week" since its well... been much longer than that. I started a new job the week after that and got way too busy. I won't specify a time frame this time but it's definitely something I hope to look into again soon. |
At the moment, the code simply treats shell code as a string, splitting by
&&
and doing string replaces.This will work most of the time, but will break fairly easily, with cases like:
bash -c 'foo && bar'
- we don't want this to be splitapk update && apk add x
- won't be replaced withapk add --no-cache
due to the double space (markdown gets rid of the double space before&&
, though)I use this tool and suffer from these from time to time, so I'd be happy to work on a small patch to use mvdan.cc/sh/syntax. Using its printer you'd also format the shell code, such as replacing
`foo`
with$(foo)
.I see no tests, though - so I'm a bit wary to touch the code. Chances are I'm going to introduce a couple of regressions :)
The text was updated successfully, but these errors were encountered: