3 ms·
How do you avoid the problems of reformatting the output code? For example, I want long lines broken up in a consistent manner. Doing this correctly in a lang
by jcoffland 10y ago
How do you avoid the problems of reformatting the output code? For example, I want long lines broken up in a consistent manner. Doing this correctly in a language like JavaScript is hard. Also, how can I configure the tool to follow a formatting style other than the default?
- reergymerej 10y agoCheckout out the print format options for recast [1], which is what jscodeshift uses behind the scenes. [1]: https://github.com/benjamn/recast https://github.com/benjamn/recast
- fkling 10y agojscodeshift is primarily a wrapper around recast. Recast takes care of all the printing. It's smart enough to reuse the original source code for AST nodes that haven't been changed. As for formatting, there are some options to influence the pretty printer, but not a lot [1]. It will infer things like indentation from the existing source code. I'm not sure right now how it handles long lines. Whenever we run into issues with the printer, we just create a PR to fix it. Getting automatic code formatting right is apparently quite hard. There are dedicated tools for this, such as https://github.com/millermedeiros/esformatter https://github.com/millermedeiros/esformatter . [1]: https://github.com/benjamn/recast/blob/master/lib/options.js https://github.com/benjamn/recast/blob/master/lib/options.js