Skip to content

Commit

Permalink
fix(check-param-names, require-jsdoc, require-param): avoid err…
Browse files Browse the repository at this point in the history
…ing upon `MemberExpression` or other constructions within dynamic properties; fixes #699
  • Loading branch information
brettz9 committed Feb 23, 2021
1 parent 937d65e commit 1cfdeb4
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 13 deletions.
12 changes: 12 additions & 0 deletions README.md
Expand Up @@ -3219,6 +3219,18 @@ function quux ({cfg}) {

}
// "jsdoc/check-param-names": ["error"|"warn", {"disableExtraPropertyReporting":true}]

class A {
/**
* @param cfg
* @param cfg.abc
*/
constructor({
[new.target.prop]: cX,
abc
}) {
}
}
````


Expand Down
29 changes: 17 additions & 12 deletions src/jsdocUtils.js
Expand Up @@ -136,20 +136,19 @@ const getFunctionParameterNames = (
}

if (param.type === 'Property') {
if (param.value.type === 'ArrayPattern') {
switch (param.value.type) {
case 'ArrayPattern':
return [param.key.name, param.value.elements.map((prop, idx) => {
return {
name: idx,
restElement: prop.type === 'RestElement',
};
})];
}
if (param.value.type === 'ObjectPattern') {
case 'ObjectPattern':
return [param.key.name, param.value.properties.map((prop) => {
return getParamName(prop, isProperty);
})];
}
if (param.value.type === 'AssignmentPattern') {
case 'AssignmentPattern': {
switch (param.value.left.type) {
case 'Identifier':
// Default parameter
Expand All @@ -172,19 +171,25 @@ const getFunctionParameterNames = (
})];
}
}
}

// As function parameters, these do not allow dynamic properties, etc.
/* istanbul ignore else */
if (param.key.type === 'Identifier') {
switch (param.key.type) {
case 'Identifier':
return param.key.name;
}

// The key of an object could also be a string or number
/* istanbul ignore else */
if (param.key.type === 'Literal') {
case 'Literal':
return param.key.raw ||
// istanbul ignore next -- `raw` may not be present in all parsers
param.key.value;

// case 'MemberExpression':
default:
// Todo: We should really create a structure (and a corresponding
// option analogous to `checkRestProperty`) which allows for
// (and optionally requires) dynamic properties to have a single
// line of documentation
return undefined;
}
}

Expand Down Expand Up @@ -212,7 +217,7 @@ const getFunctionParameterNames = (
return getParamName(param.parameter, true);
}

throw new Error('Unsupported function signature format.');
throw new Error(`Unsupported function signature format: \`${param.type}\`.`);
};

return (functionNode.params || functionNode.value.params).map((param) => {
Expand Down
2 changes: 1 addition & 1 deletion test/jsdocUtils.js
Expand Up @@ -54,7 +54,7 @@ describe('jsdocUtils', () => {
type: 'AssignmentPattern',
},
]});
}).to.throw('Unsupported function signature format.');
}).to.throw('Unsupported function signature format: `AssignmentPattern`.');
});
});
});
Expand Down
16 changes: 16 additions & 0 deletions test/rules/assertions/checkParamNames.js
Expand Up @@ -1624,5 +1624,21 @@ export default {
},
],
},
{
code: `
class A {
/**
* @param cfg
* @param cfg.abc
*/
constructor({
[new.target.prop]: cX,
abc
}) {
}
}
`,
parser: require.resolve('@typescript-eslint/parser'),
},
],
};

0 comments on commit 1cfdeb4

Please sign in to comment.