code-comment-check
Version:
A fast, SWC-powered CLI for enforcing comment standards on variable declarations before commits.
166 lines (119 loc) β’ 11.5 kB
Markdown
# π§© code-comment-check
> π§ A lightweight, fast CLI tool that checks whether your variable declarations are properly commented before committing code.
> Ensure code readability and consistency across your team with one simple command.
[πγζ₯ι
δΈζζζ‘£θ―·ηΉε»θΏιγ](https://github.com/laoer536/code-comment-check/blob/main/README.zh.md)
## π Why use code-comment-check?
Modern projects often enforce strict code style rules β yet **comment standards** are easy to overlook.
`code-comment-check` solves exactly that problem:
* β
Automatically detects missing comments for each variable declaration
* β
Prevents committing code with missing comments
* β
By default, only checks **changed lines** in the current commit β fast and efficient
* β
Supports a `--strict` mode to inspect all declarations in changed files
* π« Ignores test files by default (`__tests__`, `.test.ts`, `.spec.tsx`, etc.)
Use it in your local workflow or CI pipeline to enforce consistent documentation standards across your team.
## π¦ Installation
```bash
# Recommended
pnpm add -D code-comment-check
# Or using npm / yarn
npm install -D code-comment-check
# or
yarn add -D code-comment-check
```
After installation, the CLI command is automatically registered:
```bash
comment-check
```
## βοΈ Usage
### πΉ Default mode (recommended)
```bash
pnpm comment-check
```
Default behavior:
* Only checks **changed lines** in the committed files
* Automatically **ignores test files**
* Displays missing comment entries in a formatted table (file, line, declaration)
* Returns a non-zero exit code if issues are found, blocking the commit
### πΉ Strict mode
```bash
pnpm comment-check --strict
```
In strict mode, the tool scans all declarations in changed files,
making it ideal for **code review** or **CI environments**.
## πͺ Integrating with Husky (pre-commit hook)
In your `.husky/pre-commit` file, add the following:
```bash
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
pnpm comment-check
```
Effect:
* β
If all checks pass β the commit proceeds
* β If any declaration lacks a comment β a table of missing comments is printed, and the commit is blocked
## π‘ Example Output
```
π Checking annotation comments...
β οΈ Missing annotation comments:
ββββββββββββββββββββββββββββββββββββββββββ¬βββββββββ¬βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β File β Line β Declaration β
ββββββββββββββββββββββββββββββββββββββββββΌβββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β test/test2.ts β 1 β const test = '123' β
ββββββββββββββββββββββββββββββββββββββββββΌβββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β test/test2.ts β 2 β let test1 = '123' β
ββββββββββββββββββββββββββββββββββββββββββΌβββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β test/test2.ts β 3 β var test2 = '123' β
ββββββββββββββββββββββββββββββββββββββββββΌβββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β test/test2.ts β 4 β export const test3 = '123' β
ββββββββββββββββββββββββββββββββββββββββββΌβββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β test/test2.ts β 5 β export let test4 = '123' β
ββββββββββββββββββββββββββββββββββββββββββΌβββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β test/test2.ts β 6 β export var test5 = '123' β
ββββββββββββββββββββββββββββββββββββββββββΌβββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β test/test2.ts β 8 β interface Type { β
ββββββββββββββββββββββββββββββββββββββββββΌβββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β test/test2.ts β 12 β type UserType = 'admin' | 'user' β
ββββββββββββββββββββββββββββββββββββββββββΌβββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β test/test2.ts β 14 β export interface User { β
ββββββββββββββββββββββββββββββββββββββββββΌβββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β test/test2.ts β 20 β export type UserWithType = User & { β
ββββββββββββββββββββββββββββββββββββββββββΌβββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β test/test2.ts β 24 β const bb = '123' β
ββββββββββββββββββββββββββββββββββββββββββΌβββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β test/test2.ts β 25 β const cc = '123' β
ββββββββββββββββββββββββββββββββββββββββββΌβββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β test/test2.ts β 26 β const dd = '123' β
ββββββββββββββββββββββββββββββββββββββββββΌβββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β test/test2.ts β 28 β function fn() { β
ββββββββββββββββββββββββββββββββββββββββββΌβββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β test/test2.ts β 32 β class Test { β
ββββββββββββββββββββββββββββββββββββββββββΌβββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β test/test2.ts β 38 β enum TestEnum { β
ββββββββββββββββββββββββββββββββββββββββββ΄βββββββββ΄βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Please add comments before committing your code.
```
## π Features & Advantages
| Feature | Description |
| --------------------- |-------------------------------------------------------|
| β‘οΈ High performance | Built on the SWC compiler β much faster than Babel |
| π§© Zero configuration | Works out of the box, automatically registers the CLI |
| π¬ Clear output | Displays missing comment details in a table |
| π§ Smart parsing | Supports Javascript / TypeScript / JSX / TSX syntax |
| πͺΆ Lightweight | Non-intrusive β analyzes AST without modifying code |
| π§ Extensible | Supports the `--strict` flag for broader checks |
## π Use Cases
* Enforcing code documentation standards
* Pre-commit comment validation
* Automated CI code inspection
* Integration in internal dev tools or monorepo setups
## π License
MIT Β© 2025 β maintained by [laoer536](https://github.com/laoer536)