Skip to content

Commit d75fd6f

Browse files
committed
refactor: extract out retrieval for tagsWithNames and tagsWithoutNames through getTagsByType; also add jsdocUtils.filterTags
1 parent 57f33da commit d75fd6f

File tree

4 files changed

+41
-17
lines changed

4 files changed

+41
-17
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3752,6 +3752,12 @@ function quux (foo) {
37523752

37533753
}
37543754
// Message: Sentence should start with an uppercase character.
3755+
3756+
/**
3757+
* @typedef {Object} Hello World
3758+
* hello world
3759+
*/
3760+
// Message: Sentence must end with a period.
37553761
````
37563762

37573763
The following patterns are not considered problems:

src/iterateJsdoc.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,11 @@ const getUtils = (
161161
};
162162

163163
utils.filterTags = (filter) => {
164-
return (jsdoc.tags || []).filter(filter);
164+
return jsdocUtils.filterTags(jsdoc.tags, filter);
165+
};
166+
167+
utils.getTagsByType = (tags) => {
168+
return jsdocUtils.getTagsByType(tags, tagNamePreference);
165169
};
166170

167171
utils.getClassNode = () => {

src/jsdocUtils.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,13 +520,40 @@ const getContextObject = (contexts, checkJsdoc) => {
520520
}, {});
521521
};
522522

523+
const filterTags = (tags = [], filter) => {
524+
return tags.filter(filter);
525+
};
526+
527+
const tagsWithNamesAndDescriptions = ['param', 'arg', 'argument', 'returns', 'return'];
528+
529+
const getTagsByType = (tags, tagPreference) => {
530+
const descName = getPreferredTagName('description', tagPreference);
531+
const tagsWithoutNames = [];
532+
const tagsWithNames = filterTags(tags, (tag) => {
533+
const {tag: tagName} = tag;
534+
const tagWithName = tagsWithNamesAndDescriptions.includes(tagName);
535+
if (!tagWithName && tagName !== descName) {
536+
tagsWithoutNames.push(tag);
537+
}
538+
539+
return tagWithName;
540+
});
541+
542+
return {
543+
tagsWithoutNames,
544+
tagsWithNames
545+
};
546+
};
547+
523548
export default {
524549
enforcedContexts,
550+
filterTags,
525551
getContextObject,
526552
getFunctionParameterNames,
527553
getJsdocParameterNames,
528554
getJsdocParameterNamesDeep,
529555
getPreferredTagName,
556+
getTagsByType,
530557
hasATag,
531558
hasDefinedTypeReturnTag,
532559
hasReturnValue,

src/rules/matchDescription.js

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import _ from 'lodash';
22
import iterateJsdoc from '../iterateJsdoc';
33

4-
const tagsWithNamesAndDescriptions = ['param', 'arg', 'argument'];
5-
64
// If supporting Node >= 10, we could loosen the default to this for the
75
// initial letter: \\p{Upper}
86
const matchDescriptionDefault = '^[A-Z`\\d_][\\s\\S]*[.?!`]$';
@@ -57,28 +55,17 @@ export default iterateJsdoc(({
5755
return Boolean(options.tags[tagName]);
5856
};
5957

60-
let descName;
6158
utils.forEachPreferredTag('description', (matchingJsdocTag, targetTagName) => {
62-
descName = targetTagName;
6359
const description = (matchingJsdocTag.name + ' ' + matchingJsdocTag.description).trim();
6460
if (hasOptionTag(targetTagName)) {
6561
validateDescription(description, matchingJsdocTag);
6662
}
6763
});
6864

69-
const tagsWithoutNames = [];
70-
const tagsWithNames = utils.filterTags((tag) => {
71-
const {tag: tagName} = tag;
72-
if (!hasOptionTag(tagName)) {
73-
return false;
74-
}
75-
const tagWithName = tagsWithNamesAndDescriptions.includes(tagName);
76-
if (!tagWithName && tagName !== descName) {
77-
tagsWithoutNames.push(tag);
78-
}
79-
80-
return tagWithName;
65+
const whitelistedTags = utils.filterTags(({tag: tagName}) => {
66+
return hasOptionTag(tagName);
8167
});
68+
const {tagsWithNames, tagsWithoutNames} = utils.getTagsByType(whitelistedTags);
8269

8370
tagsWithNames.some((tag) => {
8471
const description = _.trimStart(tag.description, '- ');

0 commit comments

Comments
 (0)