From e6c9ab27f41347475263a946c4ad3a0319aa89af Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Fri, 14 May 2021 14:34:47 +0800 Subject: [PATCH] fix(`check-param-names`): adjusts line numbers to be nearer problematic item Also adds test line numbers. --- src/iterateJsdoc.js | 2 ++ src/jsdocUtils.js | 6 ++++++ src/rules/checkParamNames.js | 15 ++++++++++++--- test/rules/assertions/checkParamNames.js | 2 ++ 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/iterateJsdoc.js b/src/iterateJsdoc.js index bd96cfa27..0d0c39c83 100644 --- a/src/iterateJsdoc.js +++ b/src/iterateJsdoc.js @@ -39,6 +39,8 @@ const getBasicUtils = (context, {tagNamePreference, mode}) => { return jsdocUtils.parseClosureTemplateTag(tag); }; + utils.pathDoesNotBeginWith = jsdocUtils.pathDoesNotBeginWith; + utils.getPreferredTagNameObject = ({tagName}) => { const ret = jsdocUtils.getPreferredTagName(context, mode, tagName, tagNamePreference); const isObject = ret && typeof ret === 'object'; diff --git a/src/jsdocUtils.js b/src/jsdocUtils.js index f1017afc3..715b94b34 100644 --- a/src/jsdocUtils.js +++ b/src/jsdocUtils.js @@ -1219,6 +1219,11 @@ const comparePaths = (name) => { }; }; +const pathDoesNotBeginWith = (name, otherPathName) => { + return !name.startsWith(otherPathName) && + !dropPathSegmentQuotes(name).startsWith(dropPathSegmentQuotes(otherPathName)); +}; + const getRegexFromString = (regexString, requiredFlags) => { const match = regexString.match(/^\/(.*)\/([gimyus]*)$/us); let flags = 'u'; @@ -1266,6 +1271,7 @@ export default { isValidTag, overrideTagStructure, parseClosureTemplateTag, + pathDoesNotBeginWith, setTagStructure, tagMightHaveNamepath, tagMightHaveNamePosition, diff --git a/src/rules/checkParamNames.js b/src/rules/checkParamNames.js index ff4a30200..cc1dc86b6 100644 --- a/src/rules/checkParamNames.js +++ b/src/rules/checkParamNames.js @@ -99,7 +99,16 @@ const validateParameterNames = ( if (!checkRestProperty && rests[idx]) { return; } - missingProperties.push(name); + const missingIndex = actualNames.findIndex((actualName) => { + return utils.pathDoesNotBeginWith(name, actualName); + }); + const line = tag.source[0].number - 1 + (missingIndex > -1 ? missingIndex : actualNames.length); + missingProperties.push({ + name, + tagPlacement: { + line: line === 0 ? 1 : line, + }, + }); } else if (actualTypes[actualNameIdx].search(checkTypesRegex) === -1 && actualTypes[actualNameIdx] !== '') { notCheckingNames.push(name); } @@ -107,8 +116,8 @@ const validateParameterNames = ( const hasMissing = missingProperties.length; if (hasMissing) { - missingProperties.forEach((missingProperty) => { - report(`Missing @${targetTagName} "${missingProperty}"`, null, tag); + missingProperties.forEach(({tagPlacement, name: missingProperty}) => { + report(`Missing @${targetTagName} "${missingProperty}"`, null, tagPlacement); }); } diff --git a/test/rules/assertions/checkParamNames.js b/test/rules/assertions/checkParamNames.js index db76c4146..c3a81ce9a 100644 --- a/test/rules/assertions/checkParamNames.js +++ b/test/rules/assertions/checkParamNames.js @@ -935,6 +935,7 @@ export default { `, errors: [ { + line: 5, message: 'Missing @param "options.foo.bar"', }, ], @@ -951,6 +952,7 @@ export default { `, errors: [ { + line: 6, message: 'Missing @param "options.foo.bar.baz"', }, ],