diff --git a/.eslintrc.js b/.eslintrc.js index 770ba3e138d5..90ac9b647148 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -371,8 +371,12 @@ const base = { 'unicorn/no-unused-properties': 'error', // enforce lowercase identifier and uppercase value for number literals 'unicorn/number-literal-case': 'error', + // prefer `Array#indexOf` over `Array#findIndex`` when looking for the index of an item + 'unicorn/prefer-array-index-of': 'error', // prefer default parameters over reassignment 'unicorn/prefer-default-parameters': 'error', + // prefer `RegExp#test` over `String#match` and `RegExp#exec` + 'unicorn/prefer-regexp-test': 'error', // prefer `String#slice` over `String#{ substr, substring }` 'unicorn/prefer-string-slice': 'error', diff --git a/package.json b/package.json index b7b2aff0d912..0ed5835c8b61 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,7 @@ "eslint-plugin-optimize-regex": "^1.2.0", "eslint-plugin-qunit": "^5.2.0", "eslint-plugin-sonarjs": "~0.5.0", - "eslint-plugin-unicorn": "^25.0.1", + "eslint-plugin-unicorn": "^26.0.0", "grunt": "^1.3.0", "grunt-cli": "^1.3.2", "grunt-contrib-clean": "^2.0.0", diff --git a/tests/pure/es.promise.js b/tests/pure/es.promise.js index 2728a8779620..ed63356f1a32 100644 --- a/tests/pure/es.promise.js +++ b/tests/pure/es.promise.js @@ -203,7 +203,7 @@ QUnit.test('Promise.all', assert => { let FakePromise2 = FakePromise1[Symbol.species] = function (executor) { executor(() => { /* empty */ }, () => { /* empty */ }); }; - FakePromise1.resolve = FakePromise2.resolve = bind(Promise.resolve, Promise); + FakePromise1.resolve = FakePromise2.resolve = bind(resolve, Promise); assert.ok(all.call(FakePromise1, [1, 2, 3]) instanceof FakePromise1, 'subclassing, `this` pattern'); FakePromise1 = function () { /* empty */ }; FakePromise2 = function (executor) { @@ -212,7 +212,7 @@ QUnit.test('Promise.all', assert => { const FakePromise3 = function (executor) { executor(() => { /* empty */ }, null); }; - FakePromise1.resolve = FakePromise2.resolve = FakePromise3.resolve = bind(Promise.resolve, Promise); + FakePromise1.resolve = FakePromise2.resolve = FakePromise3.resolve = bind(resolve, Promise); assert.throws(() => { all.call(FakePromise1, [1, 2, 3]); }, 'NewPromiseCapability validations, #1'); @@ -263,7 +263,7 @@ QUnit.test('Promise.race', assert => { let FakePromise2 = FakePromise1[Symbol.species] = function (executor) { executor(() => { /* empty */ }, () => { /* empty */ }); }; - FakePromise1.resolve = FakePromise2.resolve = bind(Promise.resolve, Promise); + FakePromise1.resolve = FakePromise2.resolve = bind(resolve, Promise); assert.ok(race.call(FakePromise1, [1, 2, 3]) instanceof FakePromise1, 'subclassing, `this` pattern'); FakePromise1 = function () { /* empty */ }; FakePromise2 = function (executor) { @@ -272,7 +272,7 @@ QUnit.test('Promise.race', assert => { const FakePromise3 = function (executor) { executor(() => { /* empty */ }, null); }; - FakePromise1.resolve = FakePromise2.resolve = FakePromise3.resolve = bind(Promise.resolve, Promise); + FakePromise1.resolve = FakePromise2.resolve = FakePromise3.resolve = bind(resolve, Promise); assert.throws(() => { race.call(FakePromise1, [1, 2, 3]); }, 'NewPromiseCapability validations, #1'); diff --git a/tests/tests/es.array.find-index.js b/tests/tests/es.array.find-index.js index b05982845cfa..58df51faaf8c 100644 --- a/tests/tests/es.array.find-index.js +++ b/tests/tests/es.array.find-index.js @@ -16,7 +16,9 @@ QUnit.test('Array#findIndex', assert => { assert.same(that, array, 'correct link to array in callback'); assert.same(this, context, 'correct callback context'); }, context); + // eslint-disable-next-line unicorn/prefer-array-index-of assert.same([1, 3, NaN, 42, {}].findIndex(it => it === 42), 3); + // eslint-disable-next-line unicorn/prefer-array-index-of assert.same([1, 3, NaN, 42, {}].findIndex(it => it === 43), -1); if (STRICT) { assert.throws(() => findIndex.call(null, 0), TypeError); diff --git a/tests/tests/es.promise.js b/tests/tests/es.promise.js index 4a6b5bff0fd7..4c667f6c2142 100644 --- a/tests/tests/es.promise.js +++ b/tests/tests/es.promise.js @@ -214,7 +214,7 @@ QUnit.test('Promise.all', assert => { FakePromise2 = FakePromise1[Symbol.species] = function (executor) { executor(() => { /* empty */ }, () => { /* empty */ }); }; - FakePromise1.resolve = FakePromise2.resolve = Promise.resolve.bind(Promise); + FakePromise1.resolve = FakePromise2.resolve = resolve.bind(Promise); assert.ok(all.call(FakePromise1, [1, 2, 3]) instanceof FakePromise1, 'subclassing, `this` pattern'); FakePromise1 = function () { /* empty */ }; FakePromise2 = function (executor) { @@ -223,7 +223,7 @@ QUnit.test('Promise.all', assert => { const FakePromise3 = function (executor) { executor(() => { /* empty */ }, null); }; - FakePromise1.resolve = FakePromise2.resolve = FakePromise3.resolve = Promise.resolve.bind(Promise); + FakePromise1.resolve = FakePromise2.resolve = FakePromise3.resolve = resolve.bind(Promise); assert.throws(() => { all.call(FakePromise1, [1, 2, 3]); }, 'NewPromiseCapability validations, #1'); @@ -278,7 +278,7 @@ QUnit.test('Promise.race', assert => { FakePromise2 = FakePromise1[Symbol.species] = function (executor) { executor(() => { /* empty */ }, () => { /* empty */ }); }; - FakePromise1.resolve = FakePromise2.resolve = Promise.resolve.bind(Promise); + FakePromise1.resolve = FakePromise2.resolve = resolve.bind(Promise); assert.ok(race.call(FakePromise1, [1, 2, 3]) instanceof FakePromise1, 'subclassing, `this` pattern'); FakePromise1 = function () { /* empty */ }; FakePromise2 = function (executor) { @@ -287,7 +287,7 @@ QUnit.test('Promise.race', assert => { const FakePromise3 = function (executor) { executor(() => { /* empty */ }, null); }; - FakePromise1.resolve = FakePromise2.resolve = FakePromise3.resolve = Promise.resolve.bind(Promise); + FakePromise1.resolve = FakePromise2.resolve = FakePromise3.resolve = resolve.bind(Promise); assert.throws(() => { race.call(FakePromise1, [1, 2, 3]); }, 'NewPromiseCapability validations, #1'); diff --git a/tests/tests/es.string.trim-end.js b/tests/tests/es.string.trim-end.js index 11407275bbfa..23e82016c97e 100644 --- a/tests/tests/es.string.trim-end.js +++ b/tests/tests/es.string.trim-end.js @@ -17,13 +17,13 @@ QUnit.test('String#trimRight', assert => { }); QUnit.test('String#trimEnd', assert => { - const { trimEnd } = String.prototype; + const { trimEnd, trimRight } = String.prototype; assert.isFunction(trimEnd); assert.arity(trimEnd, 0); assert.name(trimEnd, 'trimEnd'); assert.looksNative(trimEnd); assert.nonEnumerable(String.prototype, 'trimEnd'); - assert.same(trimEnd, String.prototype.trimRight, 'same #trimRight'); + assert.same(trimEnd, trimRight, 'same #trimRight'); assert.strictEqual(' \n q w e \n '.trimEnd(), ' \n q w e', 'removes whitespaces at right side of string'); assert.strictEqual(WHITESPACES.trimEnd(), '', 'removes all whitespaces'); assert.strictEqual('\u200B\u0085'.trimEnd(), '\u200B\u0085', "shouldn't remove this symbols"); diff --git a/tests/tests/es.string.trim-start.js b/tests/tests/es.string.trim-start.js index 2bfd7843f164..de3f936f4315 100644 --- a/tests/tests/es.string.trim-start.js +++ b/tests/tests/es.string.trim-start.js @@ -17,13 +17,13 @@ QUnit.test('String#trimLeft', assert => { }); QUnit.test('String#trimStart', assert => { - const { trimStart } = String.prototype; + const { trimStart, trimLeft } = String.prototype; assert.isFunction(trimStart); assert.arity(trimStart, 0); assert.name(trimStart, 'trimStart'); assert.looksNative(trimStart); assert.nonEnumerable(String.prototype, 'trimStart'); - assert.same(trimStart, String.prototype.trimLeft, 'same #trimLeft'); + assert.same(trimStart, trimLeft, 'same #trimLeft'); assert.strictEqual(' \n q w e \n '.trimStart(), 'q w e \n ', 'removes whitespaces at left side of string'); assert.strictEqual(WHITESPACES.trimStart(), '', 'removes all whitespaces'); assert.strictEqual('\u200B\u0085'.trimStart(), '\u200B\u0085', "shouldn't remove this symbols"); diff --git a/tests/tests/es.typed-array.find-index.js b/tests/tests/es.typed-array.find-index.js index 316c6951803f..f4b7ae569235 100644 --- a/tests/tests/es.typed-array.find-index.js +++ b/tests/tests/es.typed-array.find-index.js @@ -19,6 +19,7 @@ if (DESCRIPTORS) QUnit.test('%TypedArrayPrototype%.findIndex', assert => { assert.same(this, context, 'correct callback context'); }, context); assert.same(new TypedArray([1, 2, 3]).findIndex(it => !(it % 2)), 1); + // eslint-disable-next-line unicorn/prefer-array-index-of assert.same(new TypedArray([1, 2, 3]).findIndex(it => it === 4), -1); let values = ''; let keys = ''; diff --git a/tests/tests/esnext.math.deg-per-rad.js b/tests/tests/esnext.math.deg-per-rad.js index 96290ef2cc68..a3658a8d904c 100644 --- a/tests/tests/esnext.math.deg-per-rad.js +++ b/tests/tests/esnext.math.deg-per-rad.js @@ -1,6 +1,6 @@ QUnit.test('Math.DEG_PER_RAD', assert => { - const { DEG_PER_RAD } = Math; + const { DEG_PER_RAD, PI } = Math; assert.ok('DEG_PER_RAD' in Math, 'DEG_PER_RAD in Math'); assert.nonEnumerable(Math, 'DEG_PER_RAD'); - assert.strictEqual(DEG_PER_RAD, Math.PI / 180, 'Is Math.PI / 180'); + assert.strictEqual(DEG_PER_RAD, PI / 180, 'Is Math.PI / 180'); }); diff --git a/tests/tests/esnext.math.degrees.js b/tests/tests/esnext.math.degrees.js index 38aab5efb86b..b2cd1199624f 100644 --- a/tests/tests/esnext.math.degrees.js +++ b/tests/tests/esnext.math.degrees.js @@ -1,12 +1,12 @@ QUnit.test('Math.degrees', assert => { - const { degrees } = Math; + const { degrees, PI } = Math; assert.isFunction(degrees); assert.name(degrees, 'degrees'); assert.arity(degrees, 1); assert.looksNative(degrees); assert.nonEnumerable(Math, 'degrees'); assert.same(degrees(0), 0); - assert.same(degrees(Math.PI / 2), 90); - assert.same(degrees(Math.PI), 180); - assert.same(degrees(3 * Math.PI / 2), 270); + assert.same(degrees(PI / 2), 90); + assert.same(degrees(PI), 180); + assert.same(degrees(3 * PI / 2), 270); }); diff --git a/tests/tests/esnext.math.rad-per-deg.js b/tests/tests/esnext.math.rad-per-deg.js index 56d1f3b249b4..b05553571c9a 100644 --- a/tests/tests/esnext.math.rad-per-deg.js +++ b/tests/tests/esnext.math.rad-per-deg.js @@ -1,6 +1,6 @@ QUnit.test('Math.RAD_PER_DEG', assert => { - const { RAD_PER_DEG } = Math; + const { RAD_PER_DEG, PI } = Math; assert.ok('RAD_PER_DEG' in Math, 'RAD_PER_DEG in Math'); assert.nonEnumerable(Math, 'RAD_PER_DEG'); - assert.strictEqual(RAD_PER_DEG, 180 / Math.PI, 'Is 180 / Math.PI'); + assert.strictEqual(RAD_PER_DEG, 180 / PI, 'Is 180 / Math.PI'); }); diff --git a/tests/tests/esnext.math.radians.js b/tests/tests/esnext.math.radians.js index 9434cf8735d6..42d64aef7e1d 100644 --- a/tests/tests/esnext.math.radians.js +++ b/tests/tests/esnext.math.radians.js @@ -1,12 +1,12 @@ QUnit.test('Math.radians', assert => { - const { radians } = Math; + const { radians, PI } = Math; assert.isFunction(radians); assert.name(radians, 'radians'); assert.arity(radians, 1); assert.looksNative(radians); assert.nonEnumerable(Math, 'radians'); assert.same(radians(0), 0); - assert.same(radians(90), Math.PI / 2); - assert.same(radians(180), Math.PI); - assert.same(radians(270), 3 * Math.PI / 2); + assert.same(radians(90), PI / 2); + assert.same(radians(180), PI); + assert.same(radians(270), 3 * PI / 2); });