|
6 | 6 | * [`contexts` format](#user-content-advanced-ast-and-selectors-contexts-format)
|
7 | 7 | * [Discovering available AST definitions](#user-content-advanced-ast-and-selectors-discovering-available-ast-definitions)
|
8 | 8 | * [Uses/Tips for AST](#user-content-advanced-ast-and-selectors-uses-tips-for-ast)
|
| 9 | +* [Creating your own rules](#user-content-advanced-creating-your-own-rules) |
| 10 | + * [Forbidding structures](#user-content-advanced-creating-your-own-rules-forbidding-structures) |
9 | 11 |
|
10 | 12 |
|
11 | 13 | <a name="user-content-advanced-ast-and-selectors"></a>
|
@@ -100,3 +102,83 @@ we have
|
100 | 102 | to the selector you wish so as to get messages reported in the bottom right
|
101 | 103 | pane which match your [esquery](https://github.com/estools/esquery/#readme)
|
102 | 104 | selector).
|
| 105 | + |
| 106 | +<a name="user-content-advanced-creating-your-own-rules"></a> |
| 107 | +<a name="advanced-creating-your-own-rules"></a> |
| 108 | +### Creating your own rules |
| 109 | + |
| 110 | +<a name="user-content-advanced-creating-your-own-rules-forbidding-structures"></a> |
| 111 | +<a name="advanced-creating-your-own-rules-forbidding-structures"></a> |
| 112 | +#### Forbidding structures |
| 113 | + |
| 114 | +Although `jsdoc/no-restricted-syntax` is available for restricting certain syntax, |
| 115 | +it comes at a cost that, no matter how many restrictions one adds, one can only |
| 116 | +disable a single restriction by disabling them all. |
| 117 | + |
| 118 | +With the `extraRuleDefinitions.forbid` option, one can add information that is used |
| 119 | +to create extra individual rules forbidding specific structures, and these rules can |
| 120 | +then be selectively enabled and optionally disabled on a case-by-case basis. |
| 121 | + |
| 122 | +For each `forbid` key, add the name of the context (this will be appended to |
| 123 | +`forbid-` to decide the name of the rule, so with "Any" as the key, the rule |
| 124 | +created will be `forbid-Any`). Then provide an optional `description` key |
| 125 | +(which will be used for the created rule's `meta.docs.description`) and the |
| 126 | +`contexts` array. See the `jsdoc/restricted-syntax` rule for more details. |
| 127 | + |
| 128 | +```js |
| 129 | +import {jsdoc} from 'eslint-plugin-jsdoc'; |
| 130 | + |
| 131 | +export default [ |
| 132 | + jsdoc({ |
| 133 | + config: 'flat/recommended', |
| 134 | + extraRuleDefinitions: { |
| 135 | + forbid: { |
| 136 | + Any: { |
| 137 | + contexts: [ |
| 138 | + { |
| 139 | + comment: 'JsdocBlock:has(JsdocTypeName[value="any"])', |
| 140 | + context: 'any', |
| 141 | + message: '`any` is not allowed; use a more specific type', |
| 142 | + }, |
| 143 | + ], |
| 144 | + descriptions: 'Testing here', |
| 145 | + }, |
| 146 | + Function: { |
| 147 | + contexts: [ |
| 148 | + { |
| 149 | + comment: 'JsdocBlock:has(JsdocTypeName[value="Function"])', |
| 150 | + context: 'any', |
| 151 | + message: '`Function` is not allowed; use a more specific type', |
| 152 | + }, |
| 153 | + ], |
| 154 | + }, |
| 155 | + }, |
| 156 | + }, |
| 157 | + // Be sure to enable the rules as well |
| 158 | + rules: { |
| 159 | + 'jsdoc/forbid-Any': [ |
| 160 | + 'error', |
| 161 | + ], |
| 162 | + 'jsdoc/forbid-Function': [ |
| 163 | + 'warn', |
| 164 | + ], |
| 165 | + }, |
| 166 | + }), |
| 167 | +]; |
| 168 | +``` |
| 169 | + |
| 170 | +Now you can selectively disable the rules you have created. In the following, |
| 171 | +because of the individual disable directive, only the `Function` rule will be |
| 172 | +triggered (as a warning since its rule was set to "warn"): |
| 173 | + |
| 174 | +```js |
| 175 | +/* eslint-disable jsdoc/forbid-Any */ |
| 176 | +/** |
| 177 | + * @param {any} abc Test |
| 178 | + * @param {Function} def Test2 |
| 179 | + */ |
| 180 | +export const a = (abc, def) => { |
| 181 | + b(5, abc, def); |
| 182 | +}; |
| 183 | +/* eslint-enable jsdoc/forbid-Any */ |
| 184 | +``` |
0 commit comments