esformatter
esformatter copied to clipboard
format invalid JS files
I think we should replace esprima with acorn_loose, that way we can support ES6 features and also format files that contain JS errors.. this is a better behavior than simply displaying an error to the user.
PS: I started a branch on rocambole but there are a few differences on the loc
and range
info, so maybe we will introduce a few bugs...
:+1:
:+1:
looking at how esformatter-jsx-ignore works I think we can implement this feature as a plugin that parses the code using acorn_loose, replaces the invalid code with some comments and then restore it afterwards. - it's an ugly hack but definitely easier than replacing esprima with acorn.
I was thinking more about this problem today and I think we could implement it easily inside esformatter by wrapping the rocambole.parse
into a try/catch
and if it fails we fallback to acorn_loose
and remove all the blocks of code that are invalid and restore it afterwards (like the esformatter-jsx-ignore
plugin does).
it would be better if this feature was part of the tool itself and enabled by default (think about a text editor executing esformatter at each file save or when you leave edit mode in Vim)
Is there a chance this will be resolved soon?
@danawoodman this should take a considerable amount of work (maybe a few hours in the best case scenario?).. I'm currently busy with other projects and focusing mainly in making sure esformatter works well with valid JavaScript input (and adding support to ES6 features).. Pull requests would be highly appreciated.
one thing to be aware is that accorn_loose
uses the indentation information to make a guess of block start/end, so it is not really that accurate... maybe there are other parsers that are better suited for our needs (we just need to figure out which parts of the code are invalid so that we can skip it).