Skip to content

Commit bb34c64

Browse files
perf: use ES2022 language features (#8492)
1 parent 22f0309 commit bb34c64

File tree

15 files changed

+25
-25
lines changed

15 files changed

+25
-25
lines changed

packages/browser/src/client/tester/expect/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export function toSentence(
132132
array: string[],
133133
{ wordConnector = ', ', lastWordConnector = ' and ' }: ToSentenceOptions = {},
134134
): string {
135-
return [array.slice(0, -1).join(wordConnector), array[array.length - 1]].join(
135+
return [array.slice(0, -1).join(wordConnector), array.at(-1)].join(
136136
array.length > 1 ? lastWordConnector : '',
137137
)
138138
}

packages/browser/src/node/serverOrchestrator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export async function resolveOrchestrator(
1313
// it's possible to open the page without a context
1414
if (!sessionId) {
1515
const contexts = [...globalServer.children].flatMap(p => [...p.state.orchestrators.keys()])
16-
sessionId = contexts[contexts.length - 1] ?? 'none'
16+
sessionId = contexts.at(-1) ?? 'none'
1717
}
1818

1919
// it's ok to not have a session here, especially in the preview provider

packages/expect/src/jest-expect.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
467467
const actual = this._obj as any
468468
const [propertyName, expected] = args
469469
const getValue = () => {
470-
const hasOwn = Object.prototype.hasOwnProperty.call(
470+
const hasOwn = Object.hasOwn(
471471
actual,
472472
propertyName,
473473
)
@@ -660,7 +660,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
660660
function (...args: any[]) {
661661
const spy = getSpy(this)
662662
const spyName = spy.getMockName()
663-
const lastCall = spy.mock.calls[spy.mock.calls.length - 1]
663+
const lastCall = spy.mock.calls.at(-1)
664664

665665
this.assert(
666666
lastCall && equalsArgumentArray(lastCall, args),
@@ -965,23 +965,23 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
965965
name: 'toHaveLastResolvedWith',
966966
condition: (spy, value) => {
967967
const result
968-
= spy.mock.settledResults[spy.mock.settledResults.length - 1]
969-
return (
968+
= spy.mock.settledResults.at(-1)
969+
return Boolean(
970970
result
971971
&& result.type === 'fulfilled'
972-
&& jestEquals(result.value, value)
972+
&& jestEquals(result.value, value),
973973
)
974974
},
975975
action: 'resolve',
976976
},
977977
{
978978
name: ['toHaveLastReturnedWith', 'lastReturnedWith'],
979979
condition: (spy, value) => {
980-
const result = spy.mock.results[spy.mock.results.length - 1]
981-
return (
980+
const result = spy.mock.results.at(-1)
981+
return Boolean(
982982
result
983983
&& result.type === 'return'
984-
&& jestEquals(result.value, value)
984+
&& jestEquals(result.value, value),
985985
)
986986
},
987987
action: 'return',
@@ -992,7 +992,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
992992
const spy = getSpy(this)
993993
const results
994994
= action === 'return' ? spy.mock.results : spy.mock.settledResults
995-
const result = results[results.length - 1]
995+
const result = results.at(-1)
996996
const spyName = spy.getMockName()
997997
this.assert(
998998
condition(spy, value),

packages/expect/src/jest-utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ function hasDefinedKey(obj: any, key: string) {
294294
}
295295

296296
function hasKey(obj: any, key: string) {
297-
return Object.prototype.hasOwnProperty.call(obj, key)
297+
return Object.hasOwn(obj, key)
298298
}
299299

300300
export function isA(typeName: string, value: unknown): boolean {
@@ -342,7 +342,7 @@ export function hasProperty(obj: object | null, property: string): boolean {
342342
return false
343343
}
344344

345-
if (Object.prototype.hasOwnProperty.call(obj, property)) {
345+
if (Object.hasOwn(obj, property)) {
346346
return true
347347
}
348348

@@ -571,7 +571,7 @@ function hasPropertyInObject(object: object, key: string | symbol): boolean {
571571
}
572572

573573
return (
574-
Object.prototype.hasOwnProperty.call(object, key)
574+
Object.hasOwn(object, key)
575575
|| hasPropertyInObject(Object.getPrototypeOf(object), key)
576576
)
577577
}

packages/expect/src/state.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
MATCHERS_OBJECT,
77
} from './constants'
88

9-
if (!Object.prototype.hasOwnProperty.call(globalThis, MATCHERS_OBJECT)) {
9+
if (!Object.hasOwn(globalThis, MATCHERS_OBJECT)) {
1010
const globalState = new WeakMap<ExpectStatic, MatcherState>()
1111
const matchers = Object.create(null)
1212
const customEqualityTesters: Array<Tester> = []

packages/mocker/src/node/resolver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ function getDepsCacheDir(config: ViteConfig): string {
176176
}
177177

178178
function withTrailingSlash(path: string): string {
179-
if (path[path.length - 1] !== '/') {
179+
if (path.at(-1) !== '/') {
180180
return `${path}/`
181181
}
182182

packages/pretty-format/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ export const DEFAULT_OPTIONS: Options = {
449449

450450
function validateOptions(options: OptionsReceived) {
451451
for (const key of Object.keys(options)) {
452-
if (!Object.prototype.hasOwnProperty.call(DEFAULT_OPTIONS, key)) {
452+
if (!Object.hasOwn(DEFAULT_OPTIONS, key)) {
453453
throw new Error(`pretty-format: Unknown option "${key}".`)
454454
}
455455
}

packages/runner/src/fixture.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ function splitByComma(s: string) {
395395
if (s[i] === '{' || s[i] === '[') {
396396
stack.push(s[i] === '{' ? '}' : ']')
397397
}
398-
else if (s[i] === stack[stack.length - 1]) {
398+
else if (s[i] === stack.at(-1)) {
399399
stack.pop()
400400
}
401401
else if (!stack.length && s[i] === ',') {

packages/snapshot/src/port/inlineSnapshot.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ export function stripSnapshotIndentation(inlineSnapshot: string): string {
198198
return inlineSnapshot
199199
}
200200

201-
if (lines[0].trim() !== '' || lines[lines.length - 1].trim() !== '') {
201+
if (lines[0].trim() !== '' || lines.at(-1)?.trim() !== '') {
202202
// If not blank first and last lines, abort.
203203
return inlineSnapshot
204204
}

packages/ui/client/utils/task.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { RunnerTask, RunnerTestSuite } from 'vitest'
22

33
export function isSuite(task: RunnerTask): task is RunnerTestSuite {
4-
return Object.hasOwnProperty.call(task, 'tasks')
4+
return Object.hasOwn(task, 'tasks')
55
}
66

77
export function isTaskDone(task: RunnerTask) {

0 commit comments

Comments
 (0)