From 6b4dcc12d9df303bddd9a29a3d46178f7b78c2f7 Mon Sep 17 00:00:00 2001 From: Denis Pushkarev Date: Thu, 24 Aug 2023 19:20:57 +0700 Subject: [PATCH] review `eslint` core rules: enforce a maximum number of statements allowed per line --- .../fix-regexp-well-known-symbol-logic.js | 5 ++++- packages/core-js/internals/iterator-define.js | 7 +++++-- .../core-js/modules/es.number.constructor.js | 17 +++++++++++++--- tests/compat/tests.js | 20 +++++++++++++++---- tests/eslint/eslint.config.js | 2 ++ tests/unit-global/es.string.replace.js | 5 ++++- 6 files changed, 45 insertions(+), 11 deletions(-) diff --git a/packages/core-js/internals/fix-regexp-well-known-symbol-logic.js b/packages/core-js/internals/fix-regexp-well-known-symbol-logic.js index 47a6b33387d7..b34f76b2f591 100644 --- a/packages/core-js/internals/fix-regexp-well-known-symbol-logic.js +++ b/packages/core-js/internals/fix-regexp-well-known-symbol-logic.js @@ -39,7 +39,10 @@ module.exports = function (KEY, exec, FORCED, SHAM) { re[SYMBOL] = /./[SYMBOL]; } - re.exec = function () { execCalled = true; return null; }; + re.exec = function () { + execCalled = true; + return null; + }; re[SYMBOL](''); return !execCalled; diff --git a/packages/core-js/internals/iterator-define.js b/packages/core-js/internals/iterator-define.js index b59a7f779265..c1eebd48e904 100644 --- a/packages/core-js/internals/iterator-define.js +++ b/packages/core-js/internals/iterator-define.js @@ -30,12 +30,15 @@ module.exports = function (Iterable, NAME, IteratorConstructor, next, DEFAULT, I var getIterationMethod = function (KIND) { if (KIND === DEFAULT && defaultIterator) return defaultIterator; - if (!BUGGY_SAFARI_ITERATORS && KIND in IterablePrototype) return IterablePrototype[KIND]; + if (!BUGGY_SAFARI_ITERATORS && KIND && KIND in IterablePrototype) return IterablePrototype[KIND]; + switch (KIND) { case KEYS: return function keys() { return new IteratorConstructor(this, KIND); }; case VALUES: return function values() { return new IteratorConstructor(this, KIND); }; case ENTRIES: return function entries() { return new IteratorConstructor(this, KIND); }; - } return function () { return new IteratorConstructor(this); }; + } + + return function () { return new IteratorConstructor(this); }; }; var TO_STRING_TAG = NAME + ' Iterator'; diff --git a/packages/core-js/modules/es.number.constructor.js b/packages/core-js/modules/es.number.constructor.js index 055701ecf49b..ce6ab0b06a8d 100644 --- a/packages/core-js/modules/es.number.constructor.js +++ b/packages/core-js/modules/es.number.constructor.js @@ -47,9 +47,20 @@ var toNumber = function (argument) { if (third === 88 || third === 120) return NaN; // Number('+0x1') should be NaN, old V8 fix } else if (first === 48) { switch (charCodeAt(it, 1)) { - case 66: case 98: radix = 2; maxCode = 49; break; // fast equal of /^0b[01]+$/i - case 79: case 111: radix = 8; maxCode = 55; break; // fast equal of /^0o[0-7]+$/i - default: return +it; + // fast equal of /^0b[01]+$/i + case 66: + case 98: + radix = 2; + maxCode = 49; + break; + // fast equal of /^0o[0-7]+$/i + case 79: + case 111: + radix = 8; + maxCode = 55; + break; + default: + return +it; } digits = stringSlice(it, 2); length = digits.length; diff --git a/tests/compat/tests.js b/tests/compat/tests.js index 1c718af616e7..3c6153698682 100644 --- a/tests/compat/tests.js +++ b/tests/compat/tests.js @@ -1140,7 +1140,10 @@ GLOBAL.tests = { var execCalled = false; var re = /a/; - re.exec = function () { execCalled = true; return null; }; + re.exec = function () { + execCalled = true; + return null; + }; re[Symbol.match](''); // eslint-disable-next-line regexp/prefer-regexp-exec -- required for testing @@ -1172,7 +1175,10 @@ GLOBAL.tests = { var execCalled = false; var re = /a/; - re.exec = function () { execCalled = true; return null; }; + re.exec = function () { + execCalled = true; + return null; + }; re[Symbol.replace](''); var re2 = /./; @@ -1199,7 +1205,10 @@ GLOBAL.tests = { var execCalled = false; var re = /a/; - re.exec = function () { execCalled = true; return null; }; + re.exec = function () { + execCalled = true; + return null; + }; re[Symbol.search](''); return ''.search(O) === 7 && execCalled; @@ -1210,7 +1219,10 @@ GLOBAL.tests = { var execCalled = false; var re = /a/; - re.exec = function () { execCalled = true; return null; }; + re.exec = function () { + execCalled = true; + return null; + }; re.constructor = {}; re.constructor[Symbol.species] = function () { return re; }; re[Symbol.split](''); diff --git a/tests/eslint/eslint.config.js b/tests/eslint/eslint.config.js index ef5650863b9b..1df8fd28b3f8 100644 --- a/tests/eslint/eslint.config.js +++ b/tests/eslint/eslint.config.js @@ -349,6 +349,8 @@ const base = { ignoreTemplateLiterals: true, ignoreUrls: true, }], + // enforce a maximum number of statements allowed per line + 'max-statements-per-line': [ERROR, { max: 2 }], // require parentheses when invoking a constructor with no arguments 'new-parens': ERROR, // disallow mixed spaces and tabs for indentation diff --git a/tests/unit-global/es.string.replace.js b/tests/unit-global/es.string.replace.js index 0a5ecf2bde02..0041a689633a 100644 --- a/tests/unit-global/es.string.replace.js +++ b/tests/unit-global/es.string.replace.js @@ -220,7 +220,10 @@ QUnit.test('RegExp#@@replace correctly handles substitutions', assert => { assert.same('1234'.replace(re, '$x'), '1$x4'); let args; - assert.same('1234'.replace(re, (..._args) => { args = _args; return 'x'; }), '1x4'); + assert.same('1234'.replace(re, (...$args) => { + args = $args; + return 'x'; + }), '1x4'); assert.deepEqual(args, ['23', '7', 1, '1234', { '!!!': '7' }]); });