Skip to content

Commit

Permalink
fix(check-param-name): fix up destructuring false errors
Browse files Browse the repository at this point in the history
The error mentioned in issue gajus#569 also occurred in `check-param-name` rule. So I fixed.
  • Loading branch information
hyex committed Sep 6, 2020
1 parent e28c78f commit dbd47f0
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 1 deletion.
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2132,6 +2132,31 @@ function quux ({foo, bar}, baz) {
}
// Options: [{"checkDestructured":false}]
// Message: Expected @param names to be "root, baz". Got "root, foo".

/**
* Description.
* @param {Object} options
* @param {FooBar} foo
*/
function quux ({ foo: { bar } }) {}
// Message: Missing @param "options.foo"

/**
* Description.
* @param {Object} options
* @param options.foo
*/
function quux ({ foo: { bar } }) {}
// Message: Missing @param "options.foo.bar"

/**
* Description.
* @param {object} options Options.
* @param {object} options.foo A description.
* @param {object} options.foo.bar
*/
function foo({ foo: { bar: { baz } }}) {}
// Message: Missing @param "options.foo.bar.baz"
````

The following patterns are not considered problems:
Expand Down Expand Up @@ -2408,6 +2433,29 @@ class A {
{
}
}

/**
* Description.
* @param {Object} options Options.
* @param {FooBar} options.foo foo description.
*/
function quux ({ foo: { bar }}) {}

/**
* Description.
* @param {FooBar} options
* @param {Object} options.foo
*/
function quux ({ foo: { bar } }) {}
// Options: [{"checkTypesPattern":"FooBar"}]

/**
* Description.
* @param {Object} options
* @param {FooBar} options.foo
* @param {FooBar} options.baz
*/
function quux ({ foo: { bar }, baz: { cfg } }) {}
````


Expand Down
16 changes: 15 additions & 1 deletion src/rules/checkParamNames.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,29 @@ const validateParameterNames = (
const actualNames = paramTags.map(([, paramTag]) => {
return paramTag.name.trim();
});
const actualTypes = paramTags.map(([, paramTag]) => {
return paramTag.type;
});

const missingProperties = [];
const notCheckingNames = [];

expectedNames.forEach((name, idx) => {
if (!actualNames.some(utils.comparePaths(name))) {
if (notCheckingNames.some((notCheckingName) => {
return name.startsWith(notCheckingName);
})) {
return;
}
const actualNameIdx = actualNames.findIndex((actualName) => {
return utils.comparePaths(name)(actualName);
});
if (actualNameIdx === -1) {
if (!checkRestProperty && rests[idx]) {
return;
}
missingProperties.push(name);
} else if (actualTypes[actualNameIdx].search(checkTypesRegex) === -1 && actualTypes[actualNameIdx] !== '') {
notCheckingNames.push(name);
}
});

Expand Down
85 changes: 85 additions & 0 deletions test/rules/assertions/checkParamNames.js
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,55 @@ export default {
checkDestructured: false,
}],
},
{
code: `
/**
* Description.
* @param {Object} options
* @param {FooBar} foo
*/
function quux ({ foo: { bar } }) {}
`,
errors: [
{
message: 'Missing @param "options.foo"',
},
{
message: 'Missing @param "options.foo.bar"',
},
],
},
{
code: `
/**
* Description.
* @param {Object} options
* @param options.foo
*/
function quux ({ foo: { bar } }) {}
`,
errors: [
{
message: 'Missing @param "options.foo.bar"',
},
],
},
{
code: `
/**
* Description.
* @param {object} options Options.
* @param {object} options.foo A description.
* @param {object} options.foo.bar
*/
function foo({ foo: { bar: { baz } }}) {}
`,
errors: [
{
message: 'Missing @param "options.foo.bar.baz"',
},
],
},
],
valid: [
{
Expand Down Expand Up @@ -1297,5 +1346,41 @@ export default {
sourceType: 'module',
},
},
{
code: `
/**
* Description.
* @param {Object} options Options.
* @param {FooBar} options.foo foo description.
*/
function quux ({ foo: { bar }}) {}
`,
},
{
code: `
/**
* Description.
* @param {FooBar} options
* @param {Object} options.foo
*/
function quux ({ foo: { bar } }) {}
`,
options: [
{
checkTypesPattern: 'FooBar',
},
],
},
{
code: `
/**
* Description.
* @param {Object} options
* @param {FooBar} options.foo
* @param {FooBar} options.baz
*/
function quux ({ foo: { bar }, baz: { cfg } }) {}
`,
},
],
};

0 comments on commit dbd47f0

Please sign in to comment.