Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions src/rules/requireParam.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import iterateJsdoc from '../iterateJsdoc';

export default iterateJsdoc(({
report,
jsdoc,
utils,
}) => {
const functionParameterNames = utils.getFunctionParameterNames();
Expand All @@ -18,17 +18,42 @@ export default iterateJsdoc(({
if (utils.hasTag('type')) {
return;
}
const findExpectedIndex = (jsdocTags, funcParmOrder, tagName) => {
const docTags = jsdocTags.filter(({tag}) => {
return tag === tagName;
});
let expectedIndex = jsdocTags.length;
jsdocTags.forEach((tag, idx) => {
if (tag.tag === tagName) {
expectedIndex = docTags.indexOf(tag) < funcParmOrder ? idx + 1 : idx - 1;
}
});

functionParameterNames.forEach((functionParameterName) => {
return expectedIndex;
};

functionParameterNames.forEach((functionParameterName, functionParameterIdx) => {
if (['<ObjectPattern>', '<ArrayPattern>'].includes(functionParameterName)) {
return;
}
if (!jsdocParameterNames.includes(functionParameterName)) {
report(`Missing JSDoc @${utils.getPreferredTagName({tagName: 'param'})} "${functionParameterName}" declaration.`);
const preferredTagName = utils.getPreferredTagName({tagName: 'param'});
utils.reportJSDoc(`Missing JSDoc @${preferredTagName} "${functionParameterName}" declaration.`, null, () => {
if (!jsdoc.tags) {
jsdoc.tags = [];
}

const expectedIdx = findExpectedIndex(jsdoc.tags, functionParameterIdx, preferredTagName);
jsdoc.tags.splice(expectedIdx, 0, {
name: functionParameterName,
tag: preferredTagName,
});
});
}
});
}, {
meta: {
fixable: true,
schema: [
{
additionalProperties: false,
Expand Down
93 changes: 93 additions & 0 deletions test/rules/assertions/requireParam.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ export default {
message: 'Missing JSDoc @param "foo" declaration.',
},
],
output: `
/**
* @param foo
*/
function quux (foo) {

}
`,
},
{
code: `
Expand All @@ -38,6 +46,38 @@ export default {
message: 'Missing JSDoc @param "bar" declaration.',
},
],
output: `
/**
* @param foo
*/
function quux (foo, bar) {

}
`,
},
{
code: `
/**
* @param foo
*/
function quux (foo, bar) {

}
`,
errors: [
{
message: 'Missing JSDoc @param "bar" declaration.',
},
],
output: `
/**
* @param foo
* @param bar
*/
function quux (foo, bar) {

}
`,
},
{
code: `
Expand All @@ -56,6 +96,41 @@ export default {
message: 'Missing JSDoc @param "baz" declaration.',
},
],
output: `
/**
* @param foo
* @param bar
*/
function quux (foo, bar, baz) {

}
`,
},
{
code: `
/**
* @param foo
* @param bar
*/
function quux (foo, bar, baz) {

}
`,
errors: [
{
message: 'Missing JSDoc @param "baz" declaration.',
},
],
output: `
/**
* @param foo
* @param bar
* @param baz
*/
function quux (foo, bar, baz) {

}
`,
},
{
code: `
Expand All @@ -74,6 +149,15 @@ export default {
message: 'Missing JSDoc @param "bar" declaration.',
},
],
output: `
/**
* @param foo
* @param baz
*/
function quux (foo, bar, baz) {

}
`,
},
{
code: `
Expand Down Expand Up @@ -126,6 +210,15 @@ export default {
message: 'Missing JSDoc @param "foo" declaration.',
},
],
output: `
/**
* @override
* @param foo
*/
function quux (foo) {

}
`,
settings: {
jsdoc: {
overrideReplacesDocs: false,
Expand Down