Skip to content

Commit

Permalink
fix: tag removal for 0-line tags (if other tags present, only blank o…
Browse files Browse the repository at this point in the history
…ut the tag; otherwise, remove to end of block); fixes #1040
  • Loading branch information
brettz9 committed Apr 24, 2023
1 parent 152e8c2 commit e99f4e8
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 3 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 7 additions & 3 deletions src/iterateJsdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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;
Expand Down
87 changes: 87 additions & 0 deletions test/rules/assertions/checkTagNames.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
{
Expand Down

0 comments on commit e99f4e8

Please sign in to comment.