Skip to content

Commit 609d57b

Browse files
authored
Allow rejecting normally valid tag names and/or providing custom error messages when suggesting tags to reject or replace (#302)
* feat(check-tag-names): allow rejecting normally valid tag names and/or providing custom error messages when suggesting tags to reject or replace (fixes #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. Also, ensure `require-param-name` and `check-examples` report preferred tag name (not necessarily `param` or `example`, respectively) and err if `example` is a rejected tag
1 parent 2a153d2 commit 609d57b

29 files changed

+716
-39
lines changed

.README/README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,59 @@ 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`.
151+
152+
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`.
153+
154+
```json
155+
{
156+
"rules": {},
157+
"settings": {
158+
"jsdoc": {
159+
"tagNamePreference": {
160+
"augments": {
161+
"message": "@extends is to be used over @augments as it is more evocative of classes than @augments",
162+
"replacement": "extends"
163+
}
164+
}
165+
}
166+
}
167+
}
168+
```
169+
170+
If one wishes to reject a normally valid tag, e.g., `@todo`, one may set the tag to `false`:
171+
172+
```json
173+
{
174+
"rules": {},
175+
"settings": {
176+
"jsdoc": {
177+
"tagNamePreference": {
178+
"todo": false
179+
}
180+
}
181+
}
182+
}
183+
```
184+
185+
Or one may set the targeted tag to an object with a custom `message`, but without a `replacement` property:
186+
187+
```json
188+
{
189+
"rules": {},
190+
"settings": {
191+
"jsdoc": {
192+
"tagNamePreference": {
193+
"todo": {
194+
"message": "We expect immediate perfection, so don't leave to-dos in your code."
195+
}
196+
}
197+
}
198+
}
199+
}
200+
```
201+
202+
150203
The defaults in `eslint-plugin-jsdoc` (for tags which offer
151204
aliases) are as follows:
152205

README.md

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,59 @@ 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`.
201+
202+
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`.
203+
204+
```json
205+
{
206+
"rules": {},
207+
"settings": {
208+
"jsdoc": {
209+
"tagNamePreference": {
210+
"augments": {
211+
"message": "@extends is to be used over @augments as it is more evocative of classes than @augments",
212+
"replacement": "extends"
213+
}
214+
}
215+
}
216+
}
217+
}
218+
```
219+
220+
If one wishes to reject a normally valid tag, e.g., `@todo`, one may set the tag to `false`:
221+
222+
```json
223+
{
224+
"rules": {},
225+
"settings": {
226+
"jsdoc": {
227+
"tagNamePreference": {
228+
"todo": false
229+
}
230+
}
231+
}
232+
}
233+
```
234+
235+
Or one may set the targeted tag to an object with a custom `message`, but without a `replacement` property:
236+
237+
```json
238+
{
239+
"rules": {},
240+
"settings": {
241+
"jsdoc": {
242+
"tagNamePreference": {
243+
"todo": {
244+
"message": "We expect immediate perfection, so don't leave to-dos in your code."
245+
}
246+
}
247+
}
248+
}
249+
}
250+
```
251+
252+
200253
The defaults in `eslint-plugin-jsdoc` (for tags which offer
201254
aliases) are as follows:
202255

@@ -956,6 +1009,15 @@ export class SomeClass {
9561009
constructor(private property: string) {}
9571010
}
9581011
// 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`
9591021
````
9601022

9611023
The following patterns are not considered problems:
@@ -1277,6 +1339,42 @@ function quux (foo) {
12771339
}
12781340
// Settings: {"jsdoc":{"additionalTagNames":{"customTags":["bar"]}}}
12791341
// 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 x-todo instead of todo","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 x-todo instead of todo","replacement":"x-todo"}}}}
1377+
// Message: Please use x-todo instead of todo
12801378
````
12811379

12821380
The following patterns are not considered problems:
@@ -1392,6 +1490,13 @@ function quux (foo) {}
13921490
*/
13931491
function quux (foo) {
13941492

1493+
}
1494+
1495+
/**
1496+
* @todo
1497+
*/
1498+
function quux () {
1499+
13951500
}
13961501
````
13971502

@@ -3656,6 +3761,24 @@ var quux = {
36563761
};
36573762
// Options: [{"contexts":["ObjectExpression"]}]
36583763
// Message: Missing JSDoc @description declaration.
3764+
3765+
/**
3766+
* @someDesc
3767+
*/
3768+
function quux () {
3769+
3770+
}
3771+
// Settings: {"jsdoc":{"tagNamePreference":{"description":{"message":"Please avoid `{{tagName}}`; use `{{replacement}}` instead","replacement":"someDesc"}}}}
3772+
// Message: Missing JSDoc @someDesc description.
3773+
3774+
/**
3775+
* @description
3776+
*/
3777+
function quux () {
3778+
3779+
}
3780+
// Settings: {"jsdoc":{"tagNamePreference":{"description":false}}}
3781+
// Message: Unexpected tag `@description`
36593782
````
36603783

36613784
The following patterns are not considered problems:
@@ -3934,6 +4057,15 @@ function quux () {
39344057
}
39354058
// Options: ["always"]
39364059
// Message: There must be a hyphen before @param description.
4060+
4061+
/**
4062+
* @param foo
4063+
*/
4064+
function quux (foo) {
4065+
4066+
}
4067+
// Settings: {"jsdoc":{"tagNamePreference":{"param":false}}}
4068+
// Message: Unexpected tag `@param`
39374069
````
39384070

39394071
The following patterns are not considered problems:
@@ -4848,6 +4980,15 @@ function quux (foo) {
48484980
}
48494981
// Settings: {"jsdoc":{"tagNamePreference":{"param":"arg"}}}
48504982
// Message: Missing JSDoc @arg "foo" description.
4983+
4984+
/**
4985+
* @param foo
4986+
*/
4987+
function quux (foo) {
4988+
4989+
}
4990+
// Settings: {"jsdoc":{"tagNamePreference":{"param":false}}}
4991+
// Message: Unexpected tag `@param`
48514992
````
48524993

48534994
The following patterns are not considered problems:
@@ -4902,6 +5043,15 @@ function quux (foo) {
49025043

49035044
}
49045045
// Message: There must be an identifier after @param tag.
5046+
5047+
/**
5048+
* @param foo
5049+
*/
5050+
function quux (foo) {
5051+
5052+
}
5053+
// Settings: {"jsdoc":{"tagNamePreference":{"param":false}}}
5054+
// Message: Unexpected tag `@param`
49055055
````
49065056

49075057
The following patterns are not considered problems:
@@ -4953,6 +5103,15 @@ function quux (foo) {
49535103
}
49545104
// Settings: {"jsdoc":{"tagNamePreference":{"param":"arg"}}}
49555105
// Message: Missing JSDoc @arg "foo" type.
5106+
5107+
/**
5108+
* @param foo
5109+
*/
5110+
function quux (foo) {
5111+
5112+
}
5113+
// Settings: {"jsdoc":{"tagNamePreference":{"param":false}}}
5114+
// Message: Unexpected tag `@param`
49565115
````
49575116

49585117
The following patterns are not considered problems:
@@ -5118,6 +5277,15 @@ export class SomeClass {
51185277
constructor(private property: string, private foo: number) {}
51195278
}
51205279
// Message: Missing JSDoc @param "foo" declaration.
5280+
5281+
/**
5282+
*
5283+
*/
5284+
function quux (foo) {
5285+
5286+
}
5287+
// Settings: {"jsdoc":{"tagNamePreference":{"param":false}}}
5288+
// Message: Unexpected tag `@param`
51215289
````
51225290

51235291
The following patterns are not considered problems:
@@ -5499,6 +5667,15 @@ class Foo {
54995667
}
55005668
}
55015669
// Message: JSDoc @returns declaration present but return expression not available in function.
5670+
5671+
/**
5672+
* @returns
5673+
*/
5674+
function quux () {
5675+
5676+
}
5677+
// Settings: {"jsdoc":{"tagNamePreference":{"returns":false}}}
5678+
// Message: Unexpected tag `@returns`
55025679
````
55035680

55045681
The following patterns are not considered problems:
@@ -5778,6 +5955,15 @@ function quux (foo) {
57785955
}
57795956
// Settings: {"jsdoc":{"tagNamePreference":{"returns":"return"}}}
57805957
// Message: Missing JSDoc @return description.
5958+
5959+
/**
5960+
* @returns
5961+
*/
5962+
function quux () {
5963+
5964+
}
5965+
// Settings: {"jsdoc":{"tagNamePreference":{"returns":false}}}
5966+
// Message: Unexpected tag `@returns`
57815967
````
57825968

57835969
The following patterns are not considered problems:
@@ -5851,6 +6037,15 @@ function quux () {
58516037
}
58526038
// Settings: {"jsdoc":{"tagNamePreference":{"returns":"return"}}}
58536039
// Message: Missing JSDoc @return type.
6040+
6041+
/**
6042+
* @returns
6043+
*/
6044+
function quux () {
6045+
6046+
}
6047+
// Settings: {"jsdoc":{"tagNamePreference":{"returns":false}}}
6048+
// Message: Unexpected tag `@returns`
58546049
````
58556050

58566051
The following patterns are not considered problems:
@@ -5995,6 +6190,15 @@ function quux (foo) {
59956190
return foo;
59966191
}
59976192
// Message: Found more than one @returns declaration.
6193+
6194+
/**
6195+
* @returns
6196+
*/
6197+
function quux () {
6198+
6199+
}
6200+
// Settings: {"jsdoc":{"tagNamePreference":{"returns":false}}}
6201+
// Message: Unexpected tag `@returns`
59986202
````
59996203

60006204
The following patterns are not considered problems:

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"babel-plugin-add-module-exports": "^1.0.2",
2525
"babel-plugin-istanbul": "^5.1.4",
2626
"chai": "^4.2.0",
27+
"escape-regex-string": "^1.0.6",
2728
"eslint": "^6.0.1",
2829
"eslint-config-canonical": "^17.1.1",
2930
"gitdown": "^2.6.1",

0 commit comments

Comments
 (0)