|
| 1 | +<a name="user-content-ts-ban-ts-comment"></a> |
| 2 | +<a name="ts-ban-ts-comment"></a> |
| 3 | +# <code>ts-ban-ts-comment</code> |
| 4 | + |
| 5 | +Disallows `@ts-<directive>` comments (`@ts-expect-error`, `@ts-ignore`, |
| 6 | +`@ts-nocheck`, `@ts-check`) or requires that they be accompanied by a |
| 7 | +description, mirroring the behavior of |
| 8 | +[`@typescript-eslint/ban-ts-comment`](https://typescript-eslint.io/rules/ban-ts-comment/). |
| 9 | + |
| 10 | +This rule is only recommended for `typescript-flavor` configs (i.e., those |
| 11 | +using TypeScript-style JSDoc without an actual TypeScript compiler), since |
| 12 | +`typescript` configs are expected to already have `typescript-eslint` |
| 13 | +enabled, which offers its own `@typescript-eslint/ban-ts-comment` rule for |
| 14 | +this purpose. |
| 15 | + |
| 16 | +Note that when `ts-ignore` is banned (the default), this rule always |
| 17 | +suggests replacing `@ts-ignore` with `@ts-expect-error`, since the latter |
| 18 | +will itself report an error if the following line turns out not to need |
| 19 | +suppressing. However, `@ts-expect-error` requires the following line to |
| 20 | +*always* error, so it is unsuitable if, e.g., a codebase is type-checked |
| 21 | +against multiple TypeScript versions and the line only errors under some |
| 22 | +of them; in that scenario, `@ts-ignore` remains the appropriate choice. |
| 23 | +Since the rule cannot detect this (it does not run the type checker), such |
| 24 | +codebases should instead configure `ts-ignore` as `false` or |
| 25 | +`"allow-with-description"` so the necessary `@ts-ignore` comments are not |
| 26 | +reported. |
| 27 | + |
| 28 | +<a name="user-content-ts-ban-ts-comment-options"></a> |
| 29 | +<a name="ts-ban-ts-comment-options"></a> |
| 30 | +## Options |
| 31 | + |
| 32 | +A single options object has the following properties. |
| 33 | + |
| 34 | +<a name="user-content-ts-ban-ts-comment-options-minimumdescriptionlength"></a> |
| 35 | +<a name="ts-ban-ts-comment-options-minimumdescriptionlength"></a> |
| 36 | +### <code>minimumDescriptionLength</code> |
| 37 | + |
| 38 | +A minimum character length for descriptions when `allow-with-description` is enabled. Defaults to `3`. |
| 39 | + |
| 40 | +<a name="user-content-ts-ban-ts-comment-options-ts-check"></a> |
| 41 | +<a name="ts-ban-ts-comment-options-ts-check"></a> |
| 42 | +### <code>ts-check</code> |
| 43 | + |
| 44 | +Whether (and how) to allow `@ts-check` directives. |
| 45 | + |
| 46 | +<a name="user-content-ts-ban-ts-comment-options-ts-expect-error"></a> |
| 47 | +<a name="ts-ban-ts-comment-options-ts-expect-error"></a> |
| 48 | +### <code>ts-expect-error</code> |
| 49 | + |
| 50 | +Whether (and how) to allow `@ts-expect-error` directives. |
| 51 | + |
| 52 | +<a name="user-content-ts-ban-ts-comment-options-ts-ignore"></a> |
| 53 | +<a name="ts-ban-ts-comment-options-ts-ignore"></a> |
| 54 | +### <code>ts-ignore</code> |
| 55 | + |
| 56 | +Whether (and how) to allow `@ts-ignore` directives. |
| 57 | + |
| 58 | +<a name="user-content-ts-ban-ts-comment-options-ts-nocheck"></a> |
| 59 | +<a name="ts-ban-ts-comment-options-ts-nocheck"></a> |
| 60 | +### <code>ts-nocheck</code> |
| 61 | + |
| 62 | +Whether (and how) to allow `@ts-nocheck` directives. |
| 63 | + |
| 64 | + |
| 65 | +||| |
| 66 | +|---|---| |
| 67 | +|Context|everywhere| |
| 68 | +|Tags|``| |
| 69 | +|Recommended|false, but recommended for `typescript-flavor` configs| |
| 70 | +|Settings|| |
| 71 | + |
| 72 | +<a name="user-content-ts-ban-ts-comment-failing-examples"></a> |
| 73 | +<a name="ts-ban-ts-comment-failing-examples"></a> |
| 74 | +## Failing examples |
| 75 | + |
| 76 | +The following patterns are considered problems: |
| 77 | + |
| 78 | +````ts |
| 79 | +// @ts-ignore |
| 80 | +foo(); |
| 81 | +// Message: Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free. |
| 82 | + |
| 83 | +/* @ts-ignore */ |
| 84 | +foo(); |
| 85 | +// Message: Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free. |
| 86 | + |
| 87 | +// @ts-nocheck |
| 88 | +foo(); |
| 89 | +// Message: Do not use "@ts-nocheck" because it alters compilation errors. |
| 90 | + |
| 91 | +// @ts-nocheck |
| 92 | +// Message: Do not use "@ts-nocheck" because it alters compilation errors. |
| 93 | + |
| 94 | +// @ts-check |
| 95 | +foo(); |
| 96 | +// "jsdoc/ts-ban-ts-comment": ["error"|"warn", {"ts-check":true}] |
| 97 | +// Message: Do not use "@ts-check" because it alters compilation errors. |
| 98 | + |
| 99 | +// @ts-expect-error |
| 100 | +foo(); |
| 101 | +// "jsdoc/ts-ban-ts-comment": ["error"|"warn", {"ts-expect-error":true}] |
| 102 | +// Message: Do not use "@ts-expect-error" because it alters compilation errors. |
| 103 | + |
| 104 | +// @ts-expect-error |
| 105 | +foo(); |
| 106 | +// Message: Include a description after the "@ts-expect-error" directive to explain why the @ts-expect-error is necessary. The description must be 3 characters or longer. |
| 107 | + |
| 108 | +// @ts-expect-error: TS |
| 109 | +foo(); |
| 110 | +// "jsdoc/ts-ban-ts-comment": ["error"|"warn", {"minimumDescriptionLength":10}] |
| 111 | +// Message: Include a description after the "@ts-expect-error" directive to explain why the @ts-expect-error is necessary. The description must be 10 characters or longer. |
| 112 | + |
| 113 | +/* |
| 114 | + @ts-expect-error: not matching format text */ |
| 115 | +foo(); |
| 116 | +// "jsdoc/ts-ban-ts-comment": ["error"|"warn", {"ts-expect-error":{"descriptionFormat":"^: TS\\d+ because .+$"}}] |
| 117 | +// Message: The description for the "@ts-expect-error" directive must match the ^: TS\d+ because .+$ format. |
| 118 | +```` |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | +<a name="user-content-ts-ban-ts-comment-passing-examples"></a> |
| 123 | +<a name="ts-ban-ts-comment-passing-examples"></a> |
| 124 | +## Passing examples |
| 125 | + |
| 126 | +The following patterns are not considered problems: |
| 127 | + |
| 128 | +````ts |
| 129 | +// A regular comment. |
| 130 | +/* Another regular comment. */ |
| 131 | +function foo () {} |
| 132 | + |
| 133 | +/** JSDoc comment. */ |
| 134 | +function foo () {} |
| 135 | + |
| 136 | +// @ts-check |
| 137 | +foo(); |
| 138 | + |
| 139 | +foo(); |
| 140 | +// @ts-nocheck |
| 141 | + |
| 142 | +// @ts-check |
| 143 | +foo(); |
| 144 | +// "jsdoc/ts-ban-ts-comment": ["error"|"warn", {"ts-check":{}}] |
| 145 | + |
| 146 | +// @ts-ignore |
| 147 | +foo(); |
| 148 | +// "jsdoc/ts-ban-ts-comment": ["error"|"warn", {"ts-ignore":false}] |
| 149 | + |
| 150 | +// @ts-expect-error: Suppress next line |
| 151 | +foo(); |
| 152 | + |
| 153 | +// @ts-expect-error: TS2345 because reasons |
| 154 | +foo(); |
| 155 | +// "jsdoc/ts-ban-ts-comment": ["error"|"warn", {"ts-expect-error":{"descriptionFormat":"^: TS\\d+ because .+$"}}] |
| 156 | +```` |
| 157 | + |
0 commit comments