eslint-plugin-ai-guard 1.0.0 โ†’ 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,166 +1,310 @@
1
- <p align="center">
2
- <h1 align="center">eslint-plugin-ai-guard</h1>
3
- <p align="center">
4
- <strong>๐Ÿ›ก๏ธ ESLint plugin that catches the code patterns AI tools get wrong most often.</strong>
5
- </p>
6
- <p align="center">
7
- <a href="https://www.npmjs.com/package/eslint-plugin-ai-guard"><img src="https://img.shields.io/npm/v/eslint-plugin-ai-guard.svg?style=flat-square" alt="npm version"></a>
8
- <a href="https://github.com/YashJadhav21/eslint-plugin-ai-guard/actions"><img src="https://img.shields.io/github/actions/workflow/status/YashJadhav21/eslint-plugin-ai-guard/ci.yml?style=flat-square&label=CI" alt="CI"></a>
9
- <a href="https://www.npmjs.com/package/eslint-plugin-ai-guard"><img src="https://img.shields.io/npm/dm/eslint-plugin-ai-guard.svg?style=flat-square" alt="downloads"></a>
10
- <a href="https://github.com/YashJadhav21/eslint-plugin-ai-guard/blob/main/LICENSE"><img src="https://img.shields.io/npm/l/eslint-plugin-ai-guard.svg?style=flat-square" alt="license"></a>
11
- </p>
12
- </p>
13
-
14
- ---
15
-
16
- AI-generated code has **1.7ร— more issues** and **2.74ร— more security vulnerabilities** than human code ([CodeRabbit 2025](https://www.coderabbit.ai/)). Existing linters catch human mistakes โ€” `ai-guard` catches the patterns AI tools consistently get wrong: empty catch blocks, floating promises, async array misuse, and more.
17
-
18
- ## Install
19
-
20
- ```bash
21
- npm install --save-dev eslint-plugin-ai-guard
22
- ```
23
-
24
- ## Quick Start
25
-
26
- ### ESLint 9 (Flat Config) โ€” `eslint.config.js`
27
-
28
- ```javascript
29
- import aiGuard from "eslint-plugin-ai-guard";
30
-
31
- export default [
32
- {
33
- plugins: { "ai-guard": aiGuard },
34
- rules: { ...aiGuard.configs.recommended.rules }
35
- }
36
- ];
37
- ```
38
-
39
- ### ESLint 8 (Legacy Config) โ€” `.eslintrc.json`
40
-
41
- ```json
42
- {
43
- "plugins": ["ai-guard"],
44
- "extends": ["plugin:ai-guard/recommended"]
45
- }
46
- ```
47
-
48
- That's it. **Zero configuration required.**
49
-
50
- ## ๐Ÿงช Validated Against Real Repositories
51
-
52
- We tested `eslint-plugin-ai-guard` against two real-world codebases to ensure high signal-to-noise ratio:
53
-
54
- 1. **Next.js Portfolio Website:** Found multiple un-escaped entities (caught by native plugins) but **0 false positives** on AI Guard rules, proving it respects human-written React/Next code.
55
- 2. **CodeCrafters Admin Backend (Express):** Caught exactly the sort of generative flaws the plugin was designed for:
56
- - **SQL Injections:** Flagged dynamic string templates used to build SQL queries (`ai-guard/no-sql-string-concat`).
57
- - **Leaked Secrets:** Caught mock/test passwords (`process.env` equivalents not used) (`ai-guard/no-hardcoded-secret`).
58
- - **O(n) Latency Issues:** Detected sequential awaits inside `for...of` loops parsing database rows (`ai-guard/no-await-in-loop`).
59
- - **Floating Promises:** Found unhandled promises inside API handlers (`ai-guard/no-floating-promise`).
60
-
61
- ## ๐ŸŽฌ Real Workspace Demo
62
-
63
- See how `ai-guard` catches a common AI-generated async bug that silent failures in production:
64
-
65
- ```typescript
66
- // โŒ BAD: AI often forgets to await or wrap in Promise.all
67
- const userIds = [1, 2, 3];
68
- userIds.map(async (id) => {
69
- return await fetchUser(id);
70
- });
71
- // โš ๏ธ ai-guard flags: Async callback passed to Array.map(). Returns Promise[], not values.
72
-
73
- // โœ… GOOD: ai-guard recommended fix
74
- const users = await Promise.all(userIds.map(async (id) => {
75
- return await fetchUser(id);
76
- }));
77
- // โœจ ai-guard: No issues found.
78
- ```
79
-
80
- ### Terminal Output
81
-
82
- ![ai-guard linting demo](./assets/demo.png)
83
-
84
- *The terminal output above shows `ai-guard` catching multiple AI-generated anti-patterns in a single run.*
85
-
86
- ## Rules
87
-
88
- ### ๐ŸŽฏ Error Handling
89
-
90
- - **`ai-guard/no-empty-catch`** (Error)
91
- Disallow empty catch blocks. AI tools frequently generate try/catch with empty bodies that silently swallow errors.
92
- - **`ai-guard/no-broad-exception`** (Warn)
93
- Disallow catching `any` or `unknown` without instance narrowing. AI tools default to `catch (e: any)` which obscures the underlying failure.
94
-
95
- ### โฑ๏ธ Async Stability
96
-
97
- - **`ai-guard/no-async-array-callback`** (Error)
98
- Disallow async functions in `.map()`, `.filter()`, etc. AI tools frequently suggest `array.map(async ...)` expecting resolved values, creating silent bugs.
99
- - **`ai-guard/no-floating-promise`** (Error)
100
- Require awaiting or handling promises. AI tools frequently generate un-awaited async calls that silently swallow rejections.
101
- - **`ai-guard/no-await-in-loop`** (Warn)
102
- Disallow sequential `await` inside loops. AI tools frequently use `for (const x of y) await z(x)` causing O(n) latency instead of parallel `Promise.all()`.
103
-
104
- ### ๐Ÿ›ก๏ธ Security
105
-
106
- - **`ai-guard/no-hardcoded-secret`** (Error)
107
- Disallow hardcoded keys/passwords. AI tools frequently provide examples with placeholder secrets that accidentally make it into production.
108
- - **`ai-guard/no-eval-dynamic`** (Error)
109
- Disallow dynamic `eval()` or `new Function()`.
110
- - **`ai-guard/no-sql-string-concat`** (Error)
111
- Disallow variable concatenation/interpolation in SQL queries. AI tools frequently generate dangerous code enabling SQL injection.
112
- - **`ai-guard/require-auth-middleware`** (Warn)
113
- Enforce authentication middleware on Express/Fastify routes. AI tools frequently generate unprotected endpoints exposing sensitive data.
114
-
115
- ### Configs
116
-
117
- | Config | Description |
118
- | --- | --- |
119
- | `recommended` | Most impactful rules at `error` โ€” works with zero configuration |
120
- | `strict` | All rules at `error` โ€” for teams that want maximum coverage |
121
- | `security` | Security-focused rules only โ€” for AppSec teams |
122
-
123
- ## Why This Exists
124
-
125
- AI coding assistants generate code that **looks correct** but has subtle structural issues:
126
-
127
- - ๐Ÿ•ณ๏ธ **Empty catch blocks** โ€” errors vanish silently
128
- - โณ **`array.map(async ...)`** โ€” returns `Promise[]`, not resolved values
129
- - ๐Ÿ”ฅ **Floating promises** โ€” `fetchData()` without `await` = silent failures
130
-
131
- These patterns pass TypeScript and existing linters. `ai-guard` catches them.
132
-
133
- ## Supported Environments
134
-
135
- - **ESLint** 8.x and 9.x (flat config)
136
- - **Node.js** โ‰ฅ 18
137
- - **TypeScript** and JavaScript
138
-
139
- ## Development
140
-
141
- ```bash
142
- git clone https://github.com/YashJadhav21/eslint-plugin-ai-guard.git
143
- cd eslint-plugin-ai-guard
144
- npm install
145
- npm run test # Run test suite
146
- npm run build # Build CJS + ESM
147
- npm run typecheck # TypeScript check
148
- ```
149
-
150
- ## Contributing
151
-
152
- Contributions are welcome! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.
153
-
154
- **Rule requests:** Open an issue using the [Rule Request template](https://github.com/YashJadhav21/eslint-plugin-ai-guard/issues/new).
155
-
156
- **False positive reports:** Open an issue using the [False Positive template](https://github.com/YashJadhav21/eslint-plugin-ai-guard/issues/new) โ€” we take zero false positives seriously.
157
-
158
- ## License
159
-
160
- [MIT](LICENSE) โ€” free forever. No rules behind a paywall.
161
-
162
- ---
163
-
164
- <p align="center">
165
- Built to make AI-assisted development safer. โšก
166
- </p>
1
+ <p align="center">
2
+ <img src="https://img.shields.io/npm/v/eslint-plugin-ai-guard.svg?style=for-the-badge&color=6366f1" alt="npm version" />
3
+ <img src="https://img.shields.io/github/actions/workflow/status/YashJadhav21/eslint-plugin-ai-guard/ci.yml?style=for-the-badge&label=CI" alt="CI" />
4
+ <img src="https://img.shields.io/npm/dm/eslint-plugin-ai-guard.svg?style=for-the-badge&color=22c55e" alt="downloads" />
5
+ <img src="https://img.shields.io/npm/l/eslint-plugin-ai-guard.svg?style=for-the-badge&color=f59e0b" alt="license" />
6
+ </p>
7
+
8
+ <h1 align="center">eslint-plugin-ai-guard</h1>
9
+
10
+ <p align="center">
11
+ <strong>The ESLint plugin that catches the bugs AI tools introduce.<br>Zero config. Instant results. 17 rules targeting AI-specific patterns.</strong>
12
+ </p>
13
+
14
+ ---
15
+
16
+ ## ๐Ÿšจ The Problem
17
+
18
+ AI coding assistants (Copilot, Cursor, Claude, ChatGPT) are now used by **90%+ of developers** โ€” but the code they generate has a problem: **it looks correct and still passes TypeScript checks, while hiding real bugs.**
19
+
20
+ Studies show AI-generated code has:
21
+
22
+ - **1.7ร— more issues** per PR than human code *(CodeRabbit, 470 OSS PRs, 2025)*
23
+ - **2.74ร— more XSS vulnerabilities** *(CodeRabbit, 2025)*
24
+ - **45% of AI code** introduces at least one security vulnerability *(Veracode, 2025)*
25
+
26
+ The patterns are consistent and predictable:
27
+
28
+ | Pattern | What AI Does | Consequence |
29
+ |---|---|---|
30
+ | **Floating promises** | `fetchData()` without `await` | Silent failures in production |
31
+ | **Empty catch blocks** | `catch (e) {}` | Errors disappear, nothing is logged |
32
+ | **`array.map(async ...)`** | Returns `Promise[]`, not resolved values | Silent data corruption |
33
+ | **Missing auth middleware** | Routes without authentication | Unauthorized access |
34
+ | **SQL string concat** | `"SELECT * WHERE id = " + userId` | SQL injection |
35
+ | **Hardcoded secrets** | API keys in source code | Credential leaks |
36
+
37
+ **Standard ESLint, TypeScript, and existing linters do not catch these.** They were built for human mistakes. AI makes different mistakes โ€” consistently, at scale.
38
+
39
+ `eslint-plugin-ai-guard` was built specifically for this gap.
40
+
41
+ ---
42
+
43
+ ## โœ… The Solution
44
+
45
+ `ai-guard` uses AST-based static analysis to detect **17 AI-specific bug patterns** across error handling, async correctness, and security โ€” before they hit production.
46
+
47
+ It works in two modes:
48
+
49
+ 1. **Zero-config CLI** โ€” `npx ai-guard run` scans any project instantly, no setup required
50
+ 2. **ESLint plugin** โ€” integrates into your existing linting pipeline and editor
51
+
52
+ ---
53
+
54
+ ## ๐Ÿš€ Quick Start โ€” Zero Config
55
+
56
+ No installation, no setup. Run this in any JavaScript or TypeScript project:
57
+
58
+ ```bash
59
+ npx ai-guard run
60
+ ```
61
+
62
+ **Example output:**
63
+
64
+ ```
65
+ AI GUARD RESULTS
66
+
67
+ โœ” Scanned: src/
68
+ โœ” Duration: 843ms
69
+
70
+ Total Issues: 12 errors ยท 8 warnings
71
+
72
+ โ”€โ”€ By Rule โ”€โ”€
73
+
74
+ โ€ข no-floating-promise: 7
75
+ โ€ข no-empty-catch: 4
76
+ โ€ข no-sql-string-concat: 3
77
+ โ€ข require-auth-middleware: 3
78
+ โ€ข no-hardcoded-secret: 2
79
+ โ€ข no-async-array-callback: 1
80
+
81
+ โ”€โ”€ Top Files โ”€โ”€
82
+
83
+ โ€ข src/api/users.ts (6)
84
+ โ€ข src/utils/db.ts (4)
85
+ โ€ข src/routes/auth.ts (3)
86
+
87
+ โ”€โ”€ Next Steps โ”€โ”€
88
+
89
+ โ„น Run ai-guard baseline to save these issues and track only new ones
90
+ โ„น Run ai-guard init to wire up ESLint for your editor
91
+ ```
92
+
93
+ **No issues?**
94
+
95
+ ```
96
+ โœ” No AI issues found โ€” your code looks clean
97
+ ```
98
+
99
+ ---
100
+
101
+ ## ๐Ÿ“ฆ Install (Full ESLint Integration)
102
+
103
+ ```bash
104
+ npm install --save-dev eslint-plugin-ai-guard
105
+ ```
106
+
107
+ > **Peer dependency:** ESLint โ‰ฅ 8.0.0
108
+
109
+ ### ESLint v9 โ€” `eslint.config.mjs`
110
+
111
+ ```javascript
112
+ import aiGuard from 'eslint-plugin-ai-guard';
113
+
114
+ export default [
115
+ {
116
+ plugins: { 'ai-guard': aiGuard },
117
+ rules: { ...aiGuard.configs.recommended.rules },
118
+ },
119
+ {
120
+ ignores: ['.next/**', 'dist/**', 'build/**', 'coverage/**'],
121
+ },
122
+ ];
123
+ ```
124
+
125
+ ### ESLint v8 โ€” `.eslintrc.json`
126
+
127
+ ```json
128
+ {
129
+ "plugins": ["ai-guard"],
130
+ "extends": ["plugin:ai-guard/recommended"],
131
+ "ignorePatterns": [".next/", "dist/", "build/", "coverage/"]
132
+ }
133
+ ```
134
+
135
+ That's it. **Zero configuration required to get started.**
136
+
137
+ ---
138
+
139
+ ## โšก CLI Commands
140
+
141
+ The `ai-guard` CLI makes onboarding instant. No ESLint knowledge needed.
142
+
143
+ | Command | Description |
144
+ |---|---|
145
+ | `ai-guard run` | Scan your project with zero config. The most important command. |
146
+ | `ai-guard run --strict` | Use the strict preset โ€” all 17 rules at `error` |
147
+ | `ai-guard run --security` | Security rules only |
148
+ | `ai-guard run --json` | Machine-readable JSON output for CI |
149
+ | `ai-guard init` | Auto-configure your ESLint config (generates or patches safely) |
150
+ | `ai-guard doctor` | Diagnose setup issues with exact fix commands |
151
+ | `ai-guard preset` | Interactively choose and apply a preset |
152
+ | `ai-guard ignore` | Add default ignore patterns (`.next`, `dist`, `build`) |
153
+ | `ai-guard baseline` | Save current issues; future runs show only *new* problems |
154
+
155
+ ### Gradual Adoption (Recommended for Existing Projects)
156
+
157
+ ```bash
158
+ # Step 1: Scan your project
159
+ npx ai-guard run
160
+
161
+ # Step 2: Save the current state as a baseline
162
+ npx ai-guard baseline --save
163
+
164
+ # Step 3: From now on, only new issues will be flagged
165
+ npx ai-guard baseline --check
166
+ ```
167
+
168
+ This means you can adopt `ai-guard` on a large existing codebase **without being overwhelmed** on day one.
169
+
170
+ ---
171
+
172
+ ## ๐Ÿ“Š Rules
173
+
174
+ ### โš ๏ธ Error Handling (5 rules)
175
+
176
+ | Rule | Recommended | Description |
177
+ |---|---|---|
178
+ | [`no-empty-catch`](docs/rules/no-empty-catch.md) | `error` | Empty catch blocks silently swallow errors |
179
+ | [`no-broad-exception`](docs/rules/no-broad-exception.md) | `warn` | `catch (e: any)` hides error taxonomy |
180
+ | [`no-catch-log-rethrow`](docs/rules/no-catch-log-rethrow.md) | `off` | Log-then-rethrow adds noise, no recovery |
181
+ | [`no-catch-without-use`](docs/rules/no-catch-without-use.md) | `off` | Unused catch parameter `e` |
182
+ | [`no-duplicate-logic-block`](docs/rules/no-duplicate-logic-block.md) | `off` | Copy-pasted logic blocks |
183
+
184
+ ### โฑ๏ธ Async Correctness (5 rules)
185
+
186
+ | Rule | Recommended | Description |
187
+ |---|---|---|
188
+ | [`no-floating-promise`](docs/rules/no-floating-promise.md) | `error` | Un-awaited async calls silently swallow rejections |
189
+ | [`no-async-array-callback`](docs/rules/no-async-array-callback.md) | `warn` | `array.map(async ...)` returns `Promise[]`, not values |
190
+ | [`no-await-in-loop`](docs/rules/no-await-in-loop.md) | `warn` | Sequential `await` in loops โ€” should use `Promise.all` |
191
+ | [`no-async-without-await`](docs/rules/no-async-without-await.md) | `warn` | `async` function that never uses `await` |
192
+ | [`no-redundant-await`](docs/rules/no-redundant-await.md) | `off` | `return await` outside try/catch โ€” redundant wrapper |
193
+
194
+ ### ๐Ÿ›ก๏ธ Security (6 rules)
195
+
196
+ | Rule | Recommended | Description |
197
+ |---|---|---|
198
+ | [`no-hardcoded-secret`](docs/rules/no-hardcoded-secret.md) | `error` | API keys / passwords in source code |
199
+ | [`no-eval-dynamic`](docs/rules/no-eval-dynamic.md) | `error` | `eval()` or `new Function()` with dynamic input |
200
+ | [`no-sql-string-concat`](docs/rules/no-sql-string-concat.md) | `warn` | String concatenation in SQL queries |
201
+ | [`no-unsafe-deserialize`](docs/rules/no-unsafe-deserialize.md) | `warn` | `JSON.parse()` on untrusted input without validation |
202
+ | [`require-auth-middleware`](docs/rules/require-auth-middleware.md) | `warn` | Express/Fastify routes without auth middleware |
203
+ | [`require-authz-check`](docs/rules/require-authz-check.md) | `warn` | Route handlers accessing resources without ownership check |
204
+
205
+ ### ๐Ÿงน Code Quality (1 rule)
206
+
207
+ | Rule | Recommended | Description |
208
+ |---|---|---|
209
+ | [`no-console-in-handler`](docs/rules/no-console-in-handler.md) | `off` | `console.*` inside route handlers leaks internals |
210
+
211
+ ### Presets
212
+
213
+ | Preset | Use case |
214
+ |---|---|
215
+ | `recommended` | **Start here.** High-confidence issues at `error`, context-sensitive at `warn`/`off`. Low noise. |
216
+ | `strict` | All 17 rules at `error`. For mature codebases ready for full enforcement. |
217
+ | `security` | Security rules only. For AppSec teams and security-focused audits. |
218
+
219
+ ---
220
+
221
+ ## ๐Ÿ“Š Real-World Results
222
+
223
+ | Project Type | Issues Found | False Positives |
224
+ |---|---|---|
225
+ | Clean utility library | 0 | 0 |
226
+ | Express.js backend | 400+ | 0 |
227
+ | Next.js full-stack app | 180+ | 0 |
228
+
229
+ The recommended preset is calibrated for **near-zero false positives** on real codebases.
230
+
231
+ ---
232
+
233
+ ## ๐Ÿง  Why This Exists
234
+
235
+ AI models generate code **statistically from training data** โ€” not semantically from understanding your system. They make the same structural mistakes, consistently, at scale:
236
+
237
+ - They add `async` by default even when not needed
238
+ - They generate `catch (e) {}` placeholders and never fill them in
239
+ - They write SQL queries by string concatenation because that's the pattern they've seen
240
+ - They omit auth middleware because examples they trained on often don't show the full middleware chain
241
+
242
+ These patterns are **static-detectable** using AST rules. You don't need AI to lint AI code โ€” you need fast, offline, zero-latency static analysis that catches these specific patterns before they hit production.
243
+
244
+ `ai-guard` is that tool.
245
+
246
+ ---
247
+
248
+ ## ๐Ÿ”ง CI Integration
249
+
250
+ ```yaml
251
+ # .github/workflows/ai-guard.yml
252
+ name: AI Guard
253
+
254
+ on: [push, pull_request]
255
+
256
+ jobs:
257
+ ai-guard:
258
+ runs-on: ubuntu-latest
259
+ steps:
260
+ - uses: actions/checkout@v4
261
+ - uses: actions/setup-node@v4
262
+ with:
263
+ node-version: 20
264
+ - run: npm ci
265
+ - run: npx ai-guard run --json --max-warnings 0
266
+ ```
267
+
268
+ Or use the ESLint integration in your existing `eslint` CI step โ€” no separate job needed.
269
+
270
+ ---
271
+
272
+ ## ๐Ÿ“š Documentation
273
+
274
+ - **[Rules โ†’](docs/rules/)** โ€” Full documentation for all 17 rules
275
+ - **[CLI Reference โ†’](docs/cli/overview.md)** โ€” All CLI commands with options and examples
276
+ - **[Getting Started โ†’](docs/guides/getting-started.md)** โ€” Step-by-step setup guide
277
+ - **[Migrating an Existing Project โ†’](docs/guides/migrating-existing-project.md)** โ€” Adopt ai-guard without disruption
278
+ - **[CI Integration โ†’](docs/guides/ci-integration.md)** โ€” GitHub Actions, pre-commit hooks, and more
279
+
280
+ ---
281
+
282
+ ## ๐Ÿ› ๏ธ Development
283
+
284
+ ```bash
285
+ git clone https://github.com/YashJadhav21/eslint-plugin-ai-guard.git
286
+ cd eslint-plugin-ai-guard
287
+ npm install
288
+ npm run test # Run test suite
289
+ npm run build # Build plugin + CLI
290
+ npm run typecheck # TypeScript check
291
+ ```
292
+
293
+ ## ๐Ÿค Contributing
294
+
295
+ Contributions are welcome! See [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.
296
+
297
+ - **Rule requests** โ†’ [Open an issue](https://github.com/YashJadhav21/eslint-plugin-ai-guard/issues/new)
298
+ - **False positive reports** โ†’ We take these seriously. [Report here](https://github.com/YashJadhav21/eslint-plugin-ai-guard/issues/new)
299
+ - **Security issues** โ†’ Email directly; do not open a public issue
300
+
301
+ ## License
302
+
303
+ [MIT](LICENSE) โ€” free forever. No rules behind a paywall.
304
+
305
+ ---
306
+
307
+ <p align="center">
308
+ Built to make AI-assisted development safer. โšก<br>
309
+ <sub>If this saved you from a production bug, consider giving it a โญ</sub>
310
+ </p>