UNPKG

peekchain

Version:

Optional chaining safety checker CLI

122 lines (80 loc) β€’ 3.03 kB
# πŸ” Peekchain ![npm](https://img.shields.io/npm/v/peekchain?color=blue) πŸ”’ **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)