Skip to content

Commit

Permalink
review eslint core rules: enforce a maximum number of statements al…
Browse files Browse the repository at this point in the history
…lowed per line
  • Loading branch information
zloirock committed Aug 24, 2023
1 parent 6aa48ce commit 6b4dcc1
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 5 additions & 2 deletions packages/core-js/internals/iterator-define.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
17 changes: 14 additions & 3 deletions packages/core-js/modules/es.number.constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
20 changes: 16 additions & 4 deletions tests/compat/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = /./;
Expand All @@ -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;
Expand All @@ -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]('');
Expand Down
2 changes: 2 additions & 0 deletions tests/eslint/eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion tests/unit-global/es.string.replace.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' }]);
});

Expand Down

0 comments on commit 6b4dcc1

Please sign in to comment.