If you’ve ever used DAX Formatter to format your DAX code and wondered why there isn’t an equivalent for Power Query/M, then wonder no more: the nice people at Power Pivot Insights in Germany (read their German-language blog here) have built one. You can find it here:
https://powerqueryformatter.com/
They also have an API you can use too.
Bonus information: did you know that Microsoft also has an open source parser for M, available here?