diff --git a/README.md b/README.md index e42906ad2..1a4355f62 100644 --- a/README.md +++ b/README.md @@ -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 + }) { + } +} ```` diff --git a/src/jsdocUtils.js b/src/jsdocUtils.js index 34173a6dd..48a4ad57a 100644 --- a/src/jsdocUtils.js +++ b/src/jsdocUtils.js @@ -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 @@ -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; } } @@ -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) => { diff --git a/test/jsdocUtils.js b/test/jsdocUtils.js index 2b376f498..c1b83b817 100644 --- a/test/jsdocUtils.js +++ b/test/jsdocUtils.js @@ -54,7 +54,7 @@ describe('jsdocUtils', () => { type: 'AssignmentPattern', }, ]}); - }).to.throw('Unsupported function signature format.'); + }).to.throw('Unsupported function signature format: `AssignmentPattern`.'); }); }); }); diff --git a/test/rules/assertions/checkParamNames.js b/test/rules/assertions/checkParamNames.js index d561ff5f6..2ab36b8d6 100644 --- a/test/rules/assertions/checkParamNames.js +++ b/test/rules/assertions/checkParamNames.js @@ -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'), + }, ], };