Skip to content

Commit

Permalink
fix(require-param): Object destructuring in function parameters if …
Browse files Browse the repository at this point in the history
…key is string | number (quoted or otherwise)
  • Loading branch information
brettz9 committed Jul 9, 2020
1 parent 0c1c763 commit 1842fd2
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 7 deletions.
16 changes: 16 additions & 0 deletions README.md
Expand Up @@ -11537,6 +11537,22 @@ function quux (foo, bar, {baz}) {

}
// Options: [{"checkDestructured":false}]

/**
* @param root
* @param root.foo
*/
function quux ({"foo": bar}) {

}

/**
* @param root
* @param root."foo"
*/
function quux ({foo: bar}) {

}
````


Expand Down
4 changes: 4 additions & 0 deletions src/iterateJsdoc.js
Expand Up @@ -236,6 +236,10 @@ const getUtils = (
return jsdocUtils.comparePaths(name);
};

utils.dropPathSegmentQuotes = (name) => {
return jsdocUtils.dropPathSegmentQuotes(name);
};

utils.avoidDocs = () => {
if (
overrideReplacesDocs !== false &&
Expand Down
1 change: 1 addition & 0 deletions src/jsdocUtils.js
Expand Up @@ -828,6 +828,7 @@ const comparePaths = (name) => {

export default {
comparePaths,
dropPathSegmentQuotes,
enforcedContexts,
exemptSpeciaMethods,
filterTags,
Expand Down
25 changes: 18 additions & 7 deletions src/rules/requireParam.js
Expand Up @@ -67,8 +67,17 @@ export default iterateJsdoc(({
const flattenedRoots = utils.flattenRoots(functionParameterNames).names;

const paramIndex = {};
const hasParamIndex = (cur) => {
return _.has(paramIndex, utils.dropPathSegmentQuotes(String(cur)));
};
const getParamIndex = (cur) => {
return paramIndex[utils.dropPathSegmentQuotes(String(cur))];
};
const setParamIndex = (cur, idx) => {
paramIndex[utils.dropPathSegmentQuotes(String(cur))] = idx;
};
flattenedRoots.forEach((cur, idx) => {
paramIndex[cur] = idx;
setParamIndex(cur, idx);
});

const findExpectedIndex = (jsdocTags, indexAtFunctionParams) => {
Expand Down Expand Up @@ -145,9 +154,9 @@ export default iterateJsdoc(({
});
} else {
missingTags.push({
functionParameterIdx: _.has(paramIndex, rootName) ?
paramIndex[rootName] :
paramIndex[paramName],
functionParameterIdx: hasParamIndex(rootName) ?
getParamIndex(rootName) :
getParamIndex(paramName),
functionParameterName: rootName,
inc,
});
Expand All @@ -165,10 +174,12 @@ export default iterateJsdoc(({
const fullParamName = `${rootName}.${paramName}`;

if (jsdocParameterNames && !jsdocParameterNames.find(({name}) => {
return name === fullParamName;
return utils.comparePaths(name)(fullParamName);
})) {
missingTags.push({
functionParameterIdx: paramIndex[functionParameterName[0] ? fullParamName : paramName],
functionParameterIdx: getParamIndex(
functionParameterName[0] ? fullParamName : paramName,
),
functionParameterName: fullParamName,
inc,
type: hasRestElement && !hasPropertyRest ? '...any' : undefined,
Expand All @@ -194,7 +205,7 @@ export default iterateJsdoc(({
return name === funcParamName;
})) {
missingTags.push({
functionParameterIdx: paramIndex[funcParamName],
functionParameterIdx: getParamIndex(funcParamName),
functionParameterName: funcParamName,
inc,
type,
Expand Down
22 changes: 22 additions & 0 deletions test/rules/assertions/requireParam.js
Expand Up @@ -2746,5 +2746,27 @@ export default {
},
],
},
{
code: `
/**
* @param root
* @param root.foo
*/
function quux ({"foo": bar}) {
}
`,
},
{
code: `
/**
* @param root
* @param root."foo"
*/
function quux ({foo: bar}) {
}
`,
},
],
};

0 comments on commit 1842fd2

Please sign in to comment.