From 4f3c48eed95456513c53eacfe61ab5451da1c698 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Fri, 21 Apr 2023 07:43:20 -0700 Subject: [PATCH] fix: tag removal for 0-line tags (if other tags present, only blank out the tag; otherwise, remove to end of block); fixes #1040 --- README.md | 21 +++++++ src/iterateJsdoc.js | 10 ++- test/rules/assertions/checkTagNames.js | 87 ++++++++++++++++++++++++++ 3 files changed, 115 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index cebb03c96..7515c216d 100644 --- a/README.md +++ b/README.md @@ -5013,6 +5013,27 @@ function Test() { } // Settings: {"jsdoc":{"tagNamePreference":{"returns":"return"}}} // Message: Invalid JSDoc tag (preference). Replace "constructor" JSDoc tag with "class". + +/** @typedef {Object} MyObject + * @property {string} id - my id + */ +// "jsdoc/check-tag-names": ["error"|"warn", {"typed":true}] +// Message: '@typedef' is redundant when using a type system. + +/** + * @property {string} id - my id + */ +// "jsdoc/check-tag-names": ["error"|"warn", {"typed":true}] +// Message: '@property' is redundant when using a type system. + +/** @typedef {Object} MyObject */ +// "jsdoc/check-tag-names": ["error"|"warn", {"typed":true}] +// Message: '@typedef' is redundant when using a type system. + +/** @typedef {Object} MyObject + */ +// "jsdoc/check-tag-names": ["error"|"warn", {"typed":true}] +// Message: '@typedef' is redundant when using a type system. ```` The following patterns are not considered problems: diff --git a/src/iterateJsdoc.js b/src/iterateJsdoc.js index a59382bb9..d41ab0fe3 100644 --- a/src/iterateJsdoc.js +++ b/src/iterateJsdoc.js @@ -407,11 +407,15 @@ const getUtils = ( } = jsdoc.source[spliceIdx].tokens; /* istanbul ignore if -- Currently want to clear entirely if removing tags */ - if (!removeEmptyBlock && (end || delimiter === '/**')) { + if ( + spliceIdx === 0 && jsdoc.tags.length >= 2 || + !removeEmptyBlock && (end || delimiter === '/**') + ) { const { tokens, } = jsdoc.source[spliceIdx]; for (const item of [ + 'postDelimiter', 'tag', 'postTag', 'type', @@ -423,10 +427,10 @@ const getUtils = ( tokens[item] = ''; } } else { - jsdoc.source.splice(spliceIdx, spliceCount - tagSourceOffset); + jsdoc.source.splice(spliceIdx, spliceCount - tagSourceOffset + (spliceIdx ? 0 : jsdoc.source.length)); + tagSource.splice(tagIdx + tagSourceOffset, spliceCount - tagSourceOffset + (spliceIdx ? 0 : jsdoc.source.length)); } - tagSource.splice(tagIdx + tagSourceOffset, spliceCount - tagSourceOffset); lastIndex = sourceIndex; return true; diff --git a/test/rules/assertions/checkTagNames.js b/test/rules/assertions/checkTagNames.js index e89d92495..87b2864ab 100644 --- a/test/rules/assertions/checkTagNames.js +++ b/test/rules/assertions/checkTagNames.js @@ -925,6 +925,93 @@ export default { }, }, }, + { + code: ` + /** @typedef {Object} MyObject + * @property {string} id - my id + */ + `, + errors: [ + { + line: 2, + message: '\'@typedef\' is redundant when using a type system.', + }, + { + line: 3, + message: '\'@property\' is redundant when using a type system.', + }, + ], + options: [ + { + typed: true, + }, + ], + output: ` + /** + * @property {string} id - my id + */ + `, + }, + { + code: ` + /** + * @property {string} id - my id + */ + `, + errors: [ + { + line: 3, + message: '\'@property\' is redundant when using a type system.', + }, + ], + options: [ + { + typed: true, + }, + ], + output: ` + /** + * id - my id + */ + `, + }, + { + code: ` + /** @typedef {Object} MyObject */ + `, + errors: [ + { + line: 2, + message: '\'@typedef\' is redundant when using a type system.', + }, + ], + options: [ + { + typed: true, + }, + ], + output: ` + `, + }, + { + code: ` + /** @typedef {Object} MyObject + */ + `, + errors: [ + { + line: 2, + message: '\'@typedef\' is redundant when using a type system.', + }, + ], + options: [ + { + typed: true, + }, + ], + output: ` + `, + }, ], valid: [ {