Skip to content

Commit 45f7e63

Browse files
committed
- Refactoring: Avoid arrow functions as methods (in requireJsdoc); use forEachTag utility
1 parent cea72bf commit 45f7e63

9 files changed

+22
-52
lines changed

src/iterateJsdoc.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,16 @@ const curryUtils = (
205205
return false;
206206
};
207207

208+
utils.forEachTag = (tagName, arrayHandler) => {
209+
const matchingJsdocTags = _.filter(jsdoc.tags, {
210+
tag: tagName
211+
});
212+
213+
matchingJsdocTags.forEach((matchingJsdocTag) => {
214+
arrayHandler(matchingJsdocTag);
215+
});
216+
};
217+
208218
return utils;
209219
};
210220

src/rules/checkExamples.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import _ from 'lodash';
21
import {CLIEngine, Linter} from 'eslint';
32
import iterateJsdoc from '../iterateJsdoc';
43

@@ -15,7 +14,6 @@ const countChars = (str, ch) => {
1514
};
1615

1716
export default iterateJsdoc(({
18-
jsdoc,
1917
report,
2018
utils
2119
}) => {
@@ -62,11 +60,7 @@ export default iterateJsdoc(({
6260
exampleCodeRegex = exampleCodeRegex && new RegExp(exampleCodeRegex, '');
6361
rejectExampleCodeRegex = rejectExampleCodeRegex && new RegExp(rejectExampleCodeRegex, '');
6462

65-
_.forEach(jsdoc.tags, (tag) => {
66-
if (tag.tag !== 'example') {
67-
return;
68-
}
69-
63+
utils.forEachTag('example', (tag) => {
7064
// If a space is present, we should ignore it
7165
const initialTag = tag.source.match(/^@example ?/);
7266
const initialTagLength = initialTag[0].length;

src/rules/requireHyphenBeforeParamDescription.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,20 @@ import iterateJsdoc from '../iterateJsdoc';
33

44
export default iterateJsdoc(({
55
sourceCode,
6-
jsdoc,
6+
utils,
77
report,
88
context,
99
jsdocNode
1010
}) => {
1111
let always;
1212

13-
const jsdocTags = _.filter(jsdoc.tags, {
14-
tag: 'param'
15-
});
16-
1713
if (_.has(context.options, 0)) {
1814
always = context.options[0] === 'always';
1915
} else {
2016
always = true;
2117
}
2218

23-
_.forEach(jsdocTags, (jsdocTag) => {
19+
utils.forEachTag('param', (jsdocTag) => {
2420
if (!jsdocTag.description) {
2521
return;
2622
}

src/rules/requireJsdoc.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export default iterateJsdoc(null, {
108108
const options = getOptions(context);
109109

110110
return {
111-
ArrowFunctionExpression: (node) => {
111+
ArrowFunctionExpression (node) {
112112
if (!options.ArrowFunctionExpression) {
113113
return;
114114
}
@@ -120,23 +120,23 @@ export default iterateJsdoc(null, {
120120
checkJsDoc(node);
121121
},
122122

123-
ClassDeclaration: (node) => {
123+
ClassDeclaration (node) {
124124
if (!options.ClassDeclaration) {
125125
return;
126126
}
127127

128128
checkJsDoc(node);
129129
},
130130

131-
FunctionDeclaration: (node) => {
131+
FunctionDeclaration (node) {
132132
if (!options.FunctionDeclaration) {
133133
return;
134134
}
135135

136136
checkJsDoc(node);
137137
},
138138

139-
FunctionExpression: (node) => {
139+
FunctionExpression (node) {
140140
if (options.MethodDefinition && node.parent.type === 'MethodDefinition') {
141141
checkJsDoc(node);
142142

src/rules/requireParamDescription.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
1-
import _ from 'lodash';
21
import iterateJsdoc from '../iterateJsdoc';
32

43
export default iterateJsdoc(({
5-
jsdoc,
64
report,
75
utils
86
}) => {
97
const targetTagName = utils.getPreferredTagName('param');
108

11-
const jsdocParameters = _.filter(jsdoc.tags, {
12-
tag: targetTagName
13-
});
14-
15-
_.forEach(jsdocParameters, (jsdocParameter) => {
9+
utils.forEachTag(targetTagName, (jsdocParameter) => {
1610
if (!jsdocParameter.description) {
1711
report('Missing JSDoc @' + targetTagName + ' "' + jsdocParameter.name + '" description.', null, jsdocParameter);
1812
}

src/rules/requireParamName.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
1-
import _ from 'lodash';
21
import iterateJsdoc from '../iterateJsdoc';
32

43
export default iterateJsdoc(({
5-
jsdoc,
64
report,
75
utils
86
}) => {
97
const targetTagName = utils.getPreferredTagName('param');
108

11-
const jsdocParameters = _.filter(jsdoc.tags, {
12-
tag: targetTagName
13-
});
14-
15-
_.forEach(jsdocParameters, (jsdocParameter) => {
9+
utils.forEachTag(targetTagName, (jsdocParameter) => {
1610
if (jsdocParameter.tag && jsdocParameter.name === '') {
1711
report('There must be an identifier after @param ' + (jsdocParameter.type === '' ? 'type' : 'tag') + '.', null, jsdocParameter);
1812
}

src/rules/requireParamType.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
1-
import _ from 'lodash';
21
import iterateJsdoc from '../iterateJsdoc';
32

43
export default iterateJsdoc(({
5-
jsdoc,
64
report,
75
utils
86
}) => {
97
const targetTagName = utils.getPreferredTagName('param');
108

11-
const jsdocParameters = _.filter(jsdoc.tags, {
12-
tag: targetTagName
13-
});
14-
15-
_.forEach(jsdocParameters, (jsdocParameter) => {
9+
utils.forEachTag(targetTagName, (jsdocParameter) => {
1610
if (!jsdocParameter.type) {
1711
report('Missing JSDoc @' + targetTagName + ' "' + jsdocParameter.name + '" type.', null, jsdocParameter);
1812
}

src/rules/requireReturnsDescription.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
1-
import _ from 'lodash';
21
import iterateJsdoc from '../iterateJsdoc';
32

43
export default iterateJsdoc(({
5-
jsdoc,
64
report,
75
utils
86
}) => {
97
const targetTagName = utils.getPreferredTagName('returns');
108

11-
const jsdocTags = _.filter(jsdoc.tags, {
12-
tag: targetTagName
13-
});
14-
15-
_.forEach(jsdocTags, (jsdocTag) => {
9+
utils.forEachTag(targetTagName, (jsdocTag) => {
1610
const type = jsdocTag.type && jsdocTag.type.trim();
1711

1812
if (type === 'void' || type === 'undefined') {

src/rules/requireReturnsType.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
1-
import _ from 'lodash';
21
import iterateJsdoc from '../iterateJsdoc';
32

43
export default iterateJsdoc(({
5-
jsdoc,
64
report,
75
utils
86
}) => {
97
const targetTagName = utils.getPreferredTagName('returns');
108

11-
const jsdocTags = _.filter(jsdoc.tags, {
12-
tag: targetTagName
13-
});
14-
15-
_.forEach(jsdocTags, (jsdocTag) => {
9+
utils.forEachTag(targetTagName, (jsdocTag) => {
1610
if (!jsdocTag.type) {
1711
report('Missing JSDoc @' + targetTagName + ' type.', null, jsdocTag);
1812
}

0 commit comments

Comments
 (0)