|
| 1 | +# Expected Behavior: `prefer-optional-chain` Rule |
| 2 | + |
| 3 | +This rule enforces the use of optional chaining (`?.`) over logical AND (`&&`) for property access when the same object is being checked and accessed. |
| 4 | + |
| 5 | +## ❌ Invalid Examples |
| 6 | + |
| 7 | +```typescript |
| 8 | +// Logical AND with property access (same object) |
| 9 | +const name = user && user.name |
| 10 | +const email = user && user.profile && user.profile.email |
| 11 | +const count = data && data.items && data.items.length |
| 12 | + |
| 13 | +// Logical AND with method calls (same object) |
| 14 | +const result = api && api.getData() |
| 15 | +const user = service && service.getUser(id) |
| 16 | +const processed = data && data.filter().map() |
| 17 | + |
| 18 | +// Logical AND with computed property access (same object) |
| 19 | +const value = obj && obj[key] |
| 20 | +const item = array && array[index] |
| 21 | +const prop = config && config[setting] |
| 22 | + |
| 23 | +// Complex nested access patterns |
| 24 | +const fullName = user && user.profile && user.profile.name |
| 25 | +const settings = config && config.user && config.user.preferences |
| 26 | +const data = response && response.data && response.data.results |
| 27 | + |
| 28 | +// In function parameters and returns |
| 29 | +function getUserName(user) { |
| 30 | + return user && user.name |
| 31 | +} |
| 32 | + |
| 33 | +const getUserEmail = user => user && user.profile && user.profile.email |
| 34 | + |
| 35 | +// In object properties |
| 36 | +const userData = { |
| 37 | + name: user && user.name, |
| 38 | + email: user && user.profile && user.profile.email |
| 39 | +} |
| 40 | + |
| 41 | +// In template literals |
| 42 | +const greeting = `Hello ${user && user.name}!` |
| 43 | +const status = `Count: ${data && data.items && data.items.length}` |
| 44 | + |
| 45 | +// In class methods |
| 46 | +class UserService { |
| 47 | + getName(user) { |
| 48 | + return user && user.name |
| 49 | + } |
| 50 | + |
| 51 | + getProfile(user) { |
| 52 | + return user && user.profile && user.profile.details |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +// In async functions |
| 57 | +async function fetchUserData(id) { |
| 58 | + const user = await api.getUser(id) |
| 59 | + return user && user.profile && user.profile.email |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +## ✅ Valid Examples |
| 64 | + |
| 65 | +```typescript |
| 66 | +// Optional chaining with property access (correct) |
| 67 | +const name = user?.name |
| 68 | +const email = user?.profile?.email |
| 69 | +const count = data?.items?.length |
| 70 | + |
| 71 | +// Optional chaining with method calls (correct) |
| 72 | +const result = api?.getData() |
| 73 | +const user = service?.getUser(id) |
| 74 | +const processed = data?.filter()?.map() |
| 75 | + |
| 76 | +// Optional chaining with computed property access (correct) |
| 77 | +const value = obj?.[key] |
| 78 | +const item = array?.[index] |
| 79 | +const prop = config?.[setting] |
| 80 | + |
| 81 | +// Complex nested access patterns with optional chaining |
| 82 | +const fullName = user?.profile?.name |
| 83 | +const settings = config?.user?.preferences |
| 84 | +const data = response?.data?.results |
| 85 | + |
| 86 | +// In function parameters and returns |
| 87 | +function getUserName(user) { |
| 88 | + return user?.name |
| 89 | +} |
| 90 | + |
| 91 | +const getUserEmail = user => user?.profile?.email |
| 92 | + |
| 93 | +// In object properties |
| 94 | +const userData = { |
| 95 | + name: user?.name, |
| 96 | + email: user?.profile?.email |
| 97 | +} |
| 98 | + |
| 99 | +// In template literals |
| 100 | +const greeting = `Hello ${user?.name}!` |
| 101 | +const status = `Count: ${data?.items?.length}` |
| 102 | + |
| 103 | +// In class methods |
| 104 | +class UserService { |
| 105 | + getName(user) { |
| 106 | + return user?.name |
| 107 | + } |
| 108 | + |
| 109 | + getProfile(user) { |
| 110 | + return user?.profile?.details |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +// In async functions |
| 115 | +async function fetchUserData(id) { |
| 116 | + const user = await api.getUser(id) |
| 117 | + return user?.profile?.email |
| 118 | +} |
| 119 | + |
| 120 | +// Logical AND with different objects (not affected by this rule) |
| 121 | +const result = user && profile.name |
| 122 | +const data = api && otherService.getData() |
| 123 | + |
| 124 | +// Logical AND with literals (not affected by this rule) |
| 125 | +const flag = user && 'active' |
| 126 | +const message = data && 'loaded' |
| 127 | + |
| 128 | +// Logical AND with function calls (not affected by this rule) |
| 129 | +const result = user && getName() |
| 130 | +const data = api && processData() |
| 131 | + |
| 132 | +// Already using optional chaining (not affected by this rule) |
| 133 | +const name = user?.name |
| 134 | +const email = user?.profile?.email |
| 135 | + |
| 136 | +// Logical OR (not affected by this rule) |
| 137 | +const name = user || user.name |
| 138 | +const email = user || user.profile?.email |
| 139 | +``` |
| 140 | + |
| 141 | +## Rule Scope |
| 142 | + |
| 143 | +This rule **only applies to**: |
| 144 | + |
| 145 | +- ✅ Logical AND (`&&`) operators where the same object is being checked and accessed: |
| 146 | + - `object && object.property` → `object?.property` |
| 147 | + - `object && object.method()` → `object?.method()` |
| 148 | + - `object && object[key]` → `object?.[key]` |
| 149 | + |
| 150 | +This rule **does NOT apply to**: |
| 151 | + |
| 152 | +- ❌ Logical AND (`&&`) with different objects (`user && profile.name`) |
| 153 | +- ❌ Logical AND (`&&`) with literals (`user && 'active'`) |
| 154 | +- ❌ Logical AND (`&&`) with function calls (`user && getName()`) |
| 155 | +- ❌ Logical OR (`||`) operators |
| 156 | +- ❌ Already using optional chaining (`user?.name`) |
| 157 | +- ❌ Other logical operators (`!`, `??`) |
| 158 | + |
| 159 | +## Why This Rule Matters |
| 160 | + |
| 161 | +The logical AND (`&&`) pattern for property access is verbose and error-prone: |
| 162 | + |
| 163 | +```typescript |
| 164 | +// ❌ Verbose and repetitive |
| 165 | +const name = user && user.name |
| 166 | +const email = user && user.profile && user.profile.email |
| 167 | +const count = data && data.items && data.items.length |
| 168 | +``` |
| 169 | + |
| 170 | +Optional chaining (`?.`) is more concise and readable: |
| 171 | + |
| 172 | +```typescript |
| 173 | +// ✅ Concise and clear |
| 174 | +const name = user?.name |
| 175 | +const email = user?.profile?.email |
| 176 | +const count = data?.items?.length |
| 177 | +``` |
| 178 | + |
| 179 | +## Auto-fix Behavior |
| 180 | + |
| 181 | +The rule provides auto-fix suggestions that replace `&&` with `?.`: |
| 182 | + |
| 183 | +- `user && user.name` → `user?.name` |
| 184 | +- `user && user.getName()` → `user?.getName()` |
| 185 | +- `obj && obj[key]` → `obj?.[key]` |
| 186 | +- `api && api.getData(id)` → `api?.getData(id)` |
| 187 | +- `user && user.profile && user.profile.email` → `user?.profile?.email` |
0 commit comments