Skip to content

Commit 3d5ee0c

Browse files
committedMay 12, 2020
feat(check-param-names, check-property-names): add enableFixer option
1 parent d330391 commit 3d5ee0c

File tree

7 files changed

+149
-6
lines changed

7 files changed

+149
-6
lines changed
 

‎.README/rules/check-param-names.md

+9
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ See the "Destructuring" section. Defaults to `false`.
3737

3838
See `require-param` under the option of the same name.
3939

40+
##### `enableFixer`
41+
42+
Set to `false` to avoid auto-removing `@param`'s duplicates (based on
43+
identical names).
44+
45+
Note that, by default, duplicates of the same name are removed even if
46+
the definitions do not match in other ways (e.g., the second param will
47+
be removed even if it has a different type or description).
48+
4049
##### `allowExtraTrailingParamDocs`
4150

4251
If set to `true`, this option will allow extra `@param` definitions (e.g.,

‎.README/rules/check-property-names.md

+10
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,19 @@ and that nested properties have defined roots.
55

66
#### Options
77

8+
##### `enableFixer`
9+
10+
Set to `false` to avoid auto-removing `@property`'s duplicates (based on
11+
identical names).
12+
13+
Note that, by default, duplicates of the same name are removed even if
14+
the definitions do not match in other ways (e.g., the second property will
15+
be removed even if it has a different type or description).
16+
817
|||
918
|---|---|
1019
|Context|Everywhere|
20+
|Options|`enableFixer`|
1121
|Tags|`property`|
1222

1323
<!-- assertions checkPropertyNames -->

‎README.md

+41-1
Original file line numberDiff line numberDiff line change
@@ -1584,6 +1584,16 @@ See the "Destructuring" section. Defaults to `false`.
15841584

15851585
See `require-param` under the option of the same name.
15861586

1587+
<a name="eslint-plugin-jsdoc-rules-check-param-names-options-3-enablefixer"></a>
1588+
##### <code>enableFixer</code>
1589+
1590+
Set to `false` to avoid auto-removing `@param`'s duplicates (based on
1591+
identical names).
1592+
1593+
Note that, by default, duplicates of the same name are removed even if
1594+
the definitions do not match in other ways (e.g., the second param will
1595+
be removed even if it has a different type or description).
1596+
15871597
<a name="eslint-plugin-jsdoc-rules-check-param-names-options-3-allowextratrailingparamdocs"></a>
15881598
##### <code>allowExtraTrailingParamDocs</code>
15891599

@@ -1719,6 +1729,17 @@ function quux ({foo}) {
17191729
}
17201730
// Message: Duplicate @param "cfg.foo"
17211731

1732+
/**
1733+
* @param cfg
1734+
* @param cfg.foo
1735+
* @param cfg.foo
1736+
*/
1737+
function quux ({foo}) {
1738+
1739+
}
1740+
// Options: [{"enableFixer":false}]
1741+
// Message: Duplicate @param "cfg.foo"
1742+
17221743
/**
17231744
* @param cfg
17241745
* @param cfg.foo
@@ -2088,9 +2109,20 @@ and that nested properties have defined roots.
20882109
<a name="eslint-plugin-jsdoc-rules-check-property-names-options-4"></a>
20892110
#### Options
20902111

2112+
<a name="eslint-plugin-jsdoc-rules-check-property-names-options-4-enablefixer-1"></a>
2113+
##### <code>enableFixer</code>
2114+
2115+
Set to `false` to avoid auto-removing `@property`'s duplicates (based on
2116+
identical names).
2117+
2118+
Note that, by default, duplicates of the same name are removed even if
2119+
the definitions do not match in other ways (e.g., the second property will
2120+
be removed even if it has a different type or description).
2121+
20912122
|||
20922123
|---|---|
20932124
|Context|Everywhere|
2125+
|Options|`enableFixer`|
20942126
|Tags|`property`|
20952127

20962128
The following patterns are considered problems:
@@ -2132,6 +2164,14 @@ The following patterns are considered problems:
21322164
*/
21332165
// Message: Duplicate @property "foo"
21342166

2167+
/**
2168+
* @typedef (SomeType) SomeTypedef
2169+
* @property foo
2170+
* @property foo
2171+
*/
2172+
// Options: [{"enableFixer":false}]
2173+
// Message: Duplicate @property "foo"
2174+
21352175
/**
21362176
* @typedef (SomeType) SomeTypedef
21372177
* @property cfg
@@ -9485,7 +9525,7 @@ function signature, it may appear that there is an actual property named
94859525
94869526
An options object accepts the following optional properties:
94879527
9488-
<a name="eslint-plugin-jsdoc-rules-require-param-options-23-enablefixer"></a>
9528+
<a name="eslint-plugin-jsdoc-rules-require-param-options-23-enablefixer-2"></a>
94899529
##### <code>enableFixer</code>
94909530
94919531
Whether to enable the fixer. Defaults to `true`.

‎src/rules/checkParamNames.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const validateParameterNames = (
55
allowExtraTrailingParamDocs: boolean,
66
checkRestProperty : boolean,
77
checkTypesRegex : RegExp,
8+
enableFixer: boolean,
89
functionParameterNames : Array<string>, jsdoc, jsdocNode, utils, report,
910
) => {
1011
const paramTags = Object.entries(jsdoc.tags).filter(([, tag]) => {
@@ -24,9 +25,9 @@ const validateParameterNames = (
2425
return tg.name === tag.name && idx !== index;
2526
});
2627
if (dupeTagInfo) {
27-
utils.reportJSDoc(`Duplicate @${targetTagName} "${tag.name}"`, dupeTagInfo[1], () => {
28+
utils.reportJSDoc(`Duplicate @${targetTagName} "${tag.name}"`, dupeTagInfo[1], enableFixer ? () => {
2829
jsdoc.tags.splice(tagsIndex, 1);
29-
});
30+
} : null);
3031

3132
return true;
3233
}
@@ -187,6 +188,7 @@ export default iterateJsdoc(({
187188
allowExtraTrailingParamDocs,
188189
checkRestProperty = false,
189190
checkTypesPattern = '/^(?:[oO]bject|[aA]rray|PlainObject|Generic(?:Object|Array))$/',
191+
enableFixer = true,
190192
} = context.options[0] || {};
191193

192194
const lastSlashPos = checkTypesPattern.lastIndexOf('/');
@@ -202,8 +204,10 @@ export default iterateJsdoc(({
202204
const targetTagName = utils.getPreferredTagName({tagName: 'param'});
203205
const isError = validateParameterNames(
204206
targetTagName,
205-
allowExtraTrailingParamDocs, checkRestProperty,
207+
allowExtraTrailingParamDocs,
208+
checkRestProperty,
206209
checkTypesRegex,
210+
enableFixer,
207211
functionParameterNames,
208212
jsdoc, jsdocNode, utils, report,
209213
);
@@ -232,6 +236,9 @@ export default iterateJsdoc(({
232236
checkTypesPattern: {
233237
type: 'string',
234238
},
239+
enableFixer: {
240+
type: 'boolean',
241+
},
235242
},
236243
type: 'object',
237244
},

‎src/rules/checkPropertyNames.js

+19-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import iterateJsdoc from '../iterateJsdoc';
22

33
const validatePropertyNames = (
44
targetTagName : string,
5+
enableFixer : boolean,
56
jsdoc, jsdocNode, utils,
67
) => {
78
const propertyTags = Object.entries(jsdoc.tags).filter(([, tag]) => {
@@ -16,9 +17,9 @@ const validatePropertyNames = (
1617
return tg.name === tag.name && idx !== index;
1718
});
1819
if (dupeTagInfo) {
19-
utils.reportJSDoc(`Duplicate @${targetTagName} "${tag.name}"`, dupeTagInfo[1], () => {
20+
utils.reportJSDoc(`Duplicate @${targetTagName} "${tag.name}"`, dupeTagInfo[1], enableFixer ? () => {
2021
jsdoc.tags.splice(tagsIndex, 1);
21-
});
22+
} : null);
2223

2324
return true;
2425
}
@@ -68,18 +69,23 @@ const validatePropertyNamesDeep = (
6869
};
6970

7071
export default iterateJsdoc(({
72+
context,
7173
jsdoc,
7274
jsdocNode,
7375
report,
7476
utils,
7577
}) => {
78+
const {
79+
enableFixer = true,
80+
} = context.options[0] || {};
7681
const jsdocPropertyNamesDeep = utils.getJsdocTagsDeep('property');
7782
if (!jsdocPropertyNamesDeep.length) {
7883
return;
7984
}
8085
const targetTagName = utils.getPreferredTagName({tagName: 'property'});
8186
const isError = validatePropertyNames(
8287
targetTagName,
88+
enableFixer,
8389
jsdoc, jsdocNode, utils,
8490
);
8591

@@ -95,6 +101,17 @@ export default iterateJsdoc(({
95101
iterateAllJsdocs: true,
96102
meta: {
97103
fixable: 'code',
104+
schema: [
105+
{
106+
additionalProperties: false,
107+
properties: {
108+
enableFixer: {
109+
type: 'boolean',
110+
},
111+
},
112+
type: 'object',
113+
},
114+
],
98115
type: 'suggestion',
99116
},
100117
});

‎test/rules/assertions/checkParamNames.js

+33
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,39 @@ export default {
276276
}
277277
`,
278278
},
279+
{
280+
code: `
281+
/**
282+
* @param cfg
283+
* @param cfg.foo
284+
* @param cfg.foo
285+
*/
286+
function quux ({foo}) {
287+
288+
}
289+
`,
290+
errors: [
291+
{
292+
line: 5,
293+
message: 'Duplicate @param "cfg.foo"',
294+
},
295+
],
296+
options: [
297+
{
298+
enableFixer: false,
299+
},
300+
],
301+
output: `
302+
/**
303+
* @param cfg
304+
* @param cfg.foo
305+
* @param cfg.foo
306+
*/
307+
function quux ({foo}) {
308+
309+
}
310+
`,
311+
},
279312
{
280313
code: `
281314
/**

‎test/rules/assertions/checkPropertyNames.js

+27
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,33 @@ export default {
9595
*/
9696
`,
9797
},
98+
{
99+
code: `
100+
/**
101+
* @typedef (SomeType) SomeTypedef
102+
* @property foo
103+
* @property foo
104+
*/
105+
`,
106+
errors: [
107+
{
108+
line: 5,
109+
message: 'Duplicate @property "foo"',
110+
},
111+
],
112+
options: [
113+
{
114+
enableFixer: false,
115+
},
116+
],
117+
output: `
118+
/**
119+
* @typedef (SomeType) SomeTypedef
120+
* @property foo
121+
* @property foo
122+
*/
123+
`,
124+
},
98125
{
99126
code: `
100127
/**

0 commit comments

Comments
 (0)
Please sign in to comment.