Skip to content

Commit b604128

Browse files
committed
- Refactoring: Use ES6 features over lodash where equally terse; some destructuring
1 parent b38b587 commit b604128

15 files changed

+59
-61
lines changed

src/bin/readme-assertions.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ import _ from 'lodash';
77
import glob from 'glob';
88

99
const trimCode = (code) => {
10-
let lines = _.trim(code).split('\n');
10+
let lines = code.trim().split('\n');
1111

1212
const indendation = lines[lines.length - 1].match(/^\s+/);
1313

1414
const indentSize = indendation ? indendation[0].length : 0;
1515

16-
lines = _.map(lines, (line, index) => {
16+
lines = lines.map((line, index) => {
1717
if (index === 0) {
1818
return line;
1919
}
@@ -47,11 +47,11 @@ const formatCodeSnippet = (setup) => {
4747
const getAssertions = () => {
4848
const assertionFiles = glob.sync(path.resolve(__dirname, '../../test/rules/assertions/*.js'));
4949

50-
const assertionNames = _.map(assertionFiles, (filePath) => {
50+
const assertionNames = assertionFiles.map((filePath) => {
5151
return path.basename(filePath, '.js');
5252
});
5353

54-
const assertionCodes = _.map(assertionFiles, (filePath) => {
54+
const assertionCodes = assertionFiles.map((filePath) => {
5555
// eslint-disable-next-line global-require, import/no-dynamic-require
5656
const codes = require(filePath);
5757

src/iterateJsdoc.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const parseComment = (commentNode, indent) => {
1111
commentParser.PARSERS.parse_tag,
1212
commentParser.PARSERS.parse_type,
1313
(str, data) => {
14-
if (_.includes(['return', 'returns', 'throws', 'exception'], data.tag)) {
14+
if (['return', 'returns', 'throws', 'exception'].includes(data.tag)) {
1515
return null;
1616
}
1717

@@ -150,10 +150,10 @@ const curryUtils = (
150150
};
151151

152152
utils.passesEmptyNamepathCheck = (tag) => {
153-
return !tag.name && allowEmptyNamepaths && _.includes([
153+
return !tag.name && allowEmptyNamepaths && [
154154
// These may serve some minor purpose when empty
155155
'callback', 'event', 'listens', 'fires', 'emits'
156-
], tag.tag);
156+
].includes(tag.tag);
157157
};
158158

159159
utils.hasDefinedTypeReturnTag = (tag) => {

src/jsdocUtils.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,20 @@ const getJsdocParameterNames = (jsdoc : Object, targetTagName : string) : Array<
4747

4848
jsdocParameterNames = getJsdocParameterNamesDeep(jsdoc, targetTagName);
4949

50-
jsdocParameterNames = _.filter(jsdocParameterNames, (name) => {
51-
return name.indexOf('.') === -1;
50+
jsdocParameterNames = jsdocParameterNames.filter((name) => {
51+
return !name.includes('.');
5252
});
5353

5454
return jsdocParameterNames;
5555
};
5656

5757
const getPreferredTagName = (name : string, tagPreference : Object = {}) : string => {
58-
if (_.includes(_.values(tagPreference), name)) {
58+
if (_.values(tagPreference).includes(name)) {
5959
return name;
6060
}
6161

6262
const preferredTagName = _.findKey(tagNames, (aliases) => {
63-
return _.includes(aliases, name);
63+
return aliases.includes(name);
6464
});
6565

6666
if (preferredTagName) {
@@ -75,7 +75,7 @@ const isValidTag = (name : string, additionalTagNames : Object) : boolean => {
7575
const additionalTags = additionalTagNames.customTags || [];
7676
const allTags = validTagNames.concat(additionalTags);
7777

78-
return _.includes(allTags, name);
78+
return allTags.includes(name);
7979
};
8080

8181
const hasTag = (jsdoc : Object, targetTagName : string) : boolean => {
@@ -136,7 +136,7 @@ const namepathAsNameTags = [
136136
];
137137

138138
const isNamepathType = (tagName, checkSeesForNamepaths) => {
139-
return _.includes(namepathAsNameTags, tagName) ||
139+
return namepathAsNameTags.includes(tagName) ||
140140
tagName === 'see' && checkSeesForNamepaths;
141141
};
142142

src/rules/checkTagNames.js

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

43
export default iterateJsdoc(({
@@ -8,7 +7,7 @@ export default iterateJsdoc(({
87
utils,
98
jsdocNode
109
}) => {
11-
_.forEach(jsdoc.tags, (jsdocTag) => {
10+
jsdoc.tags.forEach((jsdocTag) => {
1211
if (utils.isValidTag(jsdocTag.tag)) {
1312
const preferredTagName = utils.getPreferredTagName(jsdocTag.tag);
1413

src/rules/checkTypes.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import _ from 'lodash';
21
import {parse, traverse, publish} from 'jsdoctypeparser';
32
import iterateJsdoc from '../iterateJsdoc';
43

@@ -49,11 +48,11 @@ export default iterateJsdoc(({
4948
sourceCode,
5049
report
5150
}) => {
52-
const jsdocTags = _.filter(jsdoc.tags, (tag) => {
53-
return _.includes(targetTags, tag.tag);
51+
const jsdocTags = jsdoc.tags.filter((tag) => {
52+
return targetTags.includes(tag.tag);
5453
});
5554

56-
_.forEach(jsdocTags, (jsdocTag) => {
55+
jsdocTags.forEach((jsdocTag) => {
5756
const invalidTypes = [];
5857
let typeAst;
5958

@@ -77,7 +76,7 @@ export default iterateJsdoc(({
7776
if (invalidTypes) {
7877
const fixedType = publish(typeAst);
7978

80-
_.forEach(invalidTypes, (invalidType) => {
79+
invalidTypes.forEach((invalidType) => {
8180
const fix = (fixer) => {
8281
return fixer.replaceText(jsdocNode, sourceCode.getText(jsdocNode).replace('{' + jsdocTag.type + '}', '{' + fixedType + '}'));
8382
};

src/rules/matchDescription.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ export default iterateJsdoc(({
3838
return;
3939
}
4040

41-
const tags = _.filter(jsdoc.tags, (tag) => {
42-
return _.includes(tagsWithDescriptions, tag.tag) &&
43-
{}.hasOwnProperty.call(options.tags, tag.tag) && options.tags[tag.tag];
41+
const tags = jsdoc.tags.filter(({tag}) => {
42+
return tagsWithDescriptions.includes(tag) &&
43+
{}.hasOwnProperty.call(options.tags, tag) && options.tags[tag];
4444
});
4545

46-
_.some(tags, (tag) => {
46+
tags.some((tag) => {
4747
const description = _.trimStart(tag.description, '- ');
4848

4949
return validateDescription(description, tag.tag);

src/rules/newlineAfterDescription.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ export default iterateJsdoc(({
2424
// The contents of the jsdoc.source and of jsdoc.description is left trimmed.
2525
// The contents of the jsdoc.description is right trimmed.
2626
// This gets the text following the description.
27-
const descriptionEndsWithANewline = _.startsWith(jsdoc.source.slice(jsdoc.description.length), '\n\n');
27+
const descriptionEndsWithANewline = jsdoc.source.slice(jsdoc.description.length).startsWith('\n\n');
2828

2929
if (always) {
3030
if (!descriptionEndsWithANewline) {
3131
report('There must be a newline after the description of the JSDoc block.', (fixer) => {
3232
const sourceLines = sourceCode.getText(jsdocNode).split('\n');
3333
const lastDescriptionLine = _.findLastIndex(sourceLines, (line) => {
34-
return _.includes(line, _.last(jsdoc.description.split('\n')));
34+
return line.includes(_.last(jsdoc.description.split('\n')));
3535
});
3636

3737
// Add the new line
@@ -44,7 +44,7 @@ export default iterateJsdoc(({
4444
report('There must be no newline after the description of the JSDoc block.', (fixer) => {
4545
const sourceLines = sourceCode.getText(jsdocNode).split('\n');
4646
const lastDescriptionLine = _.findLastIndex(sourceLines, (line) => {
47-
return _.includes(line, _.last(jsdoc.description.split('\n')));
47+
return line.includes(_.last(jsdoc.description.split('\n')));
4848
});
4949

5050
// Remove the extra line

src/rules/noUndefinedTypes.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ export default iterateJsdoc(({
3333

3434
const typedefDeclarations = _(context.getAllComments())
3535
.filter((comment) => {
36-
return _.startsWith(comment.value, '*');
36+
return comment.value.startsWith('*');
3737
})
3838
.map(parseComment)
3939
.flatMap((doc) => {
40-
return (doc.tags || []).filter((tag) => {
41-
return _.includes(tagsWithNames, tag.tag);
40+
return (doc.tags || []).filter(({tag}) => {
41+
return tagsWithNames.includes(tag);
4242
});
4343
})
4444
.map((tag) => {
@@ -68,7 +68,7 @@ export default iterateJsdoc(({
6868
.concat(extraTypes)
6969
.concat(typedefDeclarations);
7070

71-
_.forEach(jsdoc.tags, (tag) => {
71+
jsdoc.tags.forEach((tag) => {
7272
let parsedType;
7373

7474
try {
@@ -78,12 +78,12 @@ export default iterateJsdoc(({
7878
return;
7979
}
8080

81-
traverse(parsedType, (node) => {
82-
if (node.type === 'NAME') {
83-
if (!_.includes(definedTypes, node.name)) {
84-
report('The type \'' + node.name + '\' is undefined.', null, tag);
85-
} else if (!_.includes(extraTypes, node.name)) {
86-
context.markVariableAsUsed(node.name);
81+
traverse(parsedType, ({type, name}) => {
82+
if (type === 'NAME') {
83+
if (!definedTypes.includes(name)) {
84+
report('The type \'' + name + '\' is undefined.', null, tag);
85+
} else if (!_.includes(extraTypes, name)) {
86+
context.markVariableAsUsed(name);
8787
}
8888
}
8989
});

src/rules/requireDescription.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@ export default iterateJsdoc(({
1212
tag: targetTagName
1313
});
1414

15-
if (_.isEmpty(functionExamples)) {
16-
return report('Missing JSDoc @' + targetTagName + ' declaration.');
15+
if (!functionExamples.length) {
16+
report('Missing JSDoc @' + targetTagName + ' declaration.');
17+
18+
return;
1719
}
1820

19-
return _.forEach(functionExamples, (example) => {
21+
functionExamples.forEach((example) => {
2022
const exampleContent = _.compact((example.name + ' ' + example.description).trim().split('\n'));
2123

22-
if (_.isEmpty(exampleContent)) {
24+
if (!exampleContent.length) {
2325
report('Missing JSDoc @' + targetTagName + ' description.');
2426
}
2527
});

src/rules/requireDescriptionCompleteSentence.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ const isNewLinePrecededByAPeriod = (text) => {
2828

2929
const lines = text.split('\n');
3030

31-
return !_.some(lines, (line) => {
32-
if (_.isBoolean(lastLineEndsSentence) && !lastLineEndsSentence && /^[A-Z]/.test(line)) {
31+
return !lines.some((line) => {
32+
if (typeof lastLineEndsSentence === 'boolean' && !lastLineEndsSentence && /^[A-Z]/.test(line)) {
3333
return true;
3434
}
3535

@@ -54,7 +54,7 @@ const validateDescription = (description, report, jsdocNode, sourceCode, tag) =>
5454

5555
const paragraphs = extractParagraphs(description);
5656

57-
return _.some(paragraphs, (paragraph) => {
57+
return paragraphs.some((paragraph) => {
5858
const sentences = extractSentences(paragraph);
5959

6060
const fix = (fixer) => {
@@ -85,7 +85,7 @@ const validateDescription = (description, report, jsdocNode, sourceCode, tag) =>
8585
return fixer.replaceText(jsdocNode, text);
8686
};
8787

88-
if (_.some(sentences, (sentence) => {
88+
if (sentences.some((sentence) => {
8989
return !isCapitalized(sentence);
9090
})) {
9191
report('Sentence should start with an uppercase character.', fix);
@@ -117,11 +117,11 @@ export default iterateJsdoc(({
117117
return;
118118
}
119119

120-
const tags = _.filter(jsdoc.tags, (tag) => {
121-
return _.includes(['param', 'arg', 'argument', 'returns', 'return'], tag.tag);
120+
const tags = jsdoc.tags.filter((tag) => {
121+
return ['param', 'arg', 'argument', 'returns', 'return'].includes(tag.tag);
122122
});
123123

124-
_.some(tags, (tag) => {
124+
tags.some((tag) => {
125125
const description = _.trimStart(tag.description, '- ');
126126

127127
return validateDescription(description, report, jsdocNode, sourceCode, tag.tag);

src/rules/requireExample.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,16 @@ export default iterateJsdoc(({
2929
return;
3030
}
3131

32-
if (_.isEmpty(functionExamples)) {
32+
if (!functionExamples.length) {
3333
report('Missing JSDoc @' + targetTagName + ' declaration.');
3434

3535
return;
3636
}
3737

38-
_.forEach(functionExamples, (example) => {
38+
functionExamples.forEach((example) => {
3939
const exampleContent = _.compact((example.name + ' ' + example.description).trim().split('\n'));
4040

41-
if (_.isEmpty(exampleContent)) {
41+
if (!exampleContent.length) {
4242
report('Missing JSDoc @' + targetTagName + ' description.');
4343
}
4444
});

src/rules/requireHyphenBeforeParamDescription.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ export default iterateJsdoc(({
2222
}
2323

2424
if (always) {
25-
if (!_.startsWith(jsdocTag.description, '-')) {
25+
if (!jsdocTag.description.startsWith('-')) {
2626
report('There must be a hyphen before @param description.', (fixer) => {
2727
const replacement = sourceCode.getText(jsdocNode).replace(jsdocTag.description, '- ' + jsdocTag.description);
2828

2929
return fixer.replaceText(jsdocNode, replacement);
3030
}, jsdocTag);
3131
}
32-
} else if (_.startsWith(jsdocTag.description, '-')) {
32+
} else if (jsdocTag.description.startsWith('-')) {
3333
report('There must be no hyphen before @param description.', (fixer) => {
3434
const [unwantedPart] = /-\s*/.exec(jsdocTag.description);
3535

src/rules/requireParam.js

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

43
// eslint-disable-next-line complexity
@@ -33,7 +32,7 @@ export default iterateJsdoc(({
3332
return;
3433
}
3534

36-
_.some(functionParameterNames, (functionParameterName, index) => {
35+
functionParameterNames.some((functionParameterName, index) => {
3736
const jsdocParameterName = jsdocParameterNames[index];
3837

3938
if (!jsdocParameterName) {

src/rules/validTypes.js

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

@@ -14,7 +13,7 @@ export default iterateJsdoc(({
1413
report,
1514
utils
1615
}) => {
17-
_.forEach(jsdoc.tags, (tag) => {
16+
jsdoc.tags.forEach((tag) => {
1817
const validTypeParsing = function (type) {
1918
try {
2019
parse(type);

test/rules/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import config from '../../src';
66

77
const ruleTester = new RuleTester();
88

9-
_.forEach([
9+
[
1010
'check-alignment',
1111
'check-examples',
1212
'check-indentation',
@@ -31,21 +31,21 @@ _.forEach([
3131
'require-returns-description',
3232
'require-returns-type',
3333
'valid-types'
34-
], (ruleName) => {
34+
].forEach((ruleName) => {
3535
const parserOptions = {
3636
ecmaVersion: 6
3737
};
3838

3939
// eslint-disable-next-line global-require, import/no-dynamic-require
4040
const assertions = require('./assertions/' + _.camelCase(ruleName));
4141

42-
assertions.invalid = _.map(assertions.invalid, (assertion) => {
42+
assertions.invalid = assertions.invalid.map((assertion) => {
4343
assertion.parserOptions = _.defaultsDeep(assertion.parserOptions, parserOptions);
4444

4545
return assertion;
4646
});
4747

48-
assertions.valid = _.map(assertions.valid, (assertion) => {
48+
assertions.valid = assertions.valid.map((assertion) => {
4949
assertion.parserOptions = _.defaultsDeep(assertion.parserOptions, parserOptions);
5050

5151
return assertion;

0 commit comments

Comments
 (0)