Skip to content

Commit

Permalink
feat(require-param): change fixer to fix all missing tags at once
Browse files Browse the repository at this point in the history
  • Loading branch information
yeonjuan authored and brettz9 committed Oct 18, 2019
1 parent f174401 commit fc4d68e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 17 deletions.
57 changes: 40 additions & 17 deletions src/rules/requireParam.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,62 @@ export default iterateJsdoc(({
if (utils.hasTag('type')) {
return;
}
const findExpectedIndex = (jsdocTags, funcParmOrder, tagName) => {
const docTags = jsdocTags.filter(({tag}) => {
return tag === tagName;

const preferredTagName = utils.getPreferredTagName({tagName: 'param'});

const findExpectedIndex = (jsdocTags, indexAtFunctionParams) => {
const functionTags = jsdocTags.filter(({tag}) => {
return tag === preferredTagName;
});
let expectedIndex = jsdocTags.length;
jsdocTags.forEach((tag, idx) => {
if (tag.tag === tagName) {
expectedIndex = docTags.indexOf(tag) < funcParmOrder ? idx + 1 : idx - 1;
jsdocTags.forEach((tag, index) => {
if (tag.tag === preferredTagName) {
expectedIndex = index;
if (functionTags.indexOf(tag) < indexAtFunctionParams) {
expectedIndex += 1;
}
}
});

return expectedIndex;
};

const missingTags = [];

functionParameterNames.forEach((functionParameterName, functionParameterIdx) => {
if (['<ObjectPattern>', '<ArrayPattern>'].includes(functionParameterName)) {
return;
}
if (!jsdocParameterNames.includes(functionParameterName)) {
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,
});
missingTags.push({
functionParameterIdx,
functionParameterName,
});
}
});

const fixAll = (missings, tags) => {
missings.forEach(({functionParameterIdx, functionParameterName}) => {
const expectedIdx = findExpectedIndex(tags, functionParameterIdx);
tags.splice(expectedIdx, 0, {
name: functionParameterName,
tag: preferredTagName,
});
});
};

missingTags.forEach(({functionParameterName}, index) => {
utils.reportJSDoc(`Missing JSDoc @${preferredTagName} "${functionParameterName}" declaration.`, null, () => {
if (!jsdoc.tags) {
jsdoc.tags = [];
}

// Fix all missing tags at the first time.
if (index === 0) {
fixAll(missingTags, jsdoc.tags);
}
});
});
}, {
meta: {
fixable: true,
Expand Down
3 changes: 3 additions & 0 deletions test/rules/assertions/requireParam.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export default {
output: `
/**
* @param foo
* @param bar
*/
function quux (foo, bar) {
Expand Down Expand Up @@ -100,6 +101,7 @@ export default {
/**
* @param foo
* @param bar
* @param baz
*/
function quux (foo, bar, baz) {
Expand Down Expand Up @@ -152,6 +154,7 @@ export default {
output: `
/**
* @param foo
* @param bar
* @param baz
*/
function quux (foo, bar, baz) {
Expand Down

0 comments on commit fc4d68e

Please sign in to comment.