Skip to content

Commit fc4d68e

Browse files
yeonjuanbrettz9
authored andcommitted
feat(require-param): change fixer to fix all missing tags at once
1 parent f174401 commit fc4d68e

File tree

2 files changed

+43
-17
lines changed

2 files changed

+43
-17
lines changed

src/rules/requireParam.js

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,39 +18,62 @@ 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;
21+
22+
const preferredTagName = utils.getPreferredTagName({tagName: 'param'});
23+
24+
const findExpectedIndex = (jsdocTags, indexAtFunctionParams) => {
25+
const functionTags = jsdocTags.filter(({tag}) => {
26+
return tag === preferredTagName;
2427
});
2528
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+
jsdocTags.forEach((tag, index) => {
30+
if (tag.tag === preferredTagName) {
31+
expectedIndex = index;
32+
if (functionTags.indexOf(tag) < indexAtFunctionParams) {
33+
expectedIndex += 1;
34+
}
2935
}
3036
});
3137

3238
return expectedIndex;
3339
};
3440

41+
const missingTags = [];
42+
3543
functionParameterNames.forEach((functionParameterName, functionParameterIdx) => {
3644
if (['<ObjectPattern>', '<ArrayPattern>'].includes(functionParameterName)) {
3745
return;
3846
}
3947
if (!jsdocParameterNames.includes(functionParameterName)) {
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-
});
48+
missingTags.push({
49+
functionParameterIdx,
50+
functionParameterName,
5151
});
5252
}
5353
});
54+
55+
const fixAll = (missings, tags) => {
56+
missings.forEach(({functionParameterIdx, functionParameterName}) => {
57+
const expectedIdx = findExpectedIndex(tags, functionParameterIdx);
58+
tags.splice(expectedIdx, 0, {
59+
name: functionParameterName,
60+
tag: preferredTagName,
61+
});
62+
});
63+
};
64+
65+
missingTags.forEach(({functionParameterName}, index) => {
66+
utils.reportJSDoc(`Missing JSDoc @${preferredTagName} "${functionParameterName}" declaration.`, null, () => {
67+
if (!jsdoc.tags) {
68+
jsdoc.tags = [];
69+
}
70+
71+
// Fix all missing tags at the first time.
72+
if (index === 0) {
73+
fixAll(missingTags, jsdoc.tags);
74+
}
75+
});
76+
});
5477
}, {
5578
meta: {
5679
fixable: true,

test/rules/assertions/requireParam.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export default {
4949
output: `
5050
/**
5151
* @param foo
52+
* @param bar
5253
*/
5354
function quux (foo, bar) {
5455
@@ -100,6 +101,7 @@ export default {
100101
/**
101102
* @param foo
102103
* @param bar
104+
* @param baz
103105
*/
104106
function quux (foo, bar, baz) {
105107
@@ -152,6 +154,7 @@ export default {
152154
output: `
153155
/**
154156
* @param foo
157+
* @param bar
155158
* @param baz
156159
*/
157160
function quux (foo, bar, baz) {

0 commit comments

Comments
 (0)