|
| 1 | +# Disable Comments |
| 2 | + |
| 3 | +Pickier supports ESLint-style disable comments to suppress rules inline without modifying your configuration. |
| 4 | + |
| 5 | +## Syntax Options |
| 6 | + |
| 7 | +You can use either `eslint-` or `pickier-` prefix for all disable directives. |
| 8 | + |
| 9 | +## Disable Next Line |
| 10 | + |
| 11 | +Disable rules for the immediately following line of code. |
| 12 | + |
| 13 | +```typescript |
| 14 | +// eslint-disable-next-line rule1, rule2 |
| 15 | +const x = debugger // debugger allowed here |
| 16 | + |
| 17 | +// pickier-disable-next-line no-console |
| 18 | +console.log('Allowed') // console allowed here |
| 19 | + |
| 20 | +// Multiple rules |
| 21 | +// eslint-disable-next-line noDebugger, noConsole |
| 22 | +console.log(debugger) // Both allowed |
| 23 | +``` |
| 24 | + |
| 25 | +**Syntax:** |
| 26 | +- `// eslint-disable-next-line rule1, rule2` |
| 27 | +- `// pickier-disable-next-line rule1, rule2` |
| 28 | + |
| 29 | +**Rules:** |
| 30 | +- Rules are comma-separated |
| 31 | +- Whitespace around commas is ignored |
| 32 | +- Both bare rule IDs and plugin-prefixed IDs work |
| 33 | + |
| 34 | +## Disable All Rules (Next Line) |
| 35 | + |
| 36 | +Disable all rules for the next line by omitting the rule list. |
| 37 | + |
| 38 | +```typescript |
| 39 | +// eslint-disable-next-line |
| 40 | +const x = debugger // All rules disabled |
| 41 | + |
| 42 | +// pickier-disable-next-line |
| 43 | +console.log(eval('code')) // All rules disabled |
| 44 | +``` |
| 45 | + |
| 46 | +## Block Disable/Enable |
| 47 | + |
| 48 | +Disable rules for a range of lines using block comments. |
| 49 | + |
| 50 | +```typescript |
| 51 | +/* eslint-disable noDebugger, noConsole */ |
| 52 | +debugger |
| 53 | +console.log('test') |
| 54 | +/* eslint-enable noDebugger, noConsole */ |
| 55 | + |
| 56 | +// Or with pickier prefix |
| 57 | +/* pickier-disable style/curly, ts/prefer-const */ |
| 58 | +if (true) x = 1 |
| 59 | +/* pickier-enable style/curly, ts/prefer-const */ |
| 60 | +``` |
| 61 | + |
| 62 | +**Features:** |
| 63 | +- Works across multiple lines |
| 64 | +- Can be nested |
| 65 | +- Supports multiple rules |
| 66 | +- Must use `/* */` block comment syntax |
| 67 | + |
| 68 | +## Inline Disable/Enable |
| 69 | + |
| 70 | +Use single-line comments to disable rules for the rest of the file or until re-enabled. |
| 71 | + |
| 72 | +```typescript |
| 73 | +// eslint-disable noConsole |
| 74 | +console.log('Allowed from here') |
| 75 | +console.log('Still allowed') |
| 76 | +// eslint-enable noConsole |
| 77 | +console.log('Not allowed') // Will trigger warning |
| 78 | + |
| 79 | +// pickier-disable style/curly |
| 80 | +if (true) x = 1 // Allowed |
| 81 | +if (false) y = 2 // Allowed |
| 82 | +// pickier-enable style/curly |
| 83 | +if (test) z = 3 // Will require curly braces |
| 84 | +``` |
| 85 | + |
| 86 | +## File-Level Disable |
| 87 | + |
| 88 | +Disable rules for the entire file by placing directive on line 1. |
| 89 | + |
| 90 | +```typescript |
| 91 | +/* eslint-disable */ |
| 92 | +// All rules disabled for entire file |
| 93 | +debugger |
| 94 | +console.log('anything goes') |
| 95 | +``` |
| 96 | + |
| 97 | +Or disable specific rules: |
| 98 | + |
| 99 | +```typescript |
| 100 | +/* eslint-disable noDebugger, noConsole */ |
| 101 | +// Only these two rules disabled for entire file |
| 102 | +debugger |
| 103 | +console.log('test') |
| 104 | +``` |
| 105 | + |
| 106 | +## Rule ID Formats |
| 107 | + |
| 108 | +Disable comments support multiple rule ID formats: |
| 109 | + |
| 110 | +### Bare Rule IDs |
| 111 | + |
| 112 | +```typescript |
| 113 | +// eslint-disable-next-line noDebugger |
| 114 | +// eslint-disable-next-line no-console |
| 115 | +``` |
| 116 | + |
| 117 | +### Plugin-Prefixed IDs |
| 118 | + |
| 119 | +```typescript |
| 120 | +// eslint-disable-next-line style/curly |
| 121 | +// eslint-disable-next-line ts/prefer-const |
| 122 | +// eslint-disable-next-line regexp/no-super-linear-backtracking |
| 123 | +``` |
| 124 | + |
| 125 | +### ESLint Compatibility |
| 126 | + |
| 127 | +```typescript |
| 128 | +// eslint-disable-next-line eslint/no-unused-vars |
| 129 | +// Works with 'eslint/' prefix for compatibility |
| 130 | +``` |
| 131 | + |
| 132 | +### Multiple Formats Mixed |
| 133 | + |
| 134 | +```typescript |
| 135 | +// eslint-disable-next-line noDebugger, style/curly, ts/prefer-const |
| 136 | +// All formats can be mixed in same directive |
| 137 | +``` |
| 138 | + |
| 139 | +## Examples |
| 140 | + |
| 141 | +### Disable Debugger for Development |
| 142 | + |
| 143 | +```typescript |
| 144 | +function debug() { |
| 145 | + // eslint-disable-next-line noDebugger |
| 146 | + debugger // OK during development |
| 147 | +} |
| 148 | +``` |
| 149 | + |
| 150 | +### Disable Console in Scripts |
| 151 | + |
| 152 | +```typescript |
| 153 | +/* eslint-disable noConsole */ |
| 154 | +// CLI script - console output is intentional |
| 155 | +console.log('Starting process...') |
| 156 | +console.log('Complete') |
| 157 | +/* eslint-enable noConsole */ |
| 158 | +``` |
| 159 | + |
| 160 | +### Temporary Style Exception |
| 161 | + |
| 162 | +```typescript |
| 163 | +// eslint-disable-next-line style/curly |
| 164 | +if (isDev) return console.log('Dev mode') |
| 165 | + |
| 166 | +// Normal code resumes |
| 167 | +if (isProd) { |
| 168 | + return startProduction() |
| 169 | +} |
| 170 | +``` |
| 171 | + |
| 172 | +### Legacy Code Migration |
| 173 | + |
| 174 | +```typescript |
| 175 | +/* eslint-disable ts/no-explicit-any, quality/no-var */ |
| 176 | +// Legacy code section being migrated |
| 177 | +var data: any = getLegacyData() |
| 178 | +/* eslint-enable ts/no-explicit-any, quality/no-var */ |
| 179 | + |
| 180 | +// New code follows best practices |
| 181 | +const typedData: UserData = getNewData() |
| 182 | +``` |
| 183 | + |
| 184 | +### Disable All in Test Fixture |
| 185 | + |
| 186 | +```typescript |
| 187 | +/* eslint-disable */ |
| 188 | +// Test fixture - intentionally problematic code |
| 189 | +export const badCode = ` |
| 190 | + debugger |
| 191 | + console.log('test') |
| 192 | + var x = 1 |
| 193 | +` |
| 194 | +``` |
| 195 | + |
| 196 | +## Implementation Details |
| 197 | + |
| 198 | +### Binary Search Optimization |
| 199 | + |
| 200 | +Pickier uses binary search to efficiently match disable directives to code lines, making disable comments fast even in large files. |
| 201 | + |
| 202 | +### Comment Line Tracking |
| 203 | + |
| 204 | +Disable directives are tracked separately from code, so they don't interfere with line number reporting. |
| 205 | + |
| 206 | +### Precedence |
| 207 | + |
| 208 | +More specific disables take precedence over broader ones: |
| 209 | + |
| 210 | +1. Next-line disable (highest precedence) |
| 211 | +2. Block disable/enable ranges |
| 212 | +3. Inline disable/enable |
| 213 | +4. File-level disable |
| 214 | + |
| 215 | +### Suppression Map |
| 216 | + |
| 217 | +Internally, Pickier maintains a suppression map tracking: |
| 218 | +- Next-line suppressions |
| 219 | +- Range-based suppressions (disable/enable blocks) |
| 220 | +- File-level suppressions |
| 221 | + |
| 222 | +This allows O(1) or O(log n) lookup when checking if a rule is suppressed. |
| 223 | + |
| 224 | +## Best Practices |
| 225 | + |
| 226 | +### Use Sparingly |
| 227 | + |
| 228 | +Disable comments should be the exception, not the rule. Prefer fixing issues over suppressing them. |
| 229 | + |
| 230 | +```typescript |
| 231 | +// Bad - hiding the problem |
| 232 | +// eslint-disable-next-line noDebugger |
| 233 | +debugger |
| 234 | + |
| 235 | +// Good - fix the problem |
| 236 | +// Remove debugger before commit |
| 237 | +``` |
| 238 | + |
| 239 | +### Be Specific |
| 240 | + |
| 241 | +Disable only the rules you need, not all rules. |
| 242 | + |
| 243 | +```typescript |
| 244 | +// Bad - too broad |
| 245 | +// eslint-disable-next-line |
| 246 | +console.log('test') |
| 247 | + |
| 248 | +// Good - specific |
| 249 | +// eslint-disable-next-line noConsole |
| 250 | +console.log('test') |
| 251 | +``` |
| 252 | + |
| 253 | +### Add Context |
| 254 | + |
| 255 | +Explain why you're disabling a rule. |
| 256 | + |
| 257 | +```typescript |
| 258 | +// Console output intentional for CLI tool |
| 259 | +// eslint-disable-next-line noConsole |
| 260 | +console.log(result) |
| 261 | + |
| 262 | +// Debugger needed for complex async debugging |
| 263 | +// eslint-disable-next-line noDebugger |
| 264 | +debugger |
| 265 | +``` |
| 266 | + |
| 267 | +### Limit Scope |
| 268 | + |
| 269 | +Use the smallest scope possible. |
| 270 | + |
| 271 | +```typescript |
| 272 | +// Bad - disables for entire file |
| 273 | +/* eslint-disable noConsole */ |
| 274 | +console.log('one line') |
| 275 | +// ... rest of file |
| 276 | + |
| 277 | +// Good - disable only what's needed |
| 278 | +// eslint-disable-next-line noConsole |
| 279 | +console.log('one line') |
| 280 | +``` |
| 281 | + |
| 282 | +### Re-enable Promptly |
| 283 | + |
| 284 | +When using block disable, re-enable as soon as possible. |
| 285 | + |
| 286 | +```typescript |
| 287 | +// Bad - never re-enabled |
| 288 | +/* eslint-disable noConsole */ |
| 289 | +console.log('test') |
| 290 | +// ... many lines later |
| 291 | + |
| 292 | +// Good - re-enable immediately |
| 293 | +/* eslint-disable noConsole */ |
| 294 | +console.log('test') |
| 295 | +/* eslint-enable noConsole */ |
| 296 | +``` |
| 297 | + |
| 298 | +## Troubleshooting |
| 299 | + |
| 300 | +### Directive Not Working |
| 301 | + |
| 302 | +**Check syntax:** |
| 303 | +- Must be on separate line before code |
| 304 | +- Correct comment syntax (`//` or `/* */`) |
| 305 | +- No typos in rule IDs |
| 306 | + |
| 307 | +**Check rule ID:** |
| 308 | +```typescript |
| 309 | +// Wrong - no spaces in rule IDs |
| 310 | +// eslint-disable-next-line no Debugger |
| 311 | + |
| 312 | +// Wrong - incorrect rule name |
| 313 | +// eslint-disable-next-line debugger |
| 314 | + |
| 315 | +// Correct |
| 316 | +// eslint-disable-next-line noDebugger |
| 317 | +``` |
| 318 | + |
| 319 | +### Multiple Line Disable Not Working |
| 320 | + |
| 321 | +Use block comments, not single-line: |
| 322 | + |
| 323 | +```typescript |
| 324 | +// Wrong - single line comment |
| 325 | +// eslint-disable noConsole |
| 326 | +console.log('test') |
| 327 | +// eslint-enable noConsole |
| 328 | + |
| 329 | +// Correct - block comment or inline comment |
| 330 | +/* eslint-disable noConsole */ |
| 331 | +console.log('test') |
| 332 | +/* eslint-enable noConsole */ |
| 333 | +``` |
| 334 | + |
| 335 | +### Rule Still Firing |
| 336 | + |
| 337 | +Verify you're using the correct rule ID: |
| 338 | + |
| 339 | +```typescript |
| 340 | +// Check the error message for the exact rule ID |
| 341 | +// Error: "Unexpected console call (noConsole)" |
| 342 | + |
| 343 | +// Use the exact rule ID from error |
| 344 | +// eslint-disable-next-line noConsole |
| 345 | +console.log('test') |
| 346 | +``` |
| 347 | + |
| 348 | +## See Also |
| 349 | + |
| 350 | +- [Configuration](/config) - Configure rules globally |
| 351 | +- [Rules Overview](/rules/overview) - All available rules |
| 352 | +- [CLI Reference](/cli) - Command-line options |
0 commit comments