Skip to content

Commit 0f6c98d

Browse files
committed
update eslint-plugin-unicorn
1 parent d72676f commit 0f6c98d

File tree

12 files changed

+51
-85
lines changed

12 files changed

+51
-85
lines changed

README.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1576,15 +1576,11 @@ Symbol().description; // => undefined
15761576
You can disable defining setters in `Object.prototype`. [Example](https://tinyurl.com/2blse6aa):
15771577
```js
15781578
Symbol.useSimple();
1579-
let symbol1 = Symbol('symbol1');
1580-
let object1 = {};
1581-
object1[symbol1] = true;
1579+
let object1 = { [Symbol('symbol1')]: true };
15821580
for (let key in object1) console.log(key); // => 'Symbol(symbol1)_t.qamkg9f3q', w/o native Symbol
15831581

15841582
Symbol.useSetter();
1585-
let symbol2 = Symbol('symbol2');
1586-
let object2 = {};
1587-
object2[symbol2] = true;
1583+
let object2 = { [Symbol('symbol2')]: true };
15881584
for (let key in object2) console.log(key); // nothing
15891585
```
15901586
- Currently, `core-js` does not add setters to `Object.prototype` for well-known symbols for correct work something like `Symbol.iterator in foo`. It can cause problems with their enumerability.

docs/web/docs/features/ecmascript/symbol.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,11 @@ Symbol().description; // => undefined
121121
You can disable defining setters in `Object.prototype`. *Example*:
122122
```ts
123123
Symbol.useSimple();
124-
let symbol1 = Symbol('symbol1');
125-
let object1 = {};
126-
object1[symbol1] = true;
124+
let object1 = { [Symbol('symbol1')]: true };
127125
for (let key in object1) console.log(key); // => 'Symbol(symbol1)_t.qamkg9f3q', w/o native Symbol
128126

129127
Symbol.useSetter();
130-
let symbol2 = Symbol('symbol2');
131-
let object2 = {};
132-
object2[symbol2] = true;
128+
let object2 = { [Symbol('symbol2')]: true };
133129
for (let key in object2) console.log(key); // nothing
134130
```
135131
- Currently, `core-js` does not add setters to `Object.prototype` for well-known symbols for correct work something like `Symbol.iterator in foo`. It can cause problems with their enumerability.

packages/core-js/internals/check-correctness-of-iteration.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ try {
1414
SAFE_CLOSING = true;
1515
}
1616
};
17+
// eslint-disable-next-line unicorn/no-immediate-mutation -- ES3 syntax limitation
1718
iteratorWithReturn[ITERATOR] = function () {
1819
return this;
1920
};
@@ -28,6 +29,7 @@ module.exports = function (exec, SKIP_CLOSING) {
2829
var ITERATION_SUPPORT = false;
2930
try {
3031
var object = {};
32+
// eslint-disable-next-line unicorn/no-immediate-mutation -- ES3 syntax limitation
3133
object[ITERATOR] = function () {
3234
return {
3335
next: function () {

packages/core-js/internals/fix-regexp-well-known-symbol-logic.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ module.exports = function (KEY, exec, FORCED, SHAM) {
1717
var DELEGATES_TO_SYMBOL = !fails(function () {
1818
// String methods call symbol-named RegExp methods
1919
var O = {};
20+
// eslint-disable-next-line unicorn/no-immediate-mutation -- ES3 syntax limitation
2021
O[SYMBOL] = function () { return 7; };
2122
return ''[KEY](O) !== 7;
2223
});
@@ -30,12 +31,13 @@ module.exports = function (KEY, exec, FORCED, SHAM) {
3031
// We can't use real regex here since it causes deoptimization
3132
// and serious performance degradation in V8
3233
// https://github.com/zloirock/core-js/issues/306
33-
re = {};
3434
// RegExp[@@split] doesn't call the regex's exec method, but first creates
3535
// a new one. We need to return the patched regex when creating the new one.
36-
re.constructor = {};
37-
re.constructor[SPECIES] = function () { return re; };
38-
re.flags = '';
36+
var constructor = {};
37+
// eslint-disable-next-line unicorn/no-immediate-mutation -- ES3 syntax limitation
38+
constructor[SPECIES] = function () { return re; };
39+
re = { constructor: constructor, flags: '' };
40+
// eslint-disable-next-line unicorn/no-immediate-mutation -- ES3 syntax limitation
3941
re[SYMBOL] = /./[SYMBOL];
4042
}
4143

packages/core-js/internals/internal-metadata.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ var enable = function () {
6060
var getOwnPropertyNames = getOwnPropertyNamesModule.f;
6161
var splice = uncurryThis([].splice);
6262
var test = {};
63+
// eslint-disable-next-line unicorn/no-immediate-mutation -- ES3 syntax limitation
6364
test[METADATA] = 1;
6465

6566
// prevent exposing of metadata key

packages/core-js/internals/set-method-accept-set-like.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ module.exports = function (name, callback) {
4848
new Set()[name](createSetLikeWithInfinitySize(-Infinity));
4949
return false;
5050
} catch (error) {
51-
var set = new Set();
52-
set.add(1);
53-
set.add(2);
51+
var set = new Set([1, 2]);
5452
return callback(set[name](createSetLikeWithInfinitySize(Infinity)));
5553
}
5654
}

packages/core-js/internals/to-string-tag-support.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ var wellKnownSymbol = require('../internals/well-known-symbol');
33

44
var TO_STRING_TAG = wellKnownSymbol('toStringTag');
55
var test = {};
6-
6+
// eslint-disable-next-line unicorn/no-immediate-mutation -- ES3 syntax limitation
77
test[TO_STRING_TAG] = 'z';
88

99
module.exports = String(test) === '[object z]';

packages/core-js/modules/es.error.cause.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ var FORCED = new Error('e', { cause: 7 }).cause !== 7;
1313

1414
var exportGlobalErrorCauseWrapper = function (ERROR_NAME, wrapper) {
1515
var O = {};
16+
// eslint-disable-next-line unicorn/no-immediate-mutation -- ES3 syntax limitation
1617
O[ERROR_NAME] = wrapErrorConstructorWithCause(ERROR_NAME, wrapper, FORCED);
1718
$({ global: true, constructor: true, arity: 1, forced: FORCED }, O);
1819
};
1920

2021
var exportWebAssemblyErrorCauseWrapper = function (ERROR_NAME, wrapper) {
2122
if (WebAssembly && WebAssembly[ERROR_NAME]) {
2223
var O = {};
24+
// eslint-disable-next-line unicorn/no-immediate-mutation -- ES3 syntax limitation
2325
O[ERROR_NAME] = wrapErrorConstructorWithCause(WEB_ASSEMBLY + '.' + ERROR_NAME, wrapper, FORCED);
2426
$({ target: WEB_ASSEMBLY, stat: true, constructor: true, arity: 1, forced: FORCED }, O);
2527
}

tests/compat/tests.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,7 @@ function createSetMethodTest(METHOD_NAME, callback) {
272272
new Set()[METHOD_NAME](createSetLikeWithInfinitySize(-Infinity));
273273
return false;
274274
} catch (error) {
275-
var set = new Set();
276-
set.add(1);
277-
set.add(2);
275+
var set = new Set([1, 2]);
278276
return callback(set[METHOD_NAME](createSetLikeWithInfinitySize(Infinity)));
279277
}
280278
}

tests/eslint/eslint.config.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,11 @@ const base = {
604604
'unicorn/no-console-spaces': ERROR,
605605
// enforce the use of unicode escapes instead of hexadecimal escapes
606606
'unicorn/no-hex-escape': ERROR,
607+
// disallow immediate mutation after variable assignment
608+
// that cause problems with objects in ES3 syntax, but since unicorn team
609+
// don't wanna add an option to allow it, manually disable this rule in such problem cases
610+
// https://github.com/sindresorhus/eslint-plugin-unicorn/issues/2796
611+
'unicorn/no-immediate-mutation': ERROR,
607612
// disallow `instanceof` with built-in objects
608613
'unicorn/no-instanceof-builtins': [ERROR, { strategy: 'loose' }],
609614
// disallow invalid options in `fetch` and `Request`
@@ -640,6 +645,8 @@ const base = {
640645
'unicorn/no-unreadable-iife': ERROR,
641646
// disallow unused object properties
642647
'unicorn/no-unused-properties': ERROR,
648+
// disallow useless values or fallbacks in `Set`, `Map`, `WeakSet`, or `WeakMap`
649+
'unicorn/no-useless-collection-argument': ERROR,
643650
// disallow unnecessary `Error.captureStackTrace()`
644651
'unicorn/no-useless-error-capture-stack-trace': ERROR,
645652
// forbid useless fallback when spreading in object literals
@@ -708,6 +715,8 @@ const base = {
708715
'unicorn/prefer-object-from-entries': ERROR,
709716
// prefer omitting the `catch` binding parameter
710717
'unicorn/prefer-optional-catch-binding': ERROR,
718+
// prefer `Response.json()` over `new Response(JSON.stringify())`
719+
'unicorn/prefer-response-static-json': ERROR,
711720
// prefer using `structuredClone` to create a deep clone
712721
'unicorn/prefer-structured-clone': ERROR,
713722
// prefer using `Set#size` instead of `Array#length`
@@ -1938,6 +1947,8 @@ const tests = {
19381947
'unicorn/error-message': OFF,
19391948
// prefer `Array#toReversed()` over `Array#reverse()`
19401949
'unicorn/no-array-reverse': OFF,
1950+
// disallow immediate mutation after variable assignment
1951+
'unicorn/no-immediate-mutation': OFF,
19411952
// disallow `instanceof` with built-in objects
19421953
'unicorn/no-instanceof-builtins': OFF,
19431954
// prefer `.at()` method for index access and `String#charAt()`

0 commit comments

Comments
 (0)