Skip to content

Commit

Permalink
fix(check-param-names): check duplicate nested names; fixes #474
Browse files Browse the repository at this point in the history
  • Loading branch information
brettz9 committed Jan 7, 2020
1 parent 3e8ea59 commit c51d616
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 3 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1631,6 +1631,38 @@ function quux (foo, foo) {
}
// Message: Duplicate @param "foo"

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

}
// Message: Duplicate @param "cfg.foo"

/**
* @param cfg
* @param cfg.foo
* @param [cfg.foo]
* @param baz
*/
function quux ({foo, bar}, baz) {

}
// Message: Duplicate @param "cfg.foo"

/**
* @param cfg
* @param cfg.foo
* @param [cfg.foo="with a default"]
* @param baz
*/
function quux ({foo, bar}, baz) {

}
// Message: Duplicate @param "cfg.foo"

export class SomeClass {
/**
* @param prop
Expand Down
17 changes: 14 additions & 3 deletions src/rules/checkParamNames.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ const validateParameterNames = (
functionParameterNames : Array<string>, jsdoc, jsdocNode, utils, report,
) => {
const paramTags = entries(jsdoc.tags).filter(([, tag]) => {
return tag.tag === targetTagName && !tag.name.includes('.');
return tag.tag === targetTagName;
});
const paramTagsNonNested = paramTags.filter(([, tag]) => {
return !tag.name.includes('.');
});

let dotted = 0;

return paramTags.some(([, tag], index) => {
let tagsIndex;
Expand All @@ -24,7 +29,13 @@ const validateParameterNames = (

return true;
}
const functionParameterName = functionParameterNames[index];
if (tag.name.includes('.')) {
dotted++;

return false;
}

const functionParameterName = functionParameterNames[index - dotted];

if (!functionParameterName) {
if (allowExtraTrailingParamDocs) {
Expand All @@ -46,7 +57,7 @@ const validateParameterNames = (

if (functionParameterName !== tag.name.trim()) {
const expectedNames = functionParameterNames.join(', ');
const actualNames = paramTags.map(([, {name}]) => {
const actualNames = paramTagsNonNested.map(([, {name}]) => {
return name.trim();
}).join(', ');

Expand Down
56 changes: 56 additions & 0 deletions test/rules/assertions/checkParamNames.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,62 @@ export default {
},
],
},
{
code: `
/**
* @param cfg
* @param cfg.foo
* @param cfg.foo
*/
function quux ({foo, bar}) {
}
`,
errors: [
{
line: 5,
message: 'Duplicate @param "cfg.foo"',
},
],
},
{
code: `
/**
* @param cfg
* @param cfg.foo
* @param [cfg.foo]
* @param baz
*/
function quux ({foo, bar}, baz) {
}
`,
errors: [
{
line: 5,
message: 'Duplicate @param "cfg.foo"',
},
],
},
{
code: `
/**
* @param cfg
* @param cfg.foo
* @param [cfg.foo="with a default"]
* @param baz
*/
function quux ({foo, bar}, baz) {
}
`,
errors: [
{
line: 5,
message: 'Duplicate @param "cfg.foo"',
},
],
},
{
code: `
export class SomeClass {
Expand Down

0 comments on commit c51d616

Please sign in to comment.