Skip to content

Commit b35e68e

Browse files
committed
enable sonar/inconsistent-function-call
1 parent 0d8fe67 commit b35e68e

20 files changed

+46
-25
lines changed

packages/core-js-compat/helpers.js

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,25 @@
22
// eslint-disable-next-line es/no-object-hasown -- safe
33
const has = Object.hasOwn || Function.call.bind({}.hasOwnProperty);
44

5-
function semver(input) {
6-
if (input instanceof semver) return input;
7-
// eslint-disable-next-line new-cap -- ok
8-
if (!(this instanceof semver)) return new semver(input);
9-
const match = /(\d+)(?:\.(\d+))?(?:\.(\d+))?/.exec(input);
10-
if (!match) throw new TypeError(`Invalid version: ${ input }`);
11-
const [, $major, $minor, $patch] = match;
12-
this.major = +$major;
13-
this.minor = $minor ? +$minor : 0;
14-
this.patch = $patch ? +$patch : 0;
5+
const VERSION_PATTERN = /(\d+)(?:\.(\d+))?(?:\.(\d+))?/;
6+
7+
class SemVer {
8+
constructor(input) {
9+
const match = VERSION_PATTERN.exec(input);
10+
if (!match) throw new TypeError(`Invalid version: ${ input }`);
11+
const [, $major, $minor, $patch] = match;
12+
this.major = +$major;
13+
this.minor = $minor ? +$minor : 0;
14+
this.patch = $patch ? +$patch : 0;
15+
}
16+
toString() {
17+
return `${ this.major }.${ this.minor }.${ this.patch }`;
18+
}
1519
}
1620

17-
semver.prototype.toString = function () {
18-
return `${ this.major }.${ this.minor }.${ this.patch }`;
19-
};
21+
function semver(input) {
22+
return input instanceof SemVer ? input : new SemVer(input);
23+
}
2024

2125
function compare($a, operator, $b) {
2226
const a = semver($a);

packages/core-js/internals/array-buffer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ if (!NATIVE_ARRAY_BUFFER) {
205205
});
206206
} else {
207207
var INCORRECT_ARRAY_BUFFER_NAME = PROPER_FUNCTION_NAME && NativeArrayBuffer.name !== ARRAY_BUFFER;
208-
/* eslint-disable no-new -- required for testing */
208+
/* eslint-disable no-new, sonar/inconsistent-function-call -- required for testing */
209209
if (!fails(function () {
210210
NativeArrayBuffer(1);
211211
}) || !fails(function () {
@@ -216,7 +216,7 @@ if (!NATIVE_ARRAY_BUFFER) {
216216
new NativeArrayBuffer(NaN);
217217
return NativeArrayBuffer.length !== 1 || INCORRECT_ARRAY_BUFFER_NAME && !CONFIGURABLE_FUNCTION_NAME;
218218
})) {
219-
/* eslint-enable no-new -- required for testing */
219+
/* eslint-enable no-new, sonar/inconsistent-function-call -- required for testing */
220220
$ArrayBuffer = function ArrayBuffer(length) {
221221
anInstance(this, ArrayBufferPrototype);
222222
return inheritIfRequired(new NativeArrayBuffer(toIndex(length)), this, $ArrayBuffer);

packages/core-js/internals/typed-array-constructors-require-wrappers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
/* eslint-disable no-new -- required for testing */
2+
/* eslint-disable no-new, sonar/inconsistent-function-call -- required for testing */
33
var globalThis = require('../internals/global-this');
44
var fails = require('../internals/fails');
55
var checkCorrectnessOfIteration = require('../internals/check-correctness-of-iteration');

packages/core-js/modules/es.regexp.constructor.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ var BASE_FORCED = DESCRIPTORS &&
4646
(!CORRECT_NEW || MISSED_STICKY || UNSUPPORTED_DOT_ALL || UNSUPPORTED_NCG || fails(function () {
4747
re2[MATCH] = false;
4848
// RegExp constructor can alter flags and IsRegExp works correct with @@match
49+
// eslint-disable-next-line sonar/inconsistent-function-call -- required for testing
4950
return NativeRegExp(re1) !== re1 || NativeRegExp(re2) === re2 || String(NativeRegExp(re1, 'i')) !== '/a/i';
5051
}));
5152

packages/core-js/modules/es.symbol.description.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ if (DESCRIPTORS && isCallable(NativeSymbol) && (!('description' in SymbolPrototy
2424
var SymbolWrapper = function Symbol() {
2525
var description = arguments.length < 1 || arguments[0] === undefined ? undefined : toString(arguments[0]);
2626
var result = isPrototypeOf(SymbolPrototype, this)
27+
// eslint-disable-next-line sonar/inconsistent-function-call -- ok
2728
? new NativeSymbol(description)
2829
// in Edge 13, String(Symbol(undefined)) === 'Symbol(undefined)'
2930
: description === undefined ? NativeSymbol() : NativeSymbol(description);

tests/eslint/eslint.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,8 @@ const base = {
696696
'sonar/expression-complexity': [OFF, { max: 3 }],
697697
// `in` should not be used with primitive types
698698
'sonar/in-operator-type-error': ERROR,
699+
// functions should be called consistently with or without `new`
700+
'sonar/inconsistent-function-call': ERROR,
699701

700702
// sonarjs
701703
// collection sizes and array length comparisons should make sense

tests/unit-global/es.error.cause.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable sonar/inconsistent-function-call -- required for testing */
12
import { GLOBAL, PROTO } from '../helpers/constants.js';
23

34
const { create } = Object;

tests/unit-global/es.typed-array.constructors.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ if (DESCRIPTORS) {
173173
assert.throws(() => new TypedArray(new ArrayBuffer(8), 16), 'If newByteLength < 0, throw a RangeError exception');
174174
assert.throws(() => new TypedArray(new ArrayBuffer(24), 8, 24), 'If offset+newByteLength > bufferByteLength, throw a RangeError exception');
175175
}
176+
// eslint-disable-next-line sonar/inconsistent-function-call -- required for testing
176177
assert.throws(() => TypedArray(1), TypeError, 'throws without `new`');
177178
assert.same(TypedArray[Symbol.species], TypedArray, '@@species');
178179
});

tests/unit-pure/es.aggregate-error.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* eslint-disable unicorn/throw-new-error -- testing */
1+
/* eslint-disable unicorn/throw-new-error, sonar/inconsistent-function-call -- required for testing */
22
import AggregateError from 'core-js-pure/es/aggregate-error';
33
import Symbol from 'core-js-pure/es/symbol';
44
import toString from 'core-js-pure/es/object/to-string';

tests/unit-pure/es.error.cause.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable sonar/inconsistent-function-call -- required for testing */
12
import { PROTO } from '../helpers/constants.js';
23

34
import path from 'core-js-pure/es/error';

tests/unit-pure/es.number.constructor.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable sonar/inconsistent-function-call -- required for testing */
12
import globalThis from 'core-js-pure/es/global-this';
23
import create from 'core-js-pure/es/object/create';
34
import Number from 'core-js-pure/es/number';

tests/unit-pure/es.promise.constructor.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ import create from 'core-js-pure/es/object/create';
77

88
QUnit.test('Promise', assert => {
99
assert.isFunction(Promise);
10-
assert.throws(() => {
11-
Promise();
12-
}, 'throws w/o `new`');
1310
new Promise(function (resolve, reject) {
1411
assert.isFunction(resolve, 'resolver is function');
1512
assert.isFunction(reject, 'rejector is function');
1613
if (STRICT) assert.same(this, undefined, 'correct executor context');
1714
});
15+
assert.throws(() => {
16+
// eslint-disable-next-line sonar/inconsistent-function-call -- required for testing
17+
Promise();
18+
}, 'throws w/o `new`');
1819
});
1920

2021
if (DESCRIPTORS) QUnit.test('Promise operations order', assert => {

tests/unit-pure/esnext.async-disposable-stack.constructor.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ QUnit.test('AsyncDisposableStack constructor', assert => {
99
assert.isFunction(AsyncDisposableStack);
1010
assert.arity(AsyncDisposableStack, 0);
1111
assert.name(AsyncDisposableStack, 'AsyncDisposableStack');
12-
13-
assert.throws(() => AsyncDisposableStack(), 'throws w/o `new`');
1412
assert.true(new AsyncDisposableStack() instanceof AsyncDisposableStack);
1513

1614
assert.same(AsyncDisposableStack.prototype.constructor, AsyncDisposableStack);
15+
16+
// eslint-disable-next-line sonar/inconsistent-function-call -- required for testing
17+
assert.throws(() => AsyncDisposableStack(), 'throws w/o `new`');
1718
});
1819

1920
QUnit.test('AsyncDisposableStack#disposeAsync', assert => {

tests/unit-pure/esnext.async-iterator.constructor.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ QUnit.test('AsyncIterator', assert => {
1717
}
1818

1919
assert.throws(() => new AsyncIterator(), 'direct constructor throws');
20+
// eslint-disable-next-line sonar/inconsistent-function-call -- required for testing
2021
assert.throws(() => AsyncIterator(), 'throws w/o `new`');
2122
});
2223

tests/unit-pure/esnext.disposable-stack.constructor.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ QUnit.test('DisposableStack constructor', assert => {
99
assert.arity(DisposableStack, 0);
1010
assert.name(DisposableStack, 'DisposableStack');
1111

12-
assert.throws(() => DisposableStack(), 'throws w/o `new`');
1312
assert.true(new DisposableStack() instanceof DisposableStack);
1413

1514
assert.same(DisposableStack.prototype.constructor, DisposableStack);
15+
16+
// eslint-disable-next-line sonar/inconsistent-function-call -- required for testing
17+
assert.throws(() => DisposableStack(), 'throws w/o `new`');
1618
});
1719

1820
QUnit.test('DisposableStack#dispose', assert => {

tests/unit-pure/esnext.iterator.constructor.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ QUnit.test('Iterator', assert => {
1515
}
1616

1717
assert.throws(() => new Iterator(), 'direct constructor throws');
18+
// eslint-disable-next-line sonar/inconsistent-function-call -- required for testing
1819
assert.throws(() => Iterator(), 'throws w/o `new`');
1920
});
2021

tests/unit-pure/esnext.observable.constructor.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import Observable from 'core-js-pure/full/observable';
66
QUnit.test('Observable', assert => {
77
assert.isFunction(Observable);
88
assert.arity(Observable, 1);
9-
assert.throws(() => Observable(() => { /* empty */ }), 'throws w/o `new`');
109
const observable = new Observable(function (subscriptionObserver) {
1110
assert.same(typeof subscriptionObserver, 'object', 'Subscription observer is object');
1211
assert.same(subscriptionObserver.constructor, Object);
@@ -23,6 +22,8 @@ QUnit.test('Observable', assert => {
2322
});
2423
observable.subscribe({});
2524
assert.true(observable instanceof Observable);
25+
// eslint-disable-next-line sonar/inconsistent-function-call -- required for testing
26+
assert.throws(() => Observable(() => { /* empty */ }), 'throws w/o `new`');
2627
});
2728

2829
QUnit.test('Observable#subscribe', assert => {

tests/unit-pure/esnext.suppressed-error.constructor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* eslint-disable unicorn/throw-new-error -- testing */
1+
/* eslint-disable unicorn/throw-new-error, sonar/inconsistent-function-call -- testing */
22
import SuppressedError from 'core-js-pure/actual/suppressed-error';
33
import Symbol from 'core-js-pure/es/symbol';
44
import toString from 'core-js-pure/es/object/to-string';

tests/unit-pure/web.dom-exception.constructor.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ QUnit.test('DOMException', assert => {
6767
assert.same(DOMException.prototype[errors[name].s], errors[name].c, `DOMException.prototype.${ errors[name].s }`);
6868
}
6969

70+
// eslint-disable-next-line sonar/inconsistent-function-call -- required for testing
7071
assert.throws(() => DOMException(42, 'DataCloneError'), "DOMException(42, 'DataCloneError')");
7172
const symbol = Symbol('DOMException constructor test');
7273
assert.throws(() => new DOMException(symbol, 'DataCloneError'), "new DOMException(Symbol(), 'DataCloneError')");

tests/unit-pure/web.url-search-params.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ QUnit.test('URLSearchParams', assert => {
123123

124124
// https://github.com/oven-sh/bun/issues/9253
125125
if (!BUN) assert.throws(() => {
126+
// eslint-disable-next-line sonar/inconsistent-function-call -- required for testing
126127
URLSearchParams('');
127128
}, 'throws w/o `new`');
128129

0 commit comments

Comments
 (0)