I’m excited to share the release of a new C, C++, and Java formatter based on Clang for the Node.js environment. This project is a fresh take inspired by Angular’s deprecated `clang-format` package.
I previously introduced this project a month or two ago, but I’m sharing it again as it has become even more stable and new features have been added. Thanks to the interest and support from the community, and the adoption by legacy users, the package recently hit over 10,000 weekly downloads. I am deeply grateful for this support.
This package now has full test coverage and a PR is in progress to replace the old Angular `clang-format` in the Node.js core with this updated package. While it’s thoroughly tested, there could still be edge cases I might have missed, so any feedback is welcome!
---
The package leverages Clang’s `clang-format`, known for its widespread use in formatting C, C++, and Java code. Think of it as similar to Prettier, but specifically for languages like C, C++, and Java, which Prettier does not support.
This formatter is especially useful in the Node.js environment when you’re working on projects that mix JavaScript with C/C++ code, such as core Node.js development. The package eliminates the need to install additional dependencies for using `clang-format`. While the previous `clang-format` package was challenging to integrate into Node.js and npm workflows, this new package streamlines the process. You can easily set it up in CI by simply downloading it via npm and configuring your scripts, which makes it much easier to use in automated environments.
---
With the recent update to version 1.2.0, we’ve added several new features, notably the `git-clang-format` package.
Let’s consider a scenario where you want to introduce code formatting for a C/C++ project. In smaller projects, you can reformat all the code at once without causing too much disruption. However, in larger projects, reformatting the entire codebase at once can make commit history harder to track.
Now, let’s imagine a developer modifies 100 lines within a 1,000-line codebase. Instead of reformatting the entire file, it would be better to check and format only the modified lines. This keeps the commit history clean and consistent. Additionally, as the project evolves, formatting becomes more uniform over time. `git-clang-format` helps achieve this by allowing formatting to be applied only to modified lines, similar to how `lint-staged` checks only staged files to optimize CI speed.
I’ve also wrapped `git-clang-format` as a standalone package, leveraging LLVM’s native support for this feature. The detailed instructions are available in the official documentation, though currently only in English.
Thank you for reading, and please feel free to leave any questions or feedback!
Official Documentation: [https://clang-format-node.lumir.page](https://clang-format-n... Repository: [https://github.com/lumirlumir/npm-clang-format-node](https:/...