|
| 1 | +# Expected Behavior: `prefer-nullish-coalescing` Rule |
| 2 | + |
| 3 | +This rule enforces the use of nullish coalescing operator (`??`) over logical OR (`||`) when dealing with null or undefined values, particularly when the right-hand side is a falsy but not nullish value. |
| 4 | + |
| 5 | +## ❌ Invalid Examples |
| 6 | + |
| 7 | +```typescript |
| 8 | +// Logical OR with empty string (falsy but not nullish) |
| 9 | +const displayName = user.name || '' |
| 10 | +const message = input || '' |
| 11 | +const title = config.title || '' |
| 12 | + |
| 13 | +// Logical OR with zero (falsy but not nullish) |
| 14 | +const count = items.length || 0 |
| 15 | +const index = searchIndex || 0 |
| 16 | +const timeout = settings.timeout || 0 |
| 17 | + |
| 18 | +// Logical OR with false (falsy but not nullish) |
| 19 | +const enabled = feature.flag || false |
| 20 | +const visible = config.show || false |
| 21 | +const required = field.required || false |
| 22 | + |
| 23 | +// Complex expressions with falsy values |
| 24 | +const result = user?.name || '' || 'Anonymous' |
| 25 | +const config = { |
| 26 | + apiUrl: process.env.API_URL || '', |
| 27 | + timeout: settings.timeout || 0, |
| 28 | + debug: process.env.DEBUG || false |
| 29 | +} |
| 30 | + |
| 31 | +// Function parameters with falsy defaults |
| 32 | +function createUser(name = user.name || '', age = user.age || 0) { |
| 33 | + return { name, age } |
| 34 | +} |
| 35 | + |
| 36 | +// Object properties with falsy values |
| 37 | +const user = { |
| 38 | + name: input.name || '', |
| 39 | + count: data.count || 0, |
| 40 | + active: status.active || false |
| 41 | +} |
| 42 | + |
| 43 | +// Template literals with falsy values |
| 44 | +const greeting = `Hello ${user.name || ''}!` |
| 45 | +const status = `Count: ${items.length || 0}` |
| 46 | +const message = `Debug: ${config.debug || false}` |
| 47 | +``` |
| 48 | + |
| 49 | +## ✅ Valid Examples |
| 50 | + |
| 51 | +```typescript |
| 52 | +// Nullish coalescing with empty string (correct) |
| 53 | +const displayName = user.name ?? '' |
| 54 | +const message = input ?? '' |
| 55 | +const title = config.title ?? '' |
| 56 | + |
| 57 | +// Nullish coalescing with zero (correct) |
| 58 | +const count = items.length ?? 0 |
| 59 | +const index = searchIndex ?? 0 |
| 60 | +const timeout = settings.timeout ?? 0 |
| 61 | + |
| 62 | +// Nullish coalescing with false (correct) |
| 63 | +const enabled = feature.flag ?? false |
| 64 | +const visible = config.show ?? false |
| 65 | +const required = field.required ?? false |
| 66 | + |
| 67 | +// Complex expressions with nullish coalescing |
| 68 | +const result = (user?.name ?? '') || 'Anonymous' |
| 69 | +const config = { |
| 70 | + apiUrl: process.env.API_URL ?? '', |
| 71 | + timeout: settings.timeout ?? 0, |
| 72 | + debug: process.env.DEBUG ?? false |
| 73 | +} |
| 74 | + |
| 75 | +// Function parameters with nullish coalescing defaults |
| 76 | +function createUser(name = user.name ?? '', age = user.age ?? 0) { |
| 77 | + return { name, age } |
| 78 | +} |
| 79 | + |
| 80 | +// Object properties with nullish coalescing |
| 81 | +const user = { |
| 82 | + name: input.name ?? '', |
| 83 | + count: data.count ?? 0, |
| 84 | + active: status.active ?? false |
| 85 | +} |
| 86 | + |
| 87 | +// Template literals with nullish coalescing |
| 88 | +const greeting = `Hello ${user.name ?? ''}!` |
| 89 | +const status = `Count: ${items.length ?? 0}` |
| 90 | +const message = `Debug: ${config.debug ?? false}` |
| 91 | + |
| 92 | +// Logical OR with non-falsy values (not affected by this rule) |
| 93 | +const name = user.name || 'Anonymous' |
| 94 | +const count = items.length || 10 |
| 95 | +const enabled = feature.flag || true |
| 96 | + |
| 97 | +// Logical AND (not affected by this rule) |
| 98 | +const result = user && user.name |
| 99 | +const isValid = input && input.length > 0 |
| 100 | + |
| 101 | +// Already using nullish coalescing (not affected by this rule) |
| 102 | +const displayName = user.name ?? 'Anonymous' |
| 103 | +const count = items.length ?? 10 |
| 104 | +const enabled = feature.flag ?? true |
| 105 | +``` |
| 106 | + |
| 107 | +## Rule Scope |
| 108 | + |
| 109 | +This rule **only applies to**: |
| 110 | + |
| 111 | +- ✅ Logical OR (`||`) operators where the right-hand side is a falsy but not nullish value: |
| 112 | + - Empty string (`""`) |
| 113 | + - Zero (`0`) |
| 114 | + - Boolean `false` |
| 115 | + |
| 116 | +This rule **does NOT apply to**: |
| 117 | + |
| 118 | +- ❌ Logical OR (`||`) with non-falsy values (`"hello"`, `1`, `true`) |
| 119 | +- ❌ Logical AND (`&&`) operators |
| 120 | +- ❌ Nullish coalescing (`??`) operators |
| 121 | +- ❌ Other logical operators (`!`, `&&`, `??`) |
| 122 | + |
| 123 | +## Why This Rule Matters |
| 124 | + |
| 125 | +The logical OR (`||`) operator treats **all** falsy values as "missing": |
| 126 | + |
| 127 | +- `""` (empty string) → treated as missing |
| 128 | +- `0` (zero) → treated as missing |
| 129 | +- `false` (boolean) → treated as missing |
| 130 | + |
| 131 | +The nullish coalescing (`??`) operator only treats `null` and `undefined` as missing: |
| 132 | + |
| 133 | +- `""` (empty string) → preserved |
| 134 | +- `0` (zero) → preserved |
| 135 | +- `false` (boolean) → preserved |
| 136 | + |
| 137 | +## Auto-fix Behavior |
| 138 | + |
| 139 | +The rule provides auto-fix suggestions that replace `||` with `??`: |
| 140 | + |
| 141 | +- `value || ""` → `value ?? ""` |
| 142 | +- `count || 0` → `count ?? 0` |
| 143 | +- `flag || false` → `flag ?? false` |
| 144 | +- `(user?.name || "") || false` → `(user?.name ?? "") || false` (first occurrence) |
0 commit comments