Skip to content

Commit

Permalink
review eslint config: enable some rules from unicorn, drop some d…
Browse files Browse the repository at this point in the history
…eprecated rules and duplicates
  • Loading branch information
zloirock committed Sep 14, 2023
1 parent c10be6d commit 6893695
Show file tree
Hide file tree
Showing 15 changed files with 76 additions and 45 deletions.
1 change: 1 addition & 0 deletions packages/core-js/postinstall.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';
/* eslint-disable node/no-sync -- avoiding overcomplicating */
/* eslint-disable unicorn/prefer-node-protocol -- ancient env possible */
var fs = require('fs');
var os = require('os');
var path = require('path');
Expand Down
3 changes: 2 additions & 1 deletion scripts/check-unused-modules.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import konan from 'konan';
import { modules, ignored } from 'core-js-compat/src/data.mjs';

async function jsModulesFrom(path) {
return new Set((await fs.readdir(path)).filter(it => it.endsWith('.js')).map(it => it.slice(0, -3)));
const directory = await fs.readdir(path);
return new Set(directory.filter(it => it.endsWith('.js')).map(it => it.slice(0, -3)));
}

function log(set, kind) {
Expand Down
90 changes: 59 additions & 31 deletions tests/eslint/eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -493,10 +493,6 @@ const base = {
'array-func/from-map': ERROR,
// avoid the `this` parameter when providing arrow function as callback in array functions
'array-func/no-unnecessary-this-arg': ERROR,
// use `.flatMap()`` to map and then flatten an array instead of using `.map().flat()`
'array-func/prefer-flat-map': ERROR,
// use `.flat()` to flatten an array of arrays
'array-func/prefer-flat': ERROR,

// promise:
// avoid calling `cb()` inside of a `then()` or `catch()`
Expand All @@ -522,17 +518,17 @@ const base = {
'promise/valid-params': ERROR,

// unicorn
// enforce a specific parameter name in catch clauses
// enforce a specific parameter name in `catch` clauses
'unicorn/catch-error-name': [ERROR, { name: ERROR, ignore: [/^err/] }],
// enforce correct `Error` subclassing
'unicorn/custom-error-definition': ERROR,
// enforce passing a message value when throwing a built-in error
'unicorn/error-message': ERROR,
// require escape sequences to use uppercase values
'unicorn/escape-case': ERROR,
// enforce a case style for filenames
'unicorn/filename-case': [ERROR, { case: 'kebabCase' }],
// enforce importing index files with `.`
'unicorn/import-index': ERROR,
// enforce specifying rules to disable in eslint-disable comments
// enforce specifying rules to disable in `eslint-disable` comments
'unicorn/no-abusive-eslint-disable': ERROR,
// enforce combining multiple `Array#push` into one call
'unicorn/no-array-push-push': ERROR,
Expand All @@ -544,12 +540,16 @@ const base = {
'unicorn/no-invalid-remove-event-listener': ERROR,
// disallow `if` statements as the only statement in `if` blocks without `else`
'unicorn/no-lonely-if': ERROR,
// enforce the use of `Buffer.from()` and `Buffer.alloc()` instead of the deprecated `new Buffer()`
'unicorn/no-new-buffer': ERROR,
// forbid classes that only have static members
'unicorn/no-static-only-class': ERROR,
// disallow `then` property
'unicorn/no-thenable': ERROR,
// disallow comparing `undefined` using `typeof` when it's not required
'unicorn/no-typeof-undefined': ERROR,
// disallow awaiting non-promise values
'unicorn/no-unnecessary-await': ERROR,
// disallow unreadable array destructuring
'unicorn/no-unreadable-array-destructuring': ERROR,
// disallow unreadable IIFEs
Expand All @@ -564,33 +564,55 @@ const base = {
'unicorn/no-useless-promise-resolve-reject': ERROR,
// disallow useless spread
'unicorn/no-useless-spread': ERROR,
// disallow useless case in switch statements
// disallow useless `case` in `switch` statements
'unicorn/no-useless-switch-case': ERROR,
// enforce lowercase identifier and uppercase value for number literals
'unicorn/number-literal-case': ERROR,
// prefer `.find(…)` over the first element from `.filter(…)`
// enforce the style of numeric separators by correctly grouping digits
'unicorn/numeric-separators-style': [ERROR, {
onlyIfContainsSeparator: true,
number: { minimumDigits: 0, groupLength: 3 },
binary: { minimumDigits: 0, groupLength: 4 },
octal: { minimumDigits: 0, groupLength: 4 },
hexadecimal: { minimumDigits: 0, groupLength: 2 },
}],
// prefer `.find()` over the first element from `.filter()`
'unicorn/prefer-array-find': ERROR,
// use `.flat()` to flatten an array of arrays
'unicorn/prefer-array-flat': ERROR,
// use `.flatMap()` to map and then flatten an array instead of using `.map().flat()`
'unicorn/prefer-array-flat-map': ERROR,
// prefer `Array#indexOf` over `Array#findIndex`` when looking for the index of an item
'unicorn/prefer-array-index-of': ERROR,
// prefer `.some()` over `.filter().length` check and `.find()`
// prefer `.some()` over `.filter().length` check and `.find()`
'unicorn/prefer-array-some': ERROR,
// prefer `.at()` method for index access and `String#charAt()`
'unicorn/prefer-at': [ERROR, { checkAllIndexAccess: false }],
// prefer `Blob#{ arrayBuffer, text }` over `FileReader#{ readAsArrayBuffer, readAsText }`
'unicorn/prefer-blob-reading-methods': ERROR,
// prefer `Date.now()` to get the number of milliseconds since the Unix Epoch
'unicorn/prefer-date-now': ERROR,
// prefer default parameters over reassignment
'unicorn/prefer-default-parameters': ERROR,
// prefer `EventTarget` over `EventEmitter`
'unicorn/prefer-event-target': ERROR,
// prefer `.includes()` over `.indexOf()` and `Array#some()` when checking for existence or non-existence
'unicorn/prefer-includes': ERROR,
// prefer reading a `JSON` file as a buffer
'unicorn/prefer-json-parse-buffer': ERROR,
// prefer using a logical operator over a ternary
'unicorn/prefer-logical-operator-over-ternary': ERROR,
// prefer modern `Math`` APIs over legacy patterns
// prefer modern `Math` APIs over legacy patterns
'unicorn/prefer-modern-math-apis': ERROR,
// prefer using Set#size instead of Array#length
// prefer negative index over `.length - index` when possible
'unicorn/prefer-negative-index': ERROR,
// prefer using the `node:` protocol when importing Node builtin modules
'unicorn/prefer-node-protocol': ERROR,
// prefer using `Object.fromEntries()` to transform a list of key-value pairs into an object
'unicorn/prefer-object-from-entries': ERROR,
// prefer omitting the `catch` binding parameter
'unicorn/prefer-optional-catch-binding': ERROR,
// prefer using `Set#size` instead of `Array#length`
'unicorn/prefer-set-size': ERROR,
// prefer `switch` over multiple `else-if`
'unicorn/prefer-switch': [ERROR, { minimumCases: 3 }],
Expand Down Expand Up @@ -845,12 +867,12 @@ const es3 = {
'prefer-template': OFF,
// require or disallow use of quotes around object literal property names
'quote-props': [ERROR, 'as-needed', { keywords: true }],
// use `.flat()` to flatten an array of arrays
'array-func/prefer-flat': OFF,
// prefer default parameters over reassignment
'unicorn/prefer-default-parameters': OFF,
// prefer using a logical operator over a ternary
'unicorn/prefer-logical-operator-over-ternary': OFF,
// prefer omitting the `catch` binding parameter
'unicorn/prefer-optional-catch-binding': OFF,
};

const forbidESAnnexBBuiltIns = {
Expand Down Expand Up @@ -892,6 +914,10 @@ const forbidES5BuiltIns = {
'es/no-object-preventextensions': ERROR,
'es/no-object-seal': ERROR,
'es/no-string-prototype-trim': ERROR,
// prefer `Date.now()` to get the number of milliseconds since the Unix Epoch
'unicorn/prefer-date-now': OFF,
// prefer modern `Math` APIs over legacy patterns
'unicorn/prefer-modern-math-apis': OFF,
};

const forbidES2015BuiltIns = {
Expand Down Expand Up @@ -956,6 +982,8 @@ const forbidES2015BuiltIns = {

const forbidES2016BuiltIns = {
'es/no-array-prototype-includes': ERROR,
// prefer `.includes()` over `.indexOf()` and `Array#some()` when checking for existence or non-existence
'unicorn/prefer-includes': OFF,
};

const forbidES2017BuiltIns = {
Expand All @@ -972,11 +1000,14 @@ const forbidES2018BuiltIns = {
};

const forbidES2019BuiltIns = {
'unicorn/prefer-array-flat': OFF,
'es/no-array-prototype-flat': ERROR,
'es/no-object-fromentries': ERROR,
'es/no-string-prototype-trimstart-trimend': ERROR,
'es/no-symbol-prototype-description': ERROR,
// use `.flat()` to flatten an array of arrays
'unicorn/prefer-array-flat': OFF,
// prefer using `Object.fromEntries()` to transform a list of key-value pairs into an object
'unicorn/prefer-object-from-entries': OFF,
};

const forbidES2020BuiltIns = {
Expand All @@ -1000,6 +1031,8 @@ const forbidES2022BuiltIns = {
'es/no-object-hasown': ERROR,
'es/no-regexp-d-flag': ERROR,
'es/no-regexp-unicode-property-escapes-2022': ERROR,
// prefer `.at()` method for index access and `String#charAt()`
'unicorn/prefer-at': OFF,
};

const forbidES2023BuiltIns = {
Expand Down Expand Up @@ -1079,8 +1112,6 @@ const asyncAwait = {
'promise/prefer-await-to-callbacks': ERROR,
// prefer `await` to `then()` / `catch()` / `finally()` for reading `Promise` values
'promise/prefer-await-to-then': ERROR,
// disallow awaiting non-promise values
'unicorn/no-unnecessary-await': ERROR,
};

const polyfills = {
Expand Down Expand Up @@ -1111,19 +1142,12 @@ const transpiledAndPolyfilled = {
'es/no-weakrefs': ERROR,
};

const testsWithoutPolyfills = {
// use `.flat()` to flatten an array of arrays
'array-func/prefer-flat': OFF,
};

const nodePackages = {
...asyncAwait,
// disallow logical assignment operator shorthand
'logical-assignment-operators': [ERROR, NEVER],
// enforce using named capture group in regular expression
'prefer-named-capture-group': OFF,
// use `.flat()` to flatten an array of arrays
'array-func/prefer-flat': OFF,
// enforces the use of `catch()` on un-returned promises
'promise/catch-or-return': ERROR,
// disallow third-party modules which are hiding core modules
Expand All @@ -1133,6 +1157,12 @@ const nodePackages = {
// prefer promises
'node/prefer-promises/dns': OFF,
'node/prefer-promises/fs': OFF,
// prefer using a logical operator over a ternary
'unicorn/prefer-logical-operator-over-ternary': OFF,
// prefer using the `node:` protocol when importing Node builtin modules
'unicorn/prefer-node-protocol': OFF,
// prefer omitting the `catch` binding parameter
'unicorn/prefer-optional-catch-binding': OFF,
...disable(forbidES5BuiltIns),
...disable(forbidES2015BuiltIns),
...disable(forbidES2016BuiltIns),
Expand Down Expand Up @@ -1206,6 +1236,10 @@ const tests = {
'prefer-named-capture-group': OFF,
// enforce passing a message value when throwing a built-in error
'unicorn/error-message': OFF,
// prefer `.at()` method for index access and `String#charAt()`
'unicorn/prefer-at': OFF,
// prefer `.includes()` over `.indexOf()` and `Array#some()` when checking for existence or non-existence
'unicorn/prefer-includes': OFF,
// ReDoS vulnerability check
'redos/no-vulnerable': OFF,
// allow Annex B methods for testing
Expand Down Expand Up @@ -1487,12 +1521,6 @@ module.exports = [
},
rules: transpiledAndPolyfilled,
},
{
files: [
'tests/@(compat|helpers|unit-pure)/**',
],
rules: testsWithoutPolyfills,
},
{
files: [
'tests/**',
Expand Down
2 changes: 1 addition & 1 deletion tests/helpers/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ try {
// Chrome 27- bug, also a bug for native `JSON.parse`
defineProperty({}, '__proto__', { value: 42, writable: true, configurable: true, enumerable: true });
REDEFINABLE_PROTO = true;
} catch (error) { /* empty */ }
} catch { /* empty */ }

export const STRICT_THIS = (function () {
return this;
Expand Down
1 change: 1 addition & 0 deletions tests/unit-global/es.date.now.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ QUnit.test('Date.now', assert => {
assert.name(now, 'now');
assert.looksNative(now);
assert.nonEnumerable(Date, 'now');
// eslint-disable-next-line unicorn/prefer-date-now -- required for testing
assert.epsilon(+new Date(), now(), 10, 'Date.now() ~ +new Date');
});
2 changes: 1 addition & 1 deletion tests/unit-global/es.promise.all.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ QUnit.test('Promise.all', assert => {
done = true;
},
})).catch(() => { /* empty */ });
} catch (error) { /* empty */ }
} catch { /* empty */ }
Promise.resolve = resolve;
assert.true(done, 'iteration closing');
FakePromise1 = function (executor) {
Expand Down
2 changes: 1 addition & 1 deletion tests/unit-global/es.promise.race.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ QUnit.test('Promise.race', assert => {
done = true;
},
})).catch(() => { /* empty */ });
} catch (error) { /* empty */ }
} catch { /* empty */ }
Promise.resolve = resolve;
assert.true(done, 'iteration closing');
FakePromise1 = function (executor) {
Expand Down
4 changes: 2 additions & 2 deletions tests/unit-global/es.regexp.dot-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ if (DESCRIPTORS) {
try {
dotAllGetter.call(/a/);
assert.required('.dotAll getter works on literals');
} catch (error) {
} catch {
assert.avoid('.dotAll getter works on literals');
}
try {
dotAllGetter.call(new RegExp('a'));
assert.required('.dotAll getter works on instances');
} catch (error) {
} catch {
assert.avoid('.dotAll getter works on instances');
}

Expand Down
2 changes: 1 addition & 1 deletion tests/unit-global/es.regexp.flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ if (DESCRIPTORS) {
let INDICES_SUPPORT = true;
try {
RegExp('.', 'd');
} catch (error) {
} catch {
INDICES_SUPPORT = false;
}

Expand Down
4 changes: 2 additions & 2 deletions tests/unit-global/es.regexp.sticky.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ if (DESCRIPTORS) {
try {
stickyGetter.call(/a/);
assert.required('.sticky getter works on literals');
} catch (error) {
} catch {
assert.avoid('.sticky getter works on literals');
}
try {
stickyGetter.call(new RegExp('a'));
assert.required('.sticky getter works on instances');
} catch (error) {
} catch {
assert.avoid('.sticky getter works on instances');
}

Expand Down
2 changes: 1 addition & 1 deletion tests/unit-global/esnext.array-buffer.detached.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ QUnit.test('ArrayBuffer#detached', assert => {
const detached = new ArrayBuffer(8);
try {
structuredClone(detached, { transfer: [detached] });
} catch (error) { /* empty */ }
} catch { /* empty */ }

if (detached.length === 0) {
assert.same(detached.detached, true, 'detached');
Expand Down
2 changes: 1 addition & 1 deletion tests/unit-global/esnext.function.is-constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ QUnit.test('Function.isConstructor', assert => {
// V8 ~ Chrome 49- bug
if (Gen) try {
new Gen();
} catch (error) {
} catch {
assert.false(isConstructor(Gen), 'gen');
}
const asyncFunc = fromSource('async function () {}');
Expand Down
2 changes: 1 addition & 1 deletion tests/unit-pure/es.promise.all.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ QUnit.test('Promise.all', assert => {
done = true;
},
})).catch(() => { /* empty */ });
} catch (error) { /* empty */ }
} catch { /* empty */ }
Promise.resolve = resolve;
assert.true(done, 'iteration closing');
let FakePromise1 = function (executor) {
Expand Down
2 changes: 1 addition & 1 deletion tests/unit-pure/es.promise.race.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ QUnit.test('Promise.race', assert => {
done = true;
},
})).catch(() => { /* empty */ });
} catch (error) { /* empty */ }
} catch { /* empty */ }
Promise.resolve = resolve;
assert.true(done, 'iteration closing');
let FakePromise1 = function (executor) {
Expand Down
2 changes: 1 addition & 1 deletion tests/unit-pure/esnext.function.is-constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ QUnit.test('Function.isConstructor', assert => {
// V8 ~ Chrome 49- bug
if (Gen) try {
new Gen();
} catch (error) {
} catch {
assert.false(isConstructor(Gen), 'gen');
}
const asyncFunc = fromSource('async function () {}');
Expand Down

0 comments on commit 6893695

Please sign in to comment.