Skip to content

Commit 5fbb15b

Browse files
committed
chore(dev-deps): add eslint-plugin-unicorn
https://github.com/sindresorhus/eslint-plugin-unicorn
1 parent 6d5be9f commit 5fbb15b

17 files changed

+41
-25
lines changed

.eslintrc.js

+13
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ module.exports = {
77
'not-an-aardvark/node',
88
'plugin:node/recommended',
99
'plugin:self/all',
10+
'plugin:unicorn/recommended',
1011
],
12+
parserOptions: {
13+
ecmaVersion: 2021,
14+
sourceType: 'script',
15+
},
1116
rules: {
1217
'comma-dangle': [
1318
'error',
@@ -22,6 +27,14 @@ module.exports = {
2227
'self/meta-property-ordering': 'off',
2328
'self/require-meta-docs-url': 'off',
2429
'self/report-message-format': ['error', '^[^a-z].*.$'],
30+
31+
'unicorn/consistent-function-scoping': 'off',
32+
'unicorn/no-array-callback-reference': 'off',
33+
'unicorn/no-array-for-each': 'off',
34+
'unicorn/no-array-reduce': 'off',
35+
'unicorn/no-null': 'off',
36+
'unicorn/prefer-module': 'off',
37+
'unicorn/prevent-abbreviations': 'off',
2538
},
2639
overrides: [
2740
{

lib/rules/consistent-output.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ module.exports = {
3939
utils.getTestInfo(context, ast).forEach(testRun => {
4040
const readableCases = testRun.invalid.filter(testCase => testCase.type === 'ObjectExpression');
4141
const casesWithoutOutput = readableCases
42-
.filter(testCase => testCase.properties.map(utils.getKeyName).indexOf('output') === -1);
42+
.filter(testCase => !testCase.properties.map(utils.getKeyName).includes('output'));
4343

4444
if (
4545
(casesWithoutOutput.length < readableCases.length) ||

lib/rules/meta-property-ordering.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ module.exports = {
5151
const violatingProps = props.filter(prop => {
5252
const curr = orderMap.has(getKeyName(prop))
5353
? orderMap.get(getKeyName(prop))
54-
: Infinity;
54+
: Number.POSITIVE_INFINITY;
5555
return last > (last = curr);
5656
});
5757

lib/rules/no-deprecated-context-methods.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ module.exports = {
5555

5656
return {
5757
'Program:exit' () {
58-
Array.from(utils.getContextIdentifiers(context, sourceCode.ast))
58+
[...utils.getContextIdentifiers(context, sourceCode.ast)]
5959
.filter(
6060
contextId =>
6161
contextId.parent.type === 'MemberExpression' &&

lib/rules/no-identical-tests.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ module.exports = {
6060
propertiesSetA.add(code);
6161
});
6262

63-
for (let i = 0; i < propertiesB.length; i++) {
64-
const code = sourceCode.getText(propertiesB[i]);
63+
for (const element of propertiesB) {
64+
const code = sourceCode.getText(element);
6565
if (!propertiesSetA.has(code)) {
6666
return false;
6767
}

lib/rules/no-missing-placeholders.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ module.exports = {
5656
) {
5757
// Same regex as the one ESLint uses
5858
// https://github.com/eslint/eslint/blob/e5446449d93668ccbdb79d78cc69f165ce4fde07/lib/eslint.js#L990
59-
const PLACEHOLDER_MATCHER = /\{\{\s*([^{}]+?)\s*\}\}/g;
59+
const PLACEHOLDER_MATCHER = /{{\s*([^{}]+?)\s*}}/g;
6060
let match;
6161

6262
while ((match = PLACEHOLDER_MATCHER.exec(reportInfo.message.value || messageStaticValue.value))) { // eslint-disable-line no-extra-parens

lib/rules/no-unused-placeholders.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ module.exports = {
5757
) {
5858
const message = reportInfo.message.value || messageStaticValue.value;
5959
// https://github.com/eslint/eslint/blob/2874d75ed8decf363006db25aac2d5f8991bd969/lib/linter.js#L986
60-
const PLACEHOLDER_MATCHER = /\{\{\s*([^{}]+?)\s*\}\}/g;
60+
const PLACEHOLDER_MATCHER = /{{\s*([^{}]+?)\s*}}/g;
6161
const placeholdersInMessage = new Set();
6262

6363
message.replace(PLACEHOLDER_MATCHER, (fullMatch, term) => {

lib/rules/no-useless-token-range.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ module.exports = {
103103

104104
return {
105105
'Program:exit' (ast) {
106-
Array.from(utils.getSourceCodeIdentifiers(context, ast))
106+
[...utils.getSourceCodeIdentifiers(context, ast)]
107107
.filter(identifier => identifier.parent.type === 'MemberExpression' &&
108108
identifier.parent.object === identifier &&
109109
identifier.parent.property.type === 'Identifier' &&

lib/rules/prefer-output-null.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ module.exports = {
4343
function getTestInfo (key) {
4444
if (test.type === 'ObjectExpression') {
4545
const res = test.properties.filter(item => item.key.name === key);
46-
return res.length ? res[res.length - 1] : null;
46+
return res.length > 0 ? res[res.length - 1] : null;
4747
}
4848
return key === 'code' ? test : null;
4949
}

lib/rules/prefer-placeholders.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ module.exports = {
7575
}
7676

7777
if (
78-
(messageNode.type === 'TemplateLiteral' && messageNode.expressions.length) ||
78+
(messageNode.type === 'TemplateLiteral' && messageNode.expressions.length > 0) ||
7979
(messageNode.type === 'BinaryExpression' && messageNode.operator === '+')
8080
) {
8181
context.report({

lib/rules/require-meta-docs-url.js

+1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ module.exports = {
9494

9595
message:
9696
!urlPropNode ? 'Rules should export a `meta.docs.url` property.' :
97+
// eslint-disable-next-line unicorn/no-nested-ternary
9798
!expectedUrl ? '`meta.docs.url` property must be a string.' :
9899
/* otherwise */ '`meta.docs.url` property must be `{{expectedUrl}}`.',
99100

lib/rules/require-meta-fixable.js

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ module.exports = {
6565
const VALID_VALUES = new Set(['code', 'whitespace', null, undefined]);
6666
const valueIsValid = metaFixableProp.value.type === 'Literal'
6767
? VALID_VALUES.has(metaFixableProp.value.value)
68+
// eslint-disable-next-line unicorn/no-nested-ternary
6869
: metaFixableProp.value.type === 'TemplateLiteral' && metaFixableProp.value.quasis.length === 1
6970
? VALID_VALUES.has(metaFixableProp.value.quasis[0].value.cooked)
7071
: metaFixableProp.value.type === 'Identifier' && metaFixableProp.value.name === 'undefined';

lib/rules/test-case-property-ordering.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,8 @@ module.exports = {
5858
// current < lastChecked to catch unordered;
5959
// and lastChecked === -1 to catch extra properties before.
6060
if (current > -1 && (current < lastChecked || lastChecked === -1)) {
61-
let orderMsg = order.filter(item => keyNames.indexOf(item) > -1);
62-
orderMsg = orderMsg.concat(
63-
lastChecked === -1 ? keyNames.filter(item => order.indexOf(item) === -1) : []
64-
);
61+
let orderMsg = order.filter(item => keyNames.includes(item));
62+
orderMsg = [...orderMsg, ...lastChecked === -1 ? keyNames.filter(item => !order.includes(item)) : []];
6563

6664
context.report({
6765
node: properties[i],

lib/utils.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function isNormalFunctionExpressionReference (node, scopeManager) {
5353
}
5454

5555
const definitions = createReference.resolved.defs;
56-
if (!definitions || !definitions.length) {
56+
if (!definitions || definitions.length === 0) {
5757
return false;
5858
}
5959

@@ -175,7 +175,7 @@ module.exports = {
175175
getContextIdentifiers (context, ast) {
176176
const ruleInfo = module.exports.getRuleInfo({ ast });
177177

178-
if (!ruleInfo || !ruleInfo.create.params.length || ruleInfo.create.params[0].type !== 'Identifier') {
178+
if (!ruleInfo || ruleInfo.create.params.length === 0 || ruleInfo.create.params[0].type !== 'Identifier') {
179179
return new Set();
180180
}
181181

@@ -270,7 +270,7 @@ module.exports = {
270270
// ['node', 'message', 'data', 'fix'].
271271
// Otherwise, the arguments are interpreted as ['node', 'loc', 'message', 'data', 'fix'].
272272

273-
if (!reportArgs.length) {
273+
if (reportArgs.length === 0) {
274274
return null;
275275
}
276276

@@ -320,7 +320,7 @@ module.exports = {
320320
* @returns {Set<ASTNode>} A set of all identifiers referring to the `SourceCode` object.
321321
*/
322322
getSourceCodeIdentifiers (context, ast) {
323-
return new Set(Array.from(module.exports.getContextIdentifiers(context, ast))
323+
return new Set([...module.exports.getContextIdentifiers(context, ast)]
324324
.filter(identifier => identifier.parent &&
325325
identifier.parent.type === 'MemberExpression' &&
326326
identifier === identifier.parent.object &&
@@ -333,9 +333,9 @@ module.exports = {
333333
identifier.parent.parent.parent.id.type === 'Identifier'
334334
)
335335
.map(identifier => context.getDeclaredVariables(identifier.parent.parent.parent))
336-
.reduce((allVariables, variablesForIdentifier) => allVariables.concat(variablesForIdentifier), [])
336+
.reduce((allVariables, variablesForIdentifier) => [...allVariables, ...variablesForIdentifier], [])
337337
.map(variable => variable.references)
338-
.reduce((allRefs, refsForVariable) => allRefs.concat(refsForVariable), [])
338+
.reduce((allRefs, refsForVariable) => [...allRefs, ...refsForVariable], [])
339339
.map(ref => ref.identifier));
340340
},
341341

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"eslint-config-not-an-aardvark": "^2.1.0",
3939
"eslint-plugin-node": "^11.1.0",
4040
"eslint-plugin-self": "^1.2.1",
41+
"eslint-plugin-unicorn": "^31.0.0",
4142
"eslint-scope": "^5.1.1",
4243
"espree": "^7.3.0",
4344
"estraverse": "^5.0.0",

tests/lib/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
const assert = require('chai').assert;
44
const plugin = require('../..');
55

6+
const RULE_NAMES = Object.keys(plugin.rules);
7+
68
describe('exported plugin', () => {
79
describe('adds a meta.docs.url property to each rule', () => {
8-
Object.keys(plugin.rules).forEach(ruleName => {
10+
RULE_NAMES.forEach(ruleName => {
911
it(ruleName, () => {
1012
assert.match(
1113
plugin.rules[ruleName].meta.docs.url,

tests/lib/utils.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const util = require('util');
3+
const { inspect } = require('util');
44
const lodash = require('lodash');
55
const espree = require('espree');
66
const eslintScope = require('eslint-scope');
@@ -118,7 +118,7 @@ describe('utils', () => {
118118
const ruleInfo = utils.getRuleInfo({ ast });
119119
assert(
120120
lodash.isMatch(ruleInfo, CASES[ruleSource]),
121-
`Expected \n${util.inspect(ruleInfo)}\nto match\n${util.inspect(CASES[ruleSource])}`
121+
`Expected \n${inspect(ruleInfo)}\nto match\n${inspect(CASES[ruleSource])}`
122122
);
123123
});
124124
});
@@ -143,7 +143,7 @@ describe('utils', () => {
143143
const ruleInfo = utils.getRuleInfo({ ast, scopeManager });
144144
assert(
145145
lodash.isMatch(ruleInfo, expected),
146-
`Expected \n${util.inspect(ruleInfo)}\nto match\n${util.inspect(expected)}`
146+
`Expected \n${inspect(ruleInfo)}\nto match\n${inspect(expected)}`
147147
);
148148
});
149149
}
@@ -178,7 +178,7 @@ describe('utils', () => {
178178
const identifiers = utils.getContextIdentifiers(scope, ast);
179179

180180
assert(identifiers instanceof Set, 'getContextIdentifiers should return a Set');
181-
Array.from(identifiers).forEach((identifier, index) => {
181+
[...identifiers].forEach((identifier, index) => {
182182
assert.strictEqual(identifier, CASES[ruleSource](ast)[index]);
183183
});
184184
});

0 commit comments

Comments
 (0)