Skip to content

Commit

Permalink
feat(check-param-names): add checkDestructured option to allow di…
Browse files Browse the repository at this point in the history
…sabling of destructured checking; fixes part of #616

Also improves display of errors when destructured properties are present
  • Loading branch information
brettz9 committed Jul 31, 2020
1 parent 24c5377 commit c4d1b80
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 5 deletions.
19 changes: 19 additions & 0 deletions README.md
Expand Up @@ -2103,6 +2103,16 @@ function quux() {
}
// Settings: {"jsdoc":{"structuredTags":{"see":{"name":false,"required":["name"]}}}}
// Message: Cannot add "name" to `require` with the tag's `name` set to `false`

/**
* @param root
* @param foo
*/
function quux ({foo, bar}, baz) {

}
// Options: [{"checkDestructured":false}]
// Message: Expected @param names to be "root, baz". Got "root, foo".
````

The following patterns are not considered problems:
Expand Down Expand Up @@ -2359,6 +2369,15 @@ export class Thing {
this.foo = new C();
}
}

/**
* @param foo
* @param root
*/
function quux (foo, {bar}) {

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


Expand Down
26 changes: 21 additions & 5 deletions src/rules/checkParamNames.js
Expand Up @@ -3,6 +3,7 @@ import iterateJsdoc from '../iterateJsdoc';
const validateParameterNames = (
targetTagName : string,
allowExtraTrailingParamDocs: boolean,
checkDestructured : boolean,
checkRestProperty : boolean,
checkTypesRegex : RegExp,
enableFixer: boolean,
Expand Down Expand Up @@ -54,6 +55,9 @@ const validateParameterNames = (
}

if (Array.isArray(functionParameterName)) {
if (!checkDestructured) {
return false;
}
if (tag.type && tag.type.search(checkTypesRegex) === -1) {
return false;
}
Expand Down Expand Up @@ -123,14 +127,20 @@ const validateParameterNames = (
}

if (funcParamName !== tag.name.trim()) {
// Todo: This won't work for array or object child items
const expectedNames = functionParameterNames.join(', ');
// Todo: Improve for array or object child items
const actualNames = paramTagsNonNested.map(([, {name}]) => {
return name.trim();
});
const expectedNames = functionParameterNames.map((item, idx) => {
if (item?.[1]?.names) {
return actualNames[idx];
}

return item;
}).join(', ');

report(
`Expected @${targetTagName} names to be "${expectedNames}". Got "${actualNames}".`,
`Expected @${targetTagName} names to be "${expectedNames}". Got "${actualNames.join(', ')}".`,
null,
tag,
);
Expand Down Expand Up @@ -191,6 +201,7 @@ export default iterateJsdoc(({
}) => {
const {
allowExtraTrailingParamDocs,
checkDestructured = true,
checkRestProperty = false,
checkTypesPattern = '/^(?:[oO]bject|[aA]rray|PlainObject|Generic(?:Object|Array))$/',
enableFixer = false,
Expand All @@ -210,19 +221,21 @@ export default iterateJsdoc(({
const isError = validateParameterNames(
targetTagName,
allowExtraTrailingParamDocs,
checkDestructured,
checkRestProperty,
checkTypesRegex,
enableFixer,
functionParameterNames,
jsdoc, jsdocNode, utils, report,
);

if (isError) {
if (isError || !checkDestructured) {
return;
}

validateParameterNamesDeep(
targetTagName, allowExtraTrailingParamDocs, jsdocParameterNamesDeep,
targetTagName, allowExtraTrailingParamDocs,
jsdocParameterNamesDeep,
jsdoc, report,
);
}, {
Expand All @@ -238,6 +251,9 @@ export default iterateJsdoc(({
allowExtraTrailingParamDocs: {
type: 'boolean',
},
checkDestructured: {
type: 'boolean',
},
checkRestProperty: {
type: 'boolean',
},
Expand Down
32 changes: 32 additions & 0 deletions test/rules/assertions/checkParamNames.js
Expand Up @@ -876,6 +876,24 @@ export default {
},
},
},
{
code: `
/**
* @param root
* @param foo
*/
function quux ({foo, bar}, baz) {
}
`,
errors: [{
line: 4,
message: 'Expected @param names to be "root, baz". Got "root, foo".',
}],
options: [{
checkDestructured: false,
}],
},
],
valid: [
{
Expand Down Expand Up @@ -1247,5 +1265,19 @@ export default {
`,
parser: require.resolve('@typescript-eslint/parser'),
},
{
code: `
/**
* @param foo
* @param root
*/
function quux (foo, {bar}) {
}
`,
options: [{
checkDestructured: false,
}],
},
],
};

0 comments on commit c4d1b80

Please sign in to comment.