最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Best way to generate prettier compliant TypeScript code - Stack Overflow

programmeradmin1浏览0评论

I would like to generate Prettier compliant code (interfaces) without formatting it in my code using a lot of conditions. This fails because of too long lines.

In my current project we parse Swagger/openAPI files (using the @apidevtools/swagger-parser package) from backends with different technology stacks, remove the technical required parts (which are added "magically" in the communication service), and generate interfaces for the endpoints. The generated interfaces and the source Swagger files are versioned as part of the codebase with git.

We use Prettier to format our code and additionally reformat the code as pre-commit-hook; we also check in the pipeline whether the code is well formatted. Additionally, we check whether the generated interfaces matche the definitions in the Swagger files by re-generating the interfaces and checking whether there are any changes. This prevents manual changes to generated code.

Till now it worked without any problem. But now the backend decided to use oneOf / anyOf which results in really long lines because of union types. Prettier complains about the long lines and the formatter in the pre-commit-hook splits them. The diff of the formatted and generated code is not empty and our pipeline fails.

I would like to generate code which is Prettier compliant. I guess it would be possible by using AST and pretty-printing the code, but I am not sure how to get the correct configuration from the Prettier config. I would like to avoid excluding the files from formatting. Different formatting is harder to read, the IDE (IntelliJ and VS Code) might mess with it despite of the exclusion, and it seems to be a quick&dirty solution. Additionally, if the "workflow" for creating compliant the is available and uses the AST, I consider using this for altering Schematic generated files as part of own Schematics, too.

Can I just use the prettier printer as motioned in .html#printers ? Or are there any libraries/packages available to transform code according to Prettier config during the generation? Or do you have another solutions/ideas?

发布评论

评论列表(0)

  1. 暂无评论