I had a few minutes so I wrote up a basic outline of this in more detail. I'm certainly not the first person to think of this but it's a shame I've never seen it gain widespread support. http://blog.shivoa.net/2017/11/tabs-vs-spaces-is-wrong-question.html
@aras @shivoa Reminds me of related work like http://bytes.schibsted.com/swift-format-part-1/ or http://esprima.org/demo/rewrite.html