Skip to content

Add eslint-plugin-unicorn #133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ module.exports = {
'not-an-aardvark/node',
'plugin:node/recommended',
'plugin:self/all',
'plugin:unicorn/recommended',
],
parserOptions: {
ecmaVersion: 2021,
sourceType: 'script',
},
rules: {
'comma-dangle': [
'error',
Expand All @@ -22,6 +27,14 @@ module.exports = {
'self/meta-property-ordering': 'off',
'self/require-meta-docs-url': 'off',
'self/report-message-format': ['error', '^[^a-z].*.$'],

'unicorn/consistent-function-scoping': 'off',
'unicorn/no-array-callback-reference': 'off',
'unicorn/no-array-for-each': 'off',
'unicorn/no-array-reduce': 'off',
'unicorn/no-null': 'off',
'unicorn/prefer-module': 'off',
'unicorn/prevent-abbreviations': 'off',
},
overrides: [
{
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/consistent-output.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ module.exports = {
utils.getTestInfo(context, ast).forEach(testRun => {
const readableCases = testRun.invalid.filter(testCase => testCase.type === 'ObjectExpression');
const casesWithoutOutput = readableCases
.filter(testCase => testCase.properties.map(utils.getKeyName).indexOf('output') === -1);
.filter(testCase => !testCase.properties.map(utils.getKeyName).includes('output'));

if (
(casesWithoutOutput.length < readableCases.length) ||
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/meta-property-ordering.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ module.exports = {
const violatingProps = props.filter(prop => {
const curr = orderMap.has(getKeyName(prop))
? orderMap.get(getKeyName(prop))
: Infinity;
: Number.POSITIVE_INFINITY;
return last > (last = curr);
});

Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-deprecated-context-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ module.exports = {

return {
'Program:exit' () {
Array.from(utils.getContextIdentifiers(context, sourceCode.ast))
[...utils.getContextIdentifiers(context, sourceCode.ast)]
.filter(
contextId =>
contextId.parent.type === 'MemberExpression' &&
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/no-identical-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ module.exports = {
propertiesSetA.add(code);
});

for (let i = 0; i < propertiesB.length; i++) {
const code = sourceCode.getText(propertiesB[i]);
for (const element of propertiesB) {
const code = sourceCode.getText(element);
if (!propertiesSetA.has(code)) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-missing-placeholders.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ module.exports = {
) {
// Same regex as the one ESLint uses
// https://github.com/eslint/eslint/blob/e5446449d93668ccbdb79d78cc69f165ce4fde07/lib/eslint.js#L990
const PLACEHOLDER_MATCHER = /\{\{\s*([^{}]+?)\s*\}\}/g;
const PLACEHOLDER_MATCHER = /{{\s*([^{}]+?)\s*}}/g;
let match;

while ((match = PLACEHOLDER_MATCHER.exec(reportInfo.message.value || messageStaticValue.value))) { // eslint-disable-line no-extra-parens
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-unused-placeholders.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ module.exports = {
) {
const message = reportInfo.message.value || messageStaticValue.value;
// https://github.com/eslint/eslint/blob/2874d75ed8decf363006db25aac2d5f8991bd969/lib/linter.js#L986
const PLACEHOLDER_MATCHER = /\{\{\s*([^{}]+?)\s*\}\}/g;
const PLACEHOLDER_MATCHER = /{{\s*([^{}]+?)\s*}}/g;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a false negative of the rule no-useless-escape?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it, unicorn/better-regex applies a few more plugins to cleanup the regexp.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you file the issue in eslint repo, to make sure we don't lost track of it? thanks!

const placeholdersInMessage = new Set();

message.replace(PLACEHOLDER_MATCHER, (fullMatch, term) => {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-useless-token-range.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ module.exports = {

return {
'Program:exit' (ast) {
Array.from(utils.getSourceCodeIdentifiers(context, ast))
[...utils.getSourceCodeIdentifiers(context, ast)]
.filter(identifier => identifier.parent.type === 'MemberExpression' &&
identifier.parent.object === identifier &&
identifier.parent.property.type === 'Identifier' &&
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/prefer-output-null.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ module.exports = {
function getTestInfo (key) {
if (test.type === 'ObjectExpression') {
const res = test.properties.filter(item => item.key.name === key);
return res.length ? res[res.length - 1] : null;
return res.length > 0 ? res[res.length - 1] : null;
}
return key === 'code' ? test : null;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/prefer-placeholders.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ module.exports = {
}

if (
(messageNode.type === 'TemplateLiteral' && messageNode.expressions.length) ||
(messageNode.type === 'TemplateLiteral' && messageNode.expressions.length > 0) ||
(messageNode.type === 'BinaryExpression' && messageNode.operator === '+')
) {
context.report({
Expand Down
1 change: 1 addition & 0 deletions lib/rules/require-meta-docs-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ module.exports = {

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

Expand Down
1 change: 1 addition & 0 deletions lib/rules/require-meta-fixable.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ module.exports = {
const VALID_VALUES = new Set(['code', 'whitespace', null, undefined]);
const valueIsValid = metaFixableProp.value.type === 'Literal'
? VALID_VALUES.has(metaFixableProp.value.value)
// eslint-disable-next-line unicorn/no-nested-ternary
: metaFixableProp.value.type === 'TemplateLiteral' && metaFixableProp.value.quasis.length === 1
? VALID_VALUES.has(metaFixableProp.value.quasis[0].value.cooked)
: metaFixableProp.value.type === 'Identifier' && metaFixableProp.value.name === 'undefined';
Expand Down
6 changes: 2 additions & 4 deletions lib/rules/test-case-property-ordering.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,8 @@ module.exports = {
// current < lastChecked to catch unordered;
// and lastChecked === -1 to catch extra properties before.
if (current > -1 && (current < lastChecked || lastChecked === -1)) {
let orderMsg = order.filter(item => keyNames.indexOf(item) > -1);
orderMsg = orderMsg.concat(
lastChecked === -1 ? keyNames.filter(item => order.indexOf(item) === -1) : []
);
let orderMsg = order.filter(item => keyNames.includes(item));
orderMsg = [...orderMsg, ...lastChecked === -1 ? keyNames.filter(item => !order.includes(item)) : []];

context.report({
node: properties[i],
Expand Down
12 changes: 6 additions & 6 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function isNormalFunctionExpressionReference (node, scopeManager) {
}

const definitions = createReference.resolved.defs;
if (!definitions || !definitions.length) {
if (!definitions || definitions.length === 0) {
return false;
}

Expand Down Expand Up @@ -175,7 +175,7 @@ module.exports = {
getContextIdentifiers (context, ast) {
const ruleInfo = module.exports.getRuleInfo({ ast });

if (!ruleInfo || !ruleInfo.create.params.length || ruleInfo.create.params[0].type !== 'Identifier') {
if (!ruleInfo || ruleInfo.create.params.length === 0 || ruleInfo.create.params[0].type !== 'Identifier') {
return new Set();
}

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

if (!reportArgs.length) {
if (reportArgs.length === 0) {
return null;
}

Expand Down Expand Up @@ -320,7 +320,7 @@ module.exports = {
* @returns {Set<ASTNode>} A set of all identifiers referring to the `SourceCode` object.
*/
getSourceCodeIdentifiers (context, ast) {
return new Set(Array.from(module.exports.getContextIdentifiers(context, ast))
return new Set([...module.exports.getContextIdentifiers(context, ast)]
.filter(identifier => identifier.parent &&
identifier.parent.type === 'MemberExpression' &&
identifier === identifier.parent.object &&
Expand All @@ -333,9 +333,9 @@ module.exports = {
identifier.parent.parent.parent.id.type === 'Identifier'
)
.map(identifier => context.getDeclaredVariables(identifier.parent.parent.parent))
.reduce((allVariables, variablesForIdentifier) => allVariables.concat(variablesForIdentifier), [])
.reduce((allVariables, variablesForIdentifier) => [...allVariables, ...variablesForIdentifier], [])
.map(variable => variable.references)
.reduce((allRefs, refsForVariable) => allRefs.concat(refsForVariable), [])
.reduce((allRefs, refsForVariable) => [...allRefs, ...refsForVariable], [])
.map(ref => ref.identifier));
},

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"eslint-config-not-an-aardvark": "^2.1.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-self": "^1.2.1",
"eslint-plugin-unicorn": "^31.0.0",
"eslint-scope": "^5.1.1",
"espree": "^7.3.0",
"estraverse": "^5.0.0",
Expand Down
4 changes: 3 additions & 1 deletion tests/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
const assert = require('chai').assert;
const plugin = require('../..');

const RULE_NAMES = Object.keys(plugin.rules);

describe('exported plugin', () => {
describe('adds a meta.docs.url property to each rule', () => {
Object.keys(plugin.rules).forEach(ruleName => {
RULE_NAMES.forEach(ruleName => {
it(ruleName, () => {
assert.match(
plugin.rules[ruleName].meta.docs.url,
Expand Down
8 changes: 4 additions & 4 deletions tests/lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const util = require('util');
const { inspect } = require('util');
const lodash = require('lodash');
const espree = require('espree');
const eslintScope = require('eslint-scope');
Expand Down Expand Up @@ -118,7 +118,7 @@ describe('utils', () => {
const ruleInfo = utils.getRuleInfo({ ast });
assert(
lodash.isMatch(ruleInfo, CASES[ruleSource]),
`Expected \n${util.inspect(ruleInfo)}\nto match\n${util.inspect(CASES[ruleSource])}`
`Expected \n${inspect(ruleInfo)}\nto match\n${inspect(CASES[ruleSource])}`
);
});
});
Expand All @@ -143,7 +143,7 @@ describe('utils', () => {
const ruleInfo = utils.getRuleInfo({ ast, scopeManager });
assert(
lodash.isMatch(ruleInfo, expected),
`Expected \n${util.inspect(ruleInfo)}\nto match\n${util.inspect(expected)}`
`Expected \n${inspect(ruleInfo)}\nto match\n${inspect(expected)}`
);
});
}
Expand Down Expand Up @@ -178,7 +178,7 @@ describe('utils', () => {
const identifiers = utils.getContextIdentifiers(scope, ast);

assert(identifiers instanceof Set, 'getContextIdentifiers should return a Set');
Array.from(identifiers).forEach((identifier, index) => {
[...identifiers].forEach((identifier, index) => {
assert.strictEqual(identifier, CASES[ruleSource](ast)[index]);
});
});
Expand Down