unguard 0.7.1 → 0.8.0

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
@@ -2,7 +2,13 @@
2
2
 
3
3
  Unguard your code. Defend against overdefensive AI-generated code.
4
4
 
5
- Built on [oxc-parser](https://oxc.rs).
5
+ Type-aware static analysis powered by the TypeScript compiler API.
6
+
7
+ If `??` is on a non-nullable type, you don't need it.
8
+
9
+ If `?.` is on a guaranteed object, it's noise.
10
+
11
+ unguard proves it with types.
6
12
 
7
13
  ## Install
8
14
 
@@ -19,15 +25,16 @@ npx unguard
19
25
  ## Usage
20
26
 
21
27
  ```bash
22
- unguard src/
23
- unguard src/ --strict # treat warnings as errors (CI)
24
- unguard src/ --filter no-any-cast # run a single rule
25
- unguard src/ --severity=error # only show errors
26
- unguard src/ --format=flat # one-line-per-diagnostic, grepable
27
- unguard src/ --format=flat | grep error
28
+ unguard src
29
+ unguard src --strict # treat warnings as errors (CI)
30
+ unguard src --filter no-any-cast # run a single rule
31
+ unguard src --severity=error # only show errors
32
+ unguard src --severity=error,warning # errors and warnings only
33
+ unguard src --format=flat # one-line-per-diagnostic, grepable
34
+ unguard src --format=flat | grep error
28
35
  ```
29
36
 
30
- Add `unguard` to your lint check.
37
+ Add `unguard` to your lint check, especially if code is written by AI.
31
38
 
32
39
  ### Exit codes
33
40
 
@@ -40,19 +47,19 @@ Add `unguard` to your lint check.
40
47
  Use `--severity=error` in CI to only fail on errors:
41
48
 
42
49
  ```bash
43
- unguard src/ --severity=error || exit 1
50
+ unguard src --severity=error || exit 1
44
51
  ```
45
52
 
46
53
  ### Output formats
47
54
 
48
- **Grouped** (default) diagnostics grouped by file:
55
+ **Grouped** (default) -- diagnostics grouped by file:
49
56
 
50
57
  ```txt
51
58
  src/lib/probe.ts
52
59
  37:4 error Empty catch blocks hide failures... no-empty-catch
53
60
  ```
54
61
 
55
- **Flat** (`--format=flat`) one line per diagnostic, grepable:
62
+ **Flat** (`--format=flat`) -- one line per diagnostic, grepable:
56
63
 
57
64
  ```txt
58
65
  src/lib/probe.ts:37:4 error [no-empty-catch] Empty catch blocks hide failures...
@@ -69,26 +76,28 @@ src/lib/probe.ts:37:4 error [no-empty-catch] Empty catch blocks hide failures...
69
76
  | `no-type-assertion` | error | `x as unknown as T` |
70
77
  | `no-ts-ignore` | error | `@ts-ignore` / `@ts-expect-error` |
71
78
 
72
- ### Defensive code
79
+ ### Defensive code (type-aware)
80
+
81
+ These rules use the TypeScript type checker. Non-nullable types suppress the diagnostic; nullable types are flagged.
73
82
 
74
83
  | Rule | Severity | What it catches |
75
84
  | ---- | -------- | --------------- |
76
- | `no-optional-property-access` | info | `obj?.prop` |
77
- | `no-optional-element-access` | info | `obj?.[key]` |
78
- | `no-optional-call` | info | `fn?.()` |
79
- | `no-nullish-coalescing` | info | `x ?? fallback` |
80
- | `no-logical-or-fallback` | warning | `x \|\| fallback` |
85
+ | `no-optional-property-access` | warning | `obj?.prop` on a non-nullable type |
86
+ | `no-optional-element-access` | warning | `obj?.[key]` on a non-nullable type |
87
+ | `no-optional-call` | warning | `fn?.()` on a non-nullable type |
88
+ | `no-nullish-coalescing` | warning | `x ?? fallback` on a non-nullable type |
89
+ | `no-logical-or-fallback` | warning | `map.get(k) \|\| fallback` -- data-structure lookups where `??` is correct; `\|\|` on numeric types that swallow `0` |
81
90
  | `no-null-ternary-normalization` | warning | `x == null ? fallback : x` |
82
- | `no-non-null-assertion` | warning | `x!` |
91
+ | `no-non-null-assertion` | warning | `x!` on a nullable type without a local narrowing guard |
83
92
  | `no-double-negation-coercion` | info | `!!value` |
84
- | `no-redundant-existence-guard` | warning | `obj && obj.prop` |
93
+ | `no-redundant-existence-guard` | warning | `obj && obj.prop` on a non-nullable type |
85
94
 
86
95
  ### Error handling
87
96
 
88
97
  | Rule | Severity | What it catches |
89
98
  | ---- | -------- | --------------- |
90
- | `no-empty-catch` | error | `catch {}` with no body (comments count as annotation) |
91
- | `no-catch-return` | warning | `catch { return fallback }` without rethrowing |
99
+ | `no-empty-catch` | error | `catch {}` with no body and no comment |
100
+ | `no-catch-return` | warning | `catch { return fallback }` with no logging or rethrow |
92
101
  | `no-error-rewrap` | error | `throw new Error(e.message)` without `{ cause: e }` |
93
102
 
94
103
  ### Interface design