Skip to content

Commit f5aa476

Browse files
authored
feat: add require-param fixer
2 parents 32ffdb5 + 893916c commit f5aa476

File tree

2 files changed

+121
-3
lines changed

2 files changed

+121
-3
lines changed

src/rules/requireParam.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import iterateJsdoc from '../iterateJsdoc';
22

33
export default iterateJsdoc(({
4-
report,
4+
jsdoc,
55
utils,
66
}) => {
77
const functionParameterNames = utils.getFunctionParameterNames();
@@ -18,17 +18,42 @@ export default iterateJsdoc(({
1818
if (utils.hasTag('type')) {
1919
return;
2020
}
21+
const findExpectedIndex = (jsdocTags, funcParmOrder, tagName) => {
22+
const docTags = jsdocTags.filter(({tag}) => {
23+
return tag === tagName;
24+
});
25+
let expectedIndex = jsdocTags.length;
26+
jsdocTags.forEach((tag, idx) => {
27+
if (tag.tag === tagName) {
28+
expectedIndex = docTags.indexOf(tag) < funcParmOrder ? idx + 1 : idx - 1;
29+
}
30+
});
2131

22-
functionParameterNames.forEach((functionParameterName) => {
32+
return expectedIndex;
33+
};
34+
35+
functionParameterNames.forEach((functionParameterName, functionParameterIdx) => {
2336
if (['<ObjectPattern>', '<ArrayPattern>'].includes(functionParameterName)) {
2437
return;
2538
}
2639
if (!jsdocParameterNames.includes(functionParameterName)) {
27-
report(`Missing JSDoc @${utils.getPreferredTagName({tagName: 'param'})} "${functionParameterName}" declaration.`);
40+
const preferredTagName = utils.getPreferredTagName({tagName: 'param'});
41+
utils.reportJSDoc(`Missing JSDoc @${preferredTagName} "${functionParameterName}" declaration.`, null, () => {
42+
if (!jsdoc.tags) {
43+
jsdoc.tags = [];
44+
}
45+
46+
const expectedIdx = findExpectedIndex(jsdoc.tags, functionParameterIdx, preferredTagName);
47+
jsdoc.tags.splice(expectedIdx, 0, {
48+
name: functionParameterName,
49+
tag: preferredTagName,
50+
});
51+
});
2852
}
2953
});
3054
}, {
3155
meta: {
56+
fixable: true,
3257
schema: [
3358
{
3459
additionalProperties: false,

test/rules/assertions/requireParam.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ export default {
2020
message: 'Missing JSDoc @param "foo" declaration.',
2121
},
2222
],
23+
output: `
24+
/**
25+
* @param foo
26+
*/
27+
function quux (foo) {
28+
29+
}
30+
`,
2331
},
2432
{
2533
code: `
@@ -38,6 +46,38 @@ export default {
3846
message: 'Missing JSDoc @param "bar" declaration.',
3947
},
4048
],
49+
output: `
50+
/**
51+
* @param foo
52+
*/
53+
function quux (foo, bar) {
54+
55+
}
56+
`,
57+
},
58+
{
59+
code: `
60+
/**
61+
* @param foo
62+
*/
63+
function quux (foo, bar) {
64+
65+
}
66+
`,
67+
errors: [
68+
{
69+
message: 'Missing JSDoc @param "bar" declaration.',
70+
},
71+
],
72+
output: `
73+
/**
74+
* @param foo
75+
* @param bar
76+
*/
77+
function quux (foo, bar) {
78+
79+
}
80+
`,
4181
},
4282
{
4383
code: `
@@ -56,6 +96,41 @@ export default {
5696
message: 'Missing JSDoc @param "baz" declaration.',
5797
},
5898
],
99+
output: `
100+
/**
101+
* @param foo
102+
* @param bar
103+
*/
104+
function quux (foo, bar, baz) {
105+
106+
}
107+
`,
108+
},
109+
{
110+
code: `
111+
/**
112+
* @param foo
113+
* @param bar
114+
*/
115+
function quux (foo, bar, baz) {
116+
117+
}
118+
`,
119+
errors: [
120+
{
121+
message: 'Missing JSDoc @param "baz" declaration.',
122+
},
123+
],
124+
output: `
125+
/**
126+
* @param foo
127+
* @param bar
128+
* @param baz
129+
*/
130+
function quux (foo, bar, baz) {
131+
132+
}
133+
`,
59134
},
60135
{
61136
code: `
@@ -74,6 +149,15 @@ export default {
74149
message: 'Missing JSDoc @param "bar" declaration.',
75150
},
76151
],
152+
output: `
153+
/**
154+
* @param foo
155+
* @param baz
156+
*/
157+
function quux (foo, bar, baz) {
158+
159+
}
160+
`,
77161
},
78162
{
79163
code: `
@@ -126,6 +210,15 @@ export default {
126210
message: 'Missing JSDoc @param "foo" declaration.',
127211
},
128212
],
213+
output: `
214+
/**
215+
* @override
216+
* @param foo
217+
*/
218+
function quux (foo) {
219+
220+
}
221+
`,
129222
settings: {
130223
jsdoc: {
131224
overrideReplacesDocs: false,

0 commit comments

Comments
 (0)