peekchain
Version:
Optional chaining safety checker CLI
122 lines (80 loc) β’ 3.03 kB
Markdown
# π Peekchain

π **Catch unsafe optional chaining before it ships** β supports **CJS & ESM**, powered by Babel.
> A CLI tool that performs **deep AST-level validation of JavaScript optional chaining** for null safety β beyond what ESLint can catch.
## π What is Peekchain?
Optional chaining (e.g., `user?.profile?.name`) can **silently fail** or **crash** when misused (e.g., `user?.profile.name`). ESLint catches only some cases β `peekchain` fills the **critical safety gaps**.
## β
What it Detects
- β `user?.profile.name` β unsafe access after optional chaining
- β `user?.profile().name` β unsafe function call return
- β `user?.[key].name` β key access not safely chained
- β `user?.name = "x"` β invalid assignment
- β `delete user?.profile.name` β unsafe delete
- β `++user?.count` β invalid prefix increment
- β `user?.likes++` β invalid postfix increment
- β `function f({user}) {}` β destructuring tracked
- β `class A {}` β class declarations scanned
- β `const {name} = user ?? {}` β safe fallback destructuring supported
- β
`user?.profile?.name` β safe
## π¦ Installation
```bash
npm install -g peekchain
```
## π§ͺ Usage
```bash
peekchain path/to/your/file.js
```
Example:
```bash
peekchain index.js
```
## π§ͺ Tested Coverage (100%)
Peekchain now has full test coverage including:
- βοΈ CLI entry point via `require.main === module`
- βοΈ Misuse patterns like assignments & increments on optional chains
- βοΈ Deep AST constructs (functions, classes, destructuring)
- βοΈ Integration tested via `jest`, `fs`, `child_process`, mocks
## π οΈ Recommended Pre-commit Hook
Create `.husky/pre-commit` and add:
```bash
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
npx eslint . --max-warnings=0 || exit 1
FILES=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.js$')
for FILE in $FILES; do
peekchain "$FILE" || exit 1
done
```
## π Why Use This?
- ESLint doesnβt catch nested `user?.profile.name` as unsafe
- Peekchain does full AST parsing, walking the member chain safely
- Ensures all intermediate accesses are **safely guarded**
## π¦ Local Development
```bash
git clone https://github.com/mrajkishor/peekChain.git
cd peekchain
npm install
node lib/check.js yourfile.js
```
## π§ Tech Stack
- [@babel/parser](https://babel.dev/docs/babel-parser)
- [@babel/traverse](https://babel.dev/docs/babel-traverse)
- β
Supports analyzing JavaScript written in ES Modules (ESM)
## π Changelog
### v1.0.5 (May 2025)
- π§Ύ All logs now written to `logs/peekchain.log`
- π§Ή Log file is cleared before each run
- π₯οΈ Console shows `PASS` or `FAIL` with absolute log file path
## π License
MIT Β© Rajkishor (mrajkishor331@gmail.com)