Skip to content

Commit 5573eb5

Browse files
committed
feat(check-tag-names): allow rejecting normally valid tag names and/or providing custom error messages when suggesting tags to reject or replace (fixes gajus#108)
Reporting normally valid tags may be of interest for tags like `@todo`. This is implemented by allowing the user to set targeted `tagNamePreference` tags to `false` or to an object with only a `message` and no `replacement` Custom messages for `check-tag-names` are implemented by allowing the user to set targeted `tagNamePreference` tags to an object with `message` and `replacement` (mirroring `preferredTypes` behavior). Note that for other rules leveraging `tagNamePreference` (via `utils.getPreferredTagName`) to find the user's preferred tag name, the `replacement` will be used in the likes of error messages but not the `message`. Also, for various (param, return, and description-related) rules which have used `tagNamePreference` (via the `utils.getPreferredTagName` utility), report to user if they have blocked (probably inadvertently) and not indicated a replacement for the relevant tag needed by the rule in the `tagNamePreference` setting.
1 parent 6baebd6 commit 5573eb5

27 files changed

+722
-14
lines changed

.README/README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,60 @@ Use `settings.jsdoc.tagNamePreference` to configure a preferred alias name for a
147147
}
148148
```
149149

150+
One may also use an object with a `message` and `replacement`, with `{{tagName}}` and `{{preferredType}}` (or its alias `{{replacement}}`) as template variables to be
151+
substituted within `message`.
152+
153+
The following will report the message `@extends is to be used over @augments as it is more evocative of classes than @augments` upon encountering `@augments`.
154+
155+
```json
156+
{
157+
"rules": {},
158+
"settings": {
159+
"jsdoc": {
160+
"tagNamePreference": {
161+
"augments": {
162+
"message": "@{{replacement}} is to be used over @{{tagName}} as it is more evocative of classes than @augments",
163+
"replacement": "extends"
164+
}
165+
}
166+
}
167+
}
168+
}
169+
```
170+
171+
If one wishes to reject a normally valid tag, e.g., `@todo`, one may set the tag to `false`:
172+
173+
```json
174+
{
175+
"rules": {},
176+
"settings": {
177+
"jsdoc": {
178+
"tagNamePreference": {
179+
"todo": false
180+
}
181+
}
182+
}
183+
}
184+
```
185+
186+
Or one may set the targeted tag to an object with a custom `message`, but without a `replacement` property:
187+
188+
```json
189+
{
190+
"rules": {},
191+
"settings": {
192+
"jsdoc": {
193+
"tagNamePreference": {
194+
"todo": {
195+
"message": "We expect immediate perfection, so don't leave to-dos in your code."
196+
}
197+
}
198+
}
199+
}
200+
}
201+
```
202+
203+
150204
The defaults in `eslint-plugin-jsdoc` (for tags which offer
151205
aliases) are as follows:
152206

README.md

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,60 @@ Use `settings.jsdoc.tagNamePreference` to configure a preferred alias name for a
197197
}
198198
```
199199

200+
One may also use an object with a `message` and `replacement`, with `{{tagName}}` and `{{preferredType}}` (or its alias `{{replacement}}`) as template variables to be
201+
substituted within `message`.
202+
203+
The following will report the message `@extends is to be used over @augments as it is more evocative of classes than @augments` upon encountering `@augments`.
204+
205+
```json
206+
{
207+
"rules": {},
208+
"settings": {
209+
"jsdoc": {
210+
"tagNamePreference": {
211+
"augments": {
212+
"message": "@{{replacement}} is to be used over @{{tagName}} as it is more evocative of classes than @augments",
213+
"replacement": "extends"
214+
}
215+
}
216+
}
217+
}
218+
}
219+
```
220+
221+
If one wishes to reject a normally valid tag, e.g., `@todo`, one may set the tag to `false`:
222+
223+
```json
224+
{
225+
"rules": {},
226+
"settings": {
227+
"jsdoc": {
228+
"tagNamePreference": {
229+
"todo": false
230+
}
231+
}
232+
}
233+
}
234+
```
235+
236+
Or one may set the targeted tag to an object with a custom `message`, but without a `replacement` property:
237+
238+
```json
239+
{
240+
"rules": {},
241+
"settings": {
242+
"jsdoc": {
243+
"tagNamePreference": {
244+
"todo": {
245+
"message": "We expect immediate perfection, so don't leave to-dos in your code."
246+
}
247+
}
248+
}
249+
}
250+
}
251+
```
252+
253+
200254
The defaults in `eslint-plugin-jsdoc` (for tags which offer
201255
aliases) are as follows:
202256

@@ -955,6 +1009,15 @@ export class SomeClass {
9551009
constructor(private property: string) {}
9561010
}
9571011
// Message: Expected @param names to be "property". Got "prop".
1012+
1013+
/**
1014+
* @param foo
1015+
*/
1016+
function quux (foo) {
1017+
1018+
}
1019+
// Settings: {"jsdoc":{"tagNamePreference":{"param":false}}}
1020+
// Message: Unexpected tag `@param`
9581021
````
9591022

9601023
The following patterns are not considered problems:
@@ -1276,6 +1339,42 @@ function quux (foo) {
12761339
}
12771340
// Settings: {"jsdoc":{"additionalTagNames":{"customTags":["bar"]}}}
12781341
// Message: Invalid JSDoc tag name "baz".
1342+
1343+
/**
1344+
* @todo
1345+
*/
1346+
function quux () {
1347+
1348+
}
1349+
// Settings: {"jsdoc":{"tagNamePreference":{"todo":false}}}
1350+
// Message: Blacklisted tag found (`@todo`)
1351+
1352+
/**
1353+
* @todo
1354+
*/
1355+
function quux () {
1356+
1357+
}
1358+
// Settings: {"jsdoc":{"tagNamePreference":{"todo":{"message":"Please resolve to-dos or add to the tracker"}}}}
1359+
// Message: Please resolve to-dos or add to the tracker
1360+
1361+
/**
1362+
* @todo
1363+
*/
1364+
function quux () {
1365+
1366+
}
1367+
// Settings: {"jsdoc":{"tagNamePreference":{"todo":{"message":"Please use {{replacement}} instead of {{tagName}}","replacement":"x-todo"}}}}
1368+
// Message: Please use x-todo instead of todo
1369+
1370+
/**
1371+
* @todo
1372+
*/
1373+
function quux () {
1374+
1375+
}
1376+
// Settings: {"jsdoc":{"tagNamePreference":{"todo":{"message":"Please use {{preferredTagName}} instead of {{tagName}}","replacement":"x-todo"}}}}
1377+
// Message: Please use x-todo instead of todo
12791378
````
12801379

12811380
The following patterns are not considered problems:
@@ -1391,6 +1490,13 @@ function quux (foo) {}
13911490
*/
13921491
function quux (foo) {
13931492

1493+
}
1494+
1495+
/**
1496+
* @todo
1497+
*/
1498+
function quux () {
1499+
13941500
}
13951501
````
13961502

@@ -3631,6 +3737,24 @@ var quux = {
36313737
};
36323738
// Options: [{"contexts":["ObjectExpression"]}]
36333739
// Message: Missing JSDoc @description declaration.
3740+
3741+
/**
3742+
* @someDesc
3743+
*/
3744+
function quux () {
3745+
3746+
}
3747+
// Settings: {"jsdoc":{"tagNamePreference":{"description":{"message":"Please avoid `{{tagName}}`; use `{{replacement}}` instead","replacement":"someDesc"}}}}
3748+
// Message: Missing JSDoc @someDesc description.
3749+
3750+
/**
3751+
* @description
3752+
*/
3753+
function quux () {
3754+
3755+
}
3756+
// Settings: {"jsdoc":{"tagNamePreference":{"description":false}}}
3757+
// Message: Unexpected tag `@description`
36343758
````
36353759

36363760
The following patterns are not considered problems:
@@ -3905,6 +4029,15 @@ function quux () {
39054029
}
39064030
// Options: ["always"]
39074031
// Message: There must be a hyphen before @param description.
4032+
4033+
/**
4034+
* @param foo
4035+
*/
4036+
function quux (foo) {
4037+
4038+
}
4039+
// Settings: {"jsdoc":{"tagNamePreference":{"param":false}}}
4040+
// Message: Unexpected tag `@param`
39084041
````
39094042

39104043
The following patterns are not considered problems:
@@ -4820,6 +4953,15 @@ function quux (foo) {
48204953
}
48214954
// Settings: {"jsdoc":{"tagNamePreference":{"param":"arg"}}}
48224955
// Message: Missing JSDoc @arg "foo" description.
4956+
4957+
/**
4958+
* @param foo
4959+
*/
4960+
function quux (foo) {
4961+
4962+
}
4963+
// Settings: {"jsdoc":{"tagNamePreference":{"param":false}}}
4964+
// Message: Unexpected tag `@param`
48234965
````
48244966

48254967
The following patterns are not considered problems:
@@ -4874,6 +5016,15 @@ function quux (foo) {
48745016

48755017
}
48765018
// Message: There must be an identifier after @param tag.
5019+
5020+
/**
5021+
* @param foo
5022+
*/
5023+
function quux (foo) {
5024+
5025+
}
5026+
// Settings: {"jsdoc":{"tagNamePreference":{"param":false}}}
5027+
// Message: Unexpected tag `@param`
48775028
````
48785029

48795030
The following patterns are not considered problems:
@@ -4925,6 +5076,15 @@ function quux (foo) {
49255076
}
49265077
// Settings: {"jsdoc":{"tagNamePreference":{"param":"arg"}}}
49275078
// Message: Missing JSDoc @arg "foo" type.
5079+
5080+
/**
5081+
* @param foo
5082+
*/
5083+
function quux (foo) {
5084+
5085+
}
5086+
// Settings: {"jsdoc":{"tagNamePreference":{"param":false}}}
5087+
// Message: Unexpected tag `@param`
49285088
````
49295089

49305090
The following patterns are not considered problems:
@@ -5090,6 +5250,15 @@ export class SomeClass {
50905250
constructor(private property: string, private foo: number) {}
50915251
}
50925252
// Message: Missing JSDoc @param "foo" declaration.
5253+
5254+
/**
5255+
*
5256+
*/
5257+
function quux (foo) {
5258+
5259+
}
5260+
// Settings: {"jsdoc":{"tagNamePreference":{"param":false}}}
5261+
// Message: Unexpected tag `@param`
50935262
````
50945263

50955264
The following patterns are not considered problems:
@@ -5471,6 +5640,15 @@ class Foo {
54715640
}
54725641
}
54735642
// Message: JSDoc @returns declaration present but return expression not available in function.
5643+
5644+
/**
5645+
* @returns
5646+
*/
5647+
function quux () {
5648+
5649+
}
5650+
// Settings: {"jsdoc":{"tagNamePreference":{"returns":false}}}
5651+
// Message: Unexpected tag `@returns`
54745652
````
54755653

54765654
The following patterns are not considered problems:
@@ -5750,6 +5928,15 @@ function quux (foo) {
57505928
}
57515929
// Settings: {"jsdoc":{"tagNamePreference":{"returns":"return"}}}
57525930
// Message: Missing JSDoc @return description.
5931+
5932+
/**
5933+
* @returns
5934+
*/
5935+
function quux () {
5936+
5937+
}
5938+
// Settings: {"jsdoc":{"tagNamePreference":{"returns":false}}}
5939+
// Message: Unexpected tag `@returns`
57535940
````
57545941

57555942
The following patterns are not considered problems:
@@ -5823,6 +6010,15 @@ function quux () {
58236010
}
58246011
// Settings: {"jsdoc":{"tagNamePreference":{"returns":"return"}}}
58256012
// Message: Missing JSDoc @return type.
6013+
6014+
/**
6015+
* @returns
6016+
*/
6017+
function quux () {
6018+
6019+
}
6020+
// Settings: {"jsdoc":{"tagNamePreference":{"returns":false}}}
6021+
// Message: Unexpected tag `@returns`
58266022
````
58276023

58286024
The following patterns are not considered problems:
@@ -5967,6 +6163,15 @@ function quux (foo) {
59676163
return foo;
59686164
}
59696165
// Message: Found more than one @returns declaration.
6166+
6167+
/**
6168+
* @returns
6169+
*/
6170+
function quux () {
6171+
6172+
}
6173+
// Settings: {"jsdoc":{"tagNamePreference":{"returns":false}}}
6174+
// Message: Unexpected tag `@returns`
59706175
````
59716176

59726177
The following patterns are not considered problems:

0 commit comments

Comments
 (0)