Skip to content

Commit 0a0a9f7

Browse files
authored
chore: update eslint-plugin-unicorn (#15336)
1 parent b0eb836 commit 0a0a9f7

File tree

24 files changed

+800
-816
lines changed

24 files changed

+800
-816
lines changed

.eslintrc.cjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ module.exports = {
226226
'unicorn/no-static-only-class': 'off',
227227
'unicorn/prefer-number-properties': 'off',
228228
'unicorn/prefer-string-raw': 'off',
229+
'unicorn/prefer-global-this': 'off',
229230
},
230231
},
231232
// demonstration of matchers usage

e2e/__tests__/__snapshots__/wrongEnv.test.ts.snap

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ exports[`Wrong globals for environment on node <=18 print useful error for navig
1313
1414
ReferenceError: navigator is not defined
1515
16-
30 |
17-
31 | test('use navigator', () => {
18-
> 32 | const userAgent = navigator.userAgent;
16+
31 |
17+
32 | test('use navigator', () => {
18+
> 33 | const userAgent = navigator.userAgent;
1919
| ^
20-
33 |
21-
34 | console.log(userAgent);
22-
35 |
20+
34 |
21+
35 | console.log(userAgent);
22+
36 |
2323
24-
at Object.navigator (__tests__/node.js:32:21)"
24+
at Object.navigator (__tests__/node.js:33:21)"
2525
`;
2626
2727
exports[`Wrong globals for environment print useful error for document 1`] = `
@@ -83,15 +83,15 @@ exports[`Wrong globals for environment print useful error for window 1`] = `
8383
8484
ReferenceError: window is not defined
8585
86-
22 |
8786
23 | test('use window', () => {
88-
> 24 | const location = window.location;
87+
24 | // eslint-disable-next-line unicorn/prefer-global-this
88+
> 25 | const location = window.location;
8989
| ^
90-
25 |
91-
26 | console.log(location);
92-
27 |
90+
26 |
91+
27 | console.log(location);
92+
28 |
9393
94-
at Object.window (__tests__/node.js:24:20)"
94+
at Object.window (__tests__/node.js:25:20)"
9595
`;
9696
9797
exports[`Wrong globals for environment print useful error when it explodes during evaluation 1`] = `

e2e/console-jsdom/__tests__/console.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ test('can mock console.error calls from jsdom', () => {
3030

3131
function onError(event) {}
3232

33-
window.addEventListener('error', onError);
33+
globalThis.addEventListener('error', onError);
3434
fakeNode.addEventListener(evtType, callCallback, false);
3535
evt.initEvent(evtType, false, false);
3636
fakeNode.dispatchEvent(evt);
37-
window.removeEventListener('error', onError);
37+
globalThis.removeEventListener('error', onError);
3838

3939
expect(console.error).toHaveBeenCalledTimes(1);
4040
expect(console.error).toHaveBeenCalledWith(

e2e/env-test/__tests__/equivalent.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
const {isArrayBuffer} = require('util').types;
1010
const isJSDOM =
11+
// eslint-disable-next-line unicorn/prefer-global-this
1112
typeof window !== 'undefined' && typeof document !== 'undefined';
1213

1314
const skipTestJSDOM = isJSDOM ? test.skip : test;

e2e/environmentOptions/__tests__/environmentOptions.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
/*eslint-env browser */
99

1010
test('found url jestjs.io', () => {
11-
expect(window.location.href).toBe('https://jestjs.io/');
11+
expect(globalThis.location.href).toBe('https://jestjs.io/');
1212
});

e2e/fake-timers/do-not-fake/__tests__/doNotFake.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@
1010
'use strict';
1111

1212
const mockPerformanceMark = jest.fn();
13-
window.performance.mark = mockPerformanceMark;
13+
globalThis.performance.mark = mockPerformanceMark;
1414

1515
test('fakes all APIs', () => {
1616
jest.useFakeTimers();
1717

18-
expect(window.performance.mark).toBeUndefined();
18+
expect(globalThis.performance.mark).toBeUndefined();
1919
});
2020

2121
test('does not fake `performance` instance', () => {
2222
jest.useFakeTimers({doNotFake: ['performance']});
2323

24-
expect(window.performance.mark).toBe(mockPerformanceMark);
24+
expect(globalThis.performance.mark).toBe(mockPerformanceMark);
2525
});

e2e/nested-event-loop/__tests__/nestedEventLoop.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ it('can assert on errors across nested event loops', () => {
1717
throw new Error('This should be caught.');
1818
});
1919
let caught = null;
20-
window.addEventListener('error', e => {
20+
globalThis.addEventListener('error', e => {
2121
caught = e.error;
2222
});
2323
expect(() => {

e2e/override-globals/__tests__/index.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,8 @@ describe('parent', () => {
2929
});
3030

3131
it('can override atob and btoa', () => {
32-
// eslint-disable-next-line no-restricted-globals
33-
global.atob = () => 'hello';
34-
// eslint-disable-next-line no-restricted-globals
35-
global.btoa = () => 'there';
32+
globalThis.atob = () => 'hello';
33+
globalThis.btoa = () => 'there';
3634

3735
expect(`${atob()} ${btoa()}`).toBe('hello there');
3836
});

e2e/test-environment/__tests__/environmentOptionsFromDocblock.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
/*eslint-env browser */
1212

1313
test('use jsdom and set the URL in this test file', () => {
14-
expect(window.location.href).toBe('https://jestjs.io/');
14+
expect(globalThis.location.href).toBe('https://jestjs.io/');
1515
});

e2e/wrong-env/__tests__/node.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ test('use document', () => {
2121
});
2222

2323
test('use window', () => {
24+
// eslint-disable-next-line unicorn/prefer-global-this
2425
const location = window.location;
2526

2627
console.log(location);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"eslint-plugin-markdown": "^3.0.0",
4242
"eslint-plugin-prettier": "^5.0.0",
4343
"eslint-plugin-promise": "^6.1.1",
44-
"eslint-plugin-unicorn": "^55.0.0",
44+
"eslint-plugin-unicorn": "^56.0.0",
4545
"execa": "^5.0.0",
4646
"find-process": "^1.4.1",
4747
"glob": "^10.3.10",

packages/diff-sequences/src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ const extendPathsF = (
164164
);
165165

166166
// Optimization: skip diagonals in which paths cannot ever overlap.
167-
const nF = d < iMaxF ? d : iMaxF;
167+
const nF = Math.min(d, iMaxF);
168168

169169
// The diagonals kF are odd when d is odd and even when d is even.
170170
for (iF += 1, kF += 2; iF <= nF; iF += 1, kF += 2) {
@@ -217,7 +217,7 @@ const extendPathsR = (
217217
);
218218

219219
// Optimization: skip diagonals in which paths cannot ever overlap.
220-
const nR = d < iMaxR ? d : iMaxR;
220+
const nR = Math.min(d, iMaxR);
221221

222222
// The diagonals kR are odd when d is odd and even when d is even.
223223
for (iR += 1, kR -= 2; iR <= nR; iR += 1, kR -= 2) {
@@ -278,7 +278,7 @@ const extendOverlappablePathsF = (
278278
let aIndexPrev1 = NOT_YET_SET; // prev value of [iF - 1] in next iteration
279279

280280
// Optimization: skip diagonals in which paths cannot ever overlap.
281-
const nF = d < iMaxF ? d : iMaxF;
281+
const nF = Math.min(d, iMaxF);
282282

283283
// The diagonals kF = 2 * iF - d are odd when d is odd and even when d is even.
284284
for (let iF = 0, kF = -d; iF <= nF; iF += 1, kF += 2) {
@@ -411,7 +411,7 @@ const extendOverlappablePathsR = (
411411
let aIndexPrev1 = NOT_YET_SET; // prev value of [iR - 1] in next iteration
412412

413413
// Optimization: skip diagonals in which paths cannot ever overlap.
414-
const nR = d < iMaxR ? d : iMaxR;
414+
const nR = Math.min(d, iMaxR);
415415

416416
// The diagonals kR = d - 2 * iR are odd when d is odd and even when d is even.
417417
for (let iR = 0, kR = d; iR <= nR; iR += 1, kR -= 2) {

packages/jest-circus/src/eventHandler.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,8 @@ const eventHandler: Circus.EventHandler = (event, state) => {
216216
}
217217
case 'test_retry': {
218218
const logErrorsBeforeRetry: boolean =
219-
// eslint-disable-next-line no-restricted-globals
220-
((global as Global.Global)[LOG_ERRORS_BEFORE_RETRY] as any) || false;
219+
((globalThis as Global.Global)[LOG_ERRORS_BEFORE_RETRY] as any) ||
220+
false;
221221
if (logErrorsBeforeRetry) {
222222
event.test.retryReasons.push(...event.test.errors);
223223
}
@@ -226,13 +226,12 @@ const eventHandler: Circus.EventHandler = (event, state) => {
226226
}
227227
case 'run_start': {
228228
state.hasStarted = true;
229-
/* eslint-disable no-restricted-globals */
230-
if ((global as Global.Global)[TEST_TIMEOUT_SYMBOL]) {
231-
state.testTimeout = (global as Global.Global)[
229+
if ((globalThis as Global.Global)[TEST_TIMEOUT_SYMBOL]) {
230+
state.testTimeout = (globalThis as Global.Global)[
232231
TEST_TIMEOUT_SYMBOL
233232
] as number;
234233
}
235-
/* eslint-enable */
234+
236235
break;
237236
}
238237
case 'run_finish': {

packages/jest-circus/src/run.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,17 @@ const _runTestsForDescribeBlock = async (
6969

7070
// Tests that fail and are retried we run after other tests
7171
const retryTimes =
72-
// eslint-disable-next-line no-restricted-globals
73-
Number.parseInt((global as Global.Global)[RETRY_TIMES] as string, 10) || 0;
72+
Number.parseInt((globalThis as Global.Global)[RETRY_TIMES] as string, 10) ||
73+
0;
7474

7575
const waitBeforeRetry =
7676
Number.parseInt(
77-
// eslint-disable-next-line no-restricted-globals
78-
(global as Global.Global)[WAIT_BEFORE_RETRY] as string,
77+
(globalThis as Global.Global)[WAIT_BEFORE_RETRY] as string,
7978
10,
8079
) || 0;
8180

8281
const retryImmediately: boolean =
83-
// eslint-disable-next-line no-restricted-globals
84-
((global as Global.Global)[RETRY_IMMEDIATELY] as any) || false;
82+
((globalThis as Global.Global)[RETRY_IMMEDIATELY] as any) || false;
8583

8684
const deferredRetryTests = [];
8785

packages/jest-circus/src/state.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,16 @@ const createState = (): Circus.State => {
3838
};
3939
};
4040

41-
/* eslint-disable no-restricted-globals */
4241
export const resetState = (): void => {
43-
(global as Global.Global)[STATE_SYM] = createState();
42+
(globalThis as Global.Global)[STATE_SYM] = createState();
4443
};
4544

4645
resetState();
4746

4847
export const getState = (): Circus.State =>
49-
(global as Global.Global)[STATE_SYM] as Circus.State;
48+
(globalThis as Global.Global)[STATE_SYM] as Circus.State;
5049
export const setState = (state: Circus.State): Circus.State =>
51-
((global as Global.Global)[STATE_SYM] = state);
52-
/* eslint-enable */
50+
((globalThis as Global.Global)[STATE_SYM] = state);
5351

5452
export const dispatch = async (event: Circus.AsyncEvent): Promise<void> => {
5553
for (const handler of eventHandlers) {

packages/jest-jasmine2/src/jasmine/jasmineLight.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,13 @@ export const create = function (createOptions: Record<string, any>): Jasmine {
6060
enumerable: true,
6161
get() {
6262
return (
63-
// eslint-disable-next-line no-restricted-globals
64-
(global as Global.Global)[testTimeoutSymbol] ||
63+
(globalThis as Global.Global)[testTimeoutSymbol] ||
6564
createOptions.testTimeout ||
6665
5000
6766
);
6867
},
6968
set(value) {
70-
// eslint-disable-next-line no-restricted-globals
71-
(global as Global.Global)[testTimeoutSymbol] = value;
69+
(globalThis as Global.Global)[testTimeoutSymbol] = value;
7270
},
7371
});
7472

packages/jest-jasmine2/src/jestExpect.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@ import type {Global} from '@jest/types';
1212
import type {JasmineMatchersObject} from './types';
1313

1414
export default function jestExpectAdapter(config: {expand: boolean}): void {
15-
// eslint-disable-next-line no-restricted-globals
16-
(global as Global.Global).expect = jestExpect;
15+
(globalThis as Global.Global).expect = jestExpect;
1716
jestExpect.setState({expand: config.expand});
1817

19-
// eslint-disable-next-line no-restricted-globals
20-
const jasmine = (global as Global.Global).jasmine;
18+
const jasmine = (globalThis as Global.Global).jasmine;
2119
jasmine.anything = jestExpect.anything;
2220
jasmine.any = jestExpect.any;
2321
jasmine.objectContaining = jestExpect.objectContaining;

packages/jest-matcher-utils/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ type PrintLabel = (string: string) => string;
504504

505505
export const getLabelPrinter = (...strings: Array<string>): PrintLabel => {
506506
const maxLength = strings.reduce(
507-
(max, string) => (string.length > max ? string.length : max),
507+
(max, string) => Math.max(string.length, max),
508508
0,
509509
);
510510
return (string: string): string =>

packages/jest-mock/src/__tests__/window-spy.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
/* eslint-env browser*/
1313

1414
function exampleDispatch() {
15-
window.dispatchEvent(new CustomEvent('event', {}));
15+
globalThis.dispatchEvent(new CustomEvent('event', {}));
1616
}
1717

1818
describe('spy on `dispatchEvent`', () => {
19-
const dispatchEventSpy = jest.spyOn(window, 'dispatchEvent');
19+
const dispatchEventSpy = jest.spyOn(globalThis, 'dispatchEvent');
2020

2121
it('should be called', () => {
2222
exampleDispatch();

packages/jest-resolve/src/nodeModulesPaths.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ function findGlobalPaths(): Array<string> {
8181
if (resolvePaths) {
8282
// the global paths start one after the root node_modules
8383
const rootIndex = resolvePaths.indexOf(globalPath);
84-
return rootIndex > -1 ? resolvePaths.slice(rootIndex + 1) : [];
84+
return rootIndex === -1 ? [] : resolvePaths.slice(rootIndex + 1);
8585
}
8686
return [];
8787
}

packages/jest-snapshot/src/utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -411,10 +411,10 @@ const traverseAst = (
411411
[],
412412
);
413413

414-
if (snapshotIndex > -1) {
415-
args[snapshotIndex] = replacementNode;
416-
} else {
414+
if (snapshotIndex === -1) {
417415
args.push(replacementNode);
416+
} else {
417+
args[snapshotIndex] = replacementNode;
418418
}
419419
});
420420

packages/pretty-format/src/__tests__/DOMElement.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ setPrettyPrint([DOMElement]);
2121
describe('pretty-format', () => {
2222
// Test is not related to plugin but is related to jsdom testing environment.
2323
it('prints global window as constructor name alone', () => {
24-
expect(prettyFormat(window)).toBe('[Window]');
24+
expect(prettyFormat(globalThis)).toBe('[Window]');
2525
});
2626
});
2727

packages/pretty-format/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ const getConstructorName = (val: new (...args: Array<any>) => unknown) =>
6363
/* global window */
6464
/** Is val is equal to global window object? Works even if it does not exist :) */
6565
const isWindow = (val: unknown) =>
66+
// eslint-disable-next-line unicorn/prefer-global-this
6667
typeof window !== 'undefined' && val === window;
6768

6869
const SYMBOL_REGEXP = /^Symbol\((.*)\)(.*)$/;

0 commit comments

Comments
 (0)