Skip to content

Commit

Permalink
review eslint config: enable some rules from unicorn, refactor so…
Browse files Browse the repository at this point in the history
…me sections
  • Loading branch information
zloirock committed Sep 15, 2023
1 parent 4f6e40d commit 6dae757
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 14 deletions.
1 change: 1 addition & 0 deletions tests/compat/node-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ require('./compat-data');
require('./common-runner');

if (process.argv.indexOf('json') !== -1) {
// eslint-disable-next-line es/no-json -- safe
console.log(JSON.stringify(global.results, null, ' '));
} else global.showResults('node', console.log);
52 changes: 39 additions & 13 deletions tests/eslint/eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,10 @@ const base = {
resolvePattern: '^resolve',
rejectPattern: '^reject',
}],
// prefer `async` / `await` to the callback pattern
'promise/prefer-await-to-callbacks': ERROR,
// prefer `await` to `then()` / `catch()` / `finally()` for reading `Promise` values
'promise/prefer-await-to-then': ERROR,
// ensures the proper number of arguments are passed to `Promise` functions
'promise/valid-params': ERROR,

Expand Down Expand Up @@ -612,8 +616,16 @@ const base = {
'unicorn/prefer-object-from-entries': ERROR,
// prefer omitting the `catch` binding parameter
'unicorn/prefer-optional-catch-binding': ERROR,
// prefer `RegExp#test()` over `String#match()` and `RegExp#exec()`
'unicorn/prefer-regexp-test': ERROR,
// prefer using `Set#size` instead of `Array#length`
'unicorn/prefer-set-size': ERROR,
// prefer `String#replaceAll()` over regex searches with the global flag
'unicorn/prefer-string-replace-all': ERROR,
// prefer `String#{ startsWith, endsWith }()` over RegExp#test()
'unicorn/prefer-string-starts-ends-with': ERROR,
// prefer `String#{ trimStart, trimEnd }()` over `String#{ trimLeft, trimRight }()`
'unicorn/prefer-string-trim-start-end': ERROR,
// prefer `switch` over multiple `else-if`
'unicorn/prefer-switch': [ERROR, { minimumCases: 3 }],
// enforce consistent relative `URL` style
Expand All @@ -626,6 +638,8 @@ const base = {
'unicorn/require-post-message-target-origin': ERROR,
// forbid braces for case clauses
'unicorn/switch-case-braces': [ERROR, 'avoid'],
// fix whitespace-insensitive template indentation
'unicorn/template-indent': OFF, // waiting for `String.dedent`
// enforce consistent case for text encoding identifiers
'unicorn/text-encoding-identifier-case': ERROR,
// require `new` when throwing an error
Expand Down Expand Up @@ -834,7 +848,15 @@ const base = {
'eslint-comments/require-description': ERROR,
};

const es3 = {
const noAsyncAwait = {
// prefer `async` / `await` to the callback pattern
'promise/prefer-await-to-callbacks': OFF,
// prefer `await` to `then()` / `catch()` / `finally()` for reading `Promise` values
'promise/prefer-await-to-then': OFF,
};

const useES3Syntax = {
...noAsyncAwait,
// disallow trailing commas in multiline object literals
'comma-dangle': [ERROR, NEVER],
// encourages use of dot notation whenever possible
Expand All @@ -859,8 +881,6 @@ const es3 = {
'prefer-exponentiation-operator': OFF,
// enforce using named capture group in regular expression
'prefer-named-capture-group': OFF,
// prefer `Object.hasOwn`
'prefer-object-has-own': OFF,
// require rest parameters instead of `arguments`
'prefer-rest-params': OFF,
// require spread operators instead of `.apply()`
Expand Down Expand Up @@ -980,6 +1000,8 @@ const forbidES2015BuiltIns = {
'es/no-typed-arrays': ERROR,
'es/no-weak-map': ERROR,
'es/no-weak-set': ERROR,
// prefer `String#{ startsWith, endsWith }()` over RegExp#test()
'unicorn/prefer-string-starts-ends-with': OFF,
};

const forbidES2016BuiltIns = {
Expand Down Expand Up @@ -1010,6 +1032,8 @@ const forbidES2019BuiltIns = {
'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,
// prefer `String#{ trimStart, trimEnd }()` over `String#{ trimLeft, trimRight }()`
'unicorn/prefer-string-trim-start-end': OFF,
};

const forbidES2020BuiltIns = {
Expand All @@ -1025,9 +1049,13 @@ const forbidES2021BuiltIns = {
'es/no-regexp-unicode-property-escapes-2021': ERROR,
'es/no-string-prototype-replaceall': ERROR,
'es/no-weakrefs': ERROR,
// prefer `String#replaceAll()` over regex searches with the global flag
'unicorn/prefer-string-replace-all': OFF,
};

const forbidES2022BuiltIns = {
// prefer `Object.hasOwn`
'prefer-object-has-own': OFF,
'es/no-array-string-prototype-at': ERROR,
'es/no-error-cause': ERROR,
'es/no-object-hasown': ERROR,
Expand Down Expand Up @@ -1109,19 +1137,16 @@ const forbidModernESBuiltIns = {
...forbidES2023IntlBuiltIns,
};

const asyncAwait = {
// prefer `async` / `await` to the callback pattern
'promise/prefer-await-to-callbacks': ERROR,
// prefer `await` to `then()` / `catch()` / `finally()` for reading `Promise` values
'promise/prefer-await-to-then': ERROR,
};

const polyfills = {
// avoid nested `then()` or `catch()` statements
'promise/no-nesting': OFF,
// prefer `RegExp#test()` over `String#match()` and `RegExp#exec()`
// use `RegExp#exec()` since it does not have implicit calls under the hood
'unicorn/prefer-regexp-test': OFF,
};

const transpiledAndPolyfilled = {
...noAsyncAwait,
// disallow accessor properties
'es/no-accessor-properties': ERROR,
// disallow async functions
Expand All @@ -1145,7 +1170,6 @@ const transpiledAndPolyfilled = {
};

const nodePackages = {
...asyncAwait,
// disallow logical assignment operator shorthand
'logical-assignment-operators': [ERROR, NEVER],
// enforce using named capture group in regular expression
Expand All @@ -1171,6 +1195,8 @@ const nodePackages = {
...disable(forbidES2017BuiltIns),
'es/no-atomics': ERROR,
'es/no-shared-array-buffer': ERROR,
// disallow top-level `await`
'es/no-top-level-await': ERROR,
...forbidES2018BuiltIns,
...forbidES2019BuiltIns,
...forbidES2020BuiltIns,
Expand All @@ -1188,7 +1214,6 @@ const nodePackages = {
};

const nodeDev = {
...asyncAwait,
// disallow unsupported ECMAScript built-ins on the specified version
'node/no-unsupported-features/node-builtins': [ERROR, { version: DEV_NODE_VERSIONS }],
...disable(forbidModernESBuiltIns),
Expand Down Expand Up @@ -1493,12 +1518,13 @@ module.exports = [
languageOptions: {
ecmaVersion: 3,
},
rules: es3,
rules: useES3Syntax,
},
{
files: [
'packages/core-js?(-pure)/**',
'tests/@(unit-pure|worker)/**',
'tests/compat/@(browsers|hermes|node|rhino)-runner.js',
],
rules: forbidModernESBuiltIns,
},
Expand Down
1 change: 1 addition & 0 deletions tests/unit-global/es.string.replace-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ QUnit.test('String#replaceAll', assert => {

// eslint-disable-next-line regexp/no-missing-g-flag -- required for testing
assert.throws(() => 'b.b.b.b.b'.replaceAll(/\./, 'a'), TypeError);
// eslint-disable-next-line unicorn/prefer-string-replace-all -- required for testing
assert.same('b.b.b.b.b'.replaceAll(/\./g, 'a'), 'babababab');
const object = {};
assert.same('[object Object]'.replaceAll(object, 'a'), 'a');
Expand Down
2 changes: 1 addition & 1 deletion tests/unit-global/es.string.replace.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* eslint-disable prefer-regex-literals, regexp/no-unused-capturing-group -- required for testing */
/* eslint-disable prefer-regex-literals, regexp/no-unused-capturing-group, unicorn/prefer-string-replace-all -- required for testing */
import { GLOBAL, NATIVE, STRICT } from '../helpers/constants.js';
import { patchRegExp$exec } from '../helpers/helpers.js';

Expand Down
1 change: 1 addition & 0 deletions tests/unit-global/es.string.trim-left.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable unicorn/prefer-string-trim-start-end -- required for testing */
import { STRICT, WHITESPACES } from '../helpers/constants.js';

QUnit.test('String#trimLeft', assert => {
Expand Down
1 change: 1 addition & 0 deletions tests/unit-global/es.string.trim-right.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable unicorn/prefer-string-trim-start-end -- required for testing */
import { STRICT, WHITESPACES } from '../helpers/constants.js';

QUnit.test('String#trimRight', assert => {
Expand Down

0 comments on commit 6dae757

Please sign in to comment.