Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for specifying ignore rules in .clang-format-ignore file using gitignore syntax #87

Merged
merged 12 commits into from
Dec 31, 2023

Conversation

lopopolo
Copy link
Member

@lopopolo lopopolo commented Dec 30, 2023

Closes #55.

This is a fairly large rewrite of this small package.

The flow of the CLI program is reworked to the following stages:

  • Walk given directory, recursing into all directories, and yielding paths to files that end in the given extensions.
  • Relativize the yielded paths to the given directory, filter with ignore, and absolutize the paths.
  • Format or check sources.
  • Print report.

These refactors contain many performance optimizations which make file path walking and path filtering much faster.

To support this new flow, this PR adds two new options to the CLI, in the style of eslint:

  • --ext, which specifies the comma-separated set of file extensions to include for formatting. Default: .c,.cc,.cpp,.h
  • --ignore-path, which specifies the path to a clang-format ignore file. Default: .clang-format-ignore.

The .clang-format-ignore file is parsed with the ignore npm package, which is compatible with the gitignore spec.

This PR also adds the ESM-only chalk v5 dependency for slightly nicer colorized reporting. I upgraded the package to ESM (mostly by converting require to import and changing export syntax; I also had to replace __dirname).

clang-format --help

$ npx . --help
Usage: clang-format [options] [directory]

Node.js runner for LLVM clang-format

clang-format is a tool to format C code.

If no arguments are given, the runner formats the current directory recursively.
Any clang-format configuration present on the filesystem will be honored.

--check mode is suitable for CI. --check does not attempt to format and instead
exits with a non-zero status code if the source code on disk does not match the
enforced style.

Options:
  -V, --version         output the version number
  -c, --check           check if the given files are formatted. Exit with a non-
                        zero status code if any formatting errors are found.
  --ext <extensions>    specify formattable file extensions (default: ".c,.cc,.cpp,.h")
  --ignore-path <path>  specify path of ignore file (default: ".clang-format-ignore")
  --no-ignore           disable use of ignore files and patterns
  -h, --help            display help for command

A significant refactor (maybe rewrite?) focused around adding support
for a `.clang-format-ignore` file as requested in #55.

Add two new options to the CLI, in the style of eslint:

- `--ext`, which specifies the comma-separated set of file extensions
  to include for formatting. Default: .c,.cc,.cpp,.h
- `--ignore-path`, which specifies the path to a clang-format ignore
  file. Default: .clang-format-ignore.

The `.clang-format-ignore` file is parsed with the `ignore` npm package,
which is compatible with the gitignore spec.

The CLI is rewritten to have the following phases:

- Walk given directory, recursing into all directories, and yielding
  paths to files that end in the given extensions.
- Relativize the yielded paths to the given directory, filter with
  `ignore`, and absolutize the paths.
- Format or check sources.
- Print report.
@lopopolo lopopolo added C-enhancement Category: New feature or request. A-cli Area: CLI runner. A-deps Area: Source and library dependencies. A-clang-format Area: Wrapper around clang-format. labels Dec 30, 2023
@lopopolo lopopolo force-pushed the lopopolo/clang-format-ignore branch from 243a83c to c801ec8 Compare December 30, 2023 08:37
@lopopolo lopopolo merged commit 12b4ab1 into trunk Dec 31, 2023
@lopopolo lopopolo deleted the lopopolo/clang-format-ignore branch December 31, 2023 16:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-clang-format Area: Wrapper around clang-format. A-cli Area: CLI runner. A-deps Area: Source and library dependencies. C-enhancement Category: New feature or request.
Development

Successfully merging this pull request may close these issues.

Ignore some files or directory ?
1 participant