Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: add type lint check #6884

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ jobs:
- name: Lint
run: pnpm run lint

- name: Type Lint
run: pnpm run tsslint

- name: Typecheck
run: pnpm run typecheck

Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"docs:contributors": "tsx scripts/update-contributors.ts",
"lint": "eslint --cache .",
"lint:fix": "nr lint --fix",
"tsslint": "tsslint --project ./tsconfig.lint.json",
"publish-ci": "tsx scripts/publish-ci.ts",
"release": "tsx scripts/release.ts",
"test": "pnpm --filter test-core test:threads",
Expand All @@ -42,6 +43,9 @@
"@rollup/plugin-commonjs": "^28.0.1",
"@rollup/plugin-json": "^6.1.0",
"@rollup/plugin-node-resolve": "^15.3.0",
"@tsslint/cli": "^1.1.4",
"@tsslint/config": "^1.1.4",
"@tsslint/eslint": "^1.1.4",
"@types/node": "^22.9.0",
"@types/ws": "^8.5.13",
"@vitest/browser": "workspace:*",
Expand Down
22 changes: 11 additions & 11 deletions packages/vitest/src/node/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ export class Vitest {
}

const workspaceModule = await this.runner.executeFile(workspaceConfigPath) as {
default: ReturnType<typeof defineWorkspace>
default: ReturnType<typeof defineWorkspace> | undefined
}

if (!workspaceModule.default || !Array.isArray(workspaceModule.default)) {
Expand Down Expand Up @@ -629,7 +629,7 @@ export class Vitest {
this.config.related = undefined
})

return await this.runningPromise
return this.runningPromise
}

async collectFiles(specs: WorkspaceSpec[]) {
Expand Down Expand Up @@ -669,15 +669,15 @@ export class Vitest {
process.exitCode = 1
}
})()
.finally(async () => {
.finally(() => {
this.runningPromise = undefined

// all subsequent runs will treat this as a fresh run
this.config.changed = false
this.config.related = undefined
})

return await this.runningPromise
return this.runningPromise
}

async cancelCurrentRun(reason: CancelReason) {
Expand Down Expand Up @@ -762,7 +762,7 @@ export class Vitest {
}
}

private _rerunTimer: any
private _rerunTimer: NodeJS.Timer | undefined
private async scheduleRerun(triggerId: string[]) {
const currentCount = this.restartsCount
clearTimeout(this._rerunTimer)
Expand Down Expand Up @@ -856,7 +856,7 @@ export class Vitest {
this.updateLastChanged(id)
const needsRerun = this.handleFileChanged(id)
if (needsRerun.length) {
this.scheduleRerun(needsRerun)
void this.scheduleRerun(needsRerun)
}
}

Expand All @@ -870,7 +870,7 @@ export class Vitest {
this.cache.results.removeFromCache(id)
this.cache.stats.removeStats(id)
this.changedTests.delete(id)
this.report('onTestRemoved', id)
void this.report('onTestRemoved', id)
}
}

Expand All @@ -888,13 +888,13 @@ export class Vitest {

if (matchingProjects.length > 0) {
this.changedTests.add(id)
this.scheduleRerun([id])
void this.scheduleRerun([id])
}
else {
// it's possible that file was already there but watcher triggered "add" event instead
const needsRerun = this.handleFileChanged(id)
if (needsRerun.length) {
this.scheduleRerun(needsRerun)
void this.scheduleRerun(needsRerun)
}
}
}
Expand Down Expand Up @@ -980,7 +980,7 @@ export class Vitest {
})
}

if (rerun) {
if (rerun as boolean) {
files.push(filepath)
}
}
Expand Down Expand Up @@ -1016,7 +1016,7 @@ export class Vitest {
teardownProjects.push(this.coreWorkspaceProject)
}
// do teardown before closing the server
for await (const project of teardownProjects.reverse()) {
for (const project of teardownProjects.reverse()) {
await project.teardownGlobalSetup()
}

Expand Down
2 changes: 2 additions & 0 deletions packages/vitest/src/public/execute.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export { VitestExecutor } from '../runtime/execute'
export type { ExecuteOptions } from '../runtime/execute'
export type { VitestMocker } from '../runtime/mocker'
10 changes: 5 additions & 5 deletions packages/web-worker/src/runner.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { VitestExecutor } from 'vitest/execute'
import { type ExecuteOptions, VitestExecutor, type VitestMocker } from 'vitest/execute'

export class InlineWorkerRunner extends VitestExecutor {
constructor(options: any, private context: any) {
constructor(options: ExecuteOptions, private context: any) {
// share the same mocker as main executor
const mocker = (globalThis as any).__vitest_mocker__
const mocker = (globalThis as any).__vitest_mocker__ as VitestMocker
super(options)
this.mocker = (globalThis as any).__vitest_mocker__ = mocker
}

prepareContext(context: Record<string, any>) {
prepareContext(context: Record<string, unknown>) {
const ctx = super.prepareContext(context)
// not supported for now, we can't synchronously load modules
return Object.assign(ctx, this.context, {
importScripts,
})
}) as Record<string, unknown>
}
}

Expand Down
24 changes: 12 additions & 12 deletions packages/web-worker/src/shared-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function convertNodePortToWebPort(port: NodeMessagePort): MessagePort {
if (!('addEventListener' in port)) {
Object.defineProperty(port, 'addEventListener', {
value(...args: any[]) {
return this.addListener(...args)
return this.addListener(...args) as unknown
},
configurable: true,
enumerable: true,
Expand All @@ -19,14 +19,14 @@ function convertNodePortToWebPort(port: NodeMessagePort): MessagePort {
if (!('removeEventListener' in port)) {
Object.defineProperty(port, 'removeEventListener', {
value(...args: any[]) {
return this.removeListener(...args)
return this.removeListener(...args) as unknown
},
configurable: true,
enumerable: true,
})
}
if (!('dispatchEvent' in port)) {
const emit = (port as any).emit.bind(port)
const emit = (port as any).emit.bind(port) as (event: any) => boolean
Object.defineProperty(port, 'emit', {
value(event: any) {
if (event.name === 'message') {
Expand All @@ -42,7 +42,7 @@ function convertNodePortToWebPort(port: NodeMessagePort): MessagePort {
})
Object.defineProperty(port, 'dispatchEvent', {
value(event: any) {
return this.emit(event)
return this.emit(event) as unknown
},
configurable: true,
enumerable: true,
Expand Down Expand Up @@ -94,8 +94,8 @@ export function createSharedWorkerConstructor(): typeof SharedWorker {
dispatchEvent: (event: Event) => {
return this._vw_workerTarget.dispatchEvent(event)
},
addEventListener: (...args: any[]) => {
return this._vw_workerTarget.addEventListener(...args as [any, any])
addEventListener: (...args: Parameters<typeof addEventListener>) => {
return this._vw_workerTarget.addEventListener(...args)
},
removeEventListener: this._vw_workerTarget.removeEventListener,
get self() {
Expand All @@ -106,11 +106,11 @@ export function createSharedWorkerConstructor(): typeof SharedWorker {
selfProxy = new Proxy(context, {
get(target, prop, receiver) {
if (Reflect.has(target, prop)) {
return Reflect.get(target, prop, receiver)
return Reflect.get(target, prop, receiver) as unknown
}
return Reflect.get(globalThis, prop, receiver)
return Reflect.get(globalThis, prop, receiver) as unknown
},
}) as any
}) as unknown as typeof globalThis

const channel = new MessageChannel()
this.port = convertNodePortToWebPort(channel.port1)
Expand All @@ -135,7 +135,7 @@ export function createSharedWorkerConstructor(): typeof SharedWorker {

return runner.executeFile(fsPath).then(() => {
// worker should be new every time, invalidate its sub dependency
runnerOptions.moduleCache.invalidateSubDepTree([
runnerOptions.moduleCache!.invalidateSubDepTree([
fsPath,
runner.mocker.getMockPath(fsPath),
])
Expand All @@ -147,12 +147,12 @@ export function createSharedWorkerConstructor(): typeof SharedWorker {
debug('shared worker %s successfully initialized', this._vw_name)
})
})
.catch((e) => {
.catch((e: unknown) => {
debug('shared worker %s failed to initialize: %o', this._vw_name, e)
const EventConstructor = globalThis.ErrorEvent || globalThis.Event
const error = new EventConstructor('error', {
error: e,
message: e.message,
message: e instanceof Error ? e.message : undefined,
})
this.dispatchEvent(error)
this.onerror?.(error)
Expand Down
5 changes: 5 additions & 0 deletions packages/web-worker/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
export type Procedure = (...args: any[]) => void
export type CloneOption = 'native' | 'ponyfill' | 'none'

export type PostMessageArgs = [
unknown,
StructuredSerializeOptions | Transferable[] | undefined,
]

export interface DefineWorkerOptions {
clone: CloneOption
}
11 changes: 6 additions & 5 deletions packages/web-worker/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { WorkerGlobalState } from 'vitest'
import type { ExecuteOptions } from 'vitest/execute'
import type { CloneOption } from './types'
import { readFileSync as _readFileSync } from 'node:fs'
import ponyfillStructuredClone from '@ungap/structured-clone'
Expand All @@ -9,9 +10,9 @@ const readFileSync = _readFileSync

export const debug = createDebug('vitest:web-worker')

export function getWorkerState(): WorkerGlobalState {
function getWorkerState(): WorkerGlobalState {
// @ts-expect-error untyped global
return globalThis.__vitest_worker__
return globalThis.__vitest_worker__ as WorkerGlobalState
}

export function assertGlobalExists(name: string) {
Expand All @@ -23,7 +24,7 @@ export function assertGlobalExists(name: string) {
}

function createClonedMessageEvent(
data: any,
data: unknown,
transferOrOptions: StructuredSerializeOptions | Transferable[] | undefined,
clone: CloneOption,
) {
Expand Down Expand Up @@ -52,7 +53,7 @@ function createClonedMessageEvent(
)
}
return new MessageEvent('message', {
data: ponyfillStructuredClone(data, { lossy: true } as any),
data: ponyfillStructuredClone(data, { lossy: true }),
origin,
})
}
Expand All @@ -79,7 +80,7 @@ export function createMessageEvent(
}
}

export function getRunnerOptions(): any {
export function getRunnerOptions(): ExecuteOptions {
const state = getWorkerState()
const { config, rpc, moduleCache } = state

Expand Down
21 changes: 11 additions & 10 deletions packages/web-worker/src/worker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {
CloneOption,
DefineWorkerOptions,
PostMessageArgs,
Procedure,
} from './types'
import { InlineWorkerRunner } from './runner'
Expand Down Expand Up @@ -35,7 +36,7 @@ export function createWorkerConstructor(
>()

private _vw_name: string
private _vw_messageQueue: any[] | null = []
private _vw_messageQueue: PostMessageArgs[] | null = []

public onmessage: null | Procedure = null
public onmessageerror: null | Procedure = null
Expand Down Expand Up @@ -69,14 +70,14 @@ export function createWorkerConstructor(
dispatchEvent: (event: Event) => {
return this._vw_workerTarget.dispatchEvent(event)
},
addEventListener: (...args: any[]) => {
addEventListener: (...args: Parameters<typeof addEventListener>) => {
if (args[1]) {
this._vw_insideListeners.set(args[0], args[1])
}
return this._vw_workerTarget.addEventListener(...args as [any, any])
return this._vw_workerTarget.addEventListener(...args)
},
removeEventListener: this._vw_workerTarget.removeEventListener,
postMessage: (...args: any[]) => {
postMessage: (...args: Parameters<typeof postMessage>) => {
if (!args.length) {
throw new SyntaxError(
'"postMessage" requires at least one argument.',
Expand All @@ -99,11 +100,11 @@ export function createWorkerConstructor(
selfProxy = new Proxy(context, {
get(target, prop, receiver) {
if (Reflect.has(target, prop)) {
return Reflect.get(target, prop, receiver)
return Reflect.get(target, prop, receiver) as unknown
}
return globalThis[prop as 'crypto']
},
}) as any
}) as unknown as typeof globalThis

this._vw_workerTarget.addEventListener('message', (e) => {
context.onmessage?.(e as MessageEvent)
Expand Down Expand Up @@ -132,7 +133,7 @@ export function createWorkerConstructor(

return runner.executeFile(fsPath).then(() => {
// worker should be new every time, invalidate its sub dependency
runnerOptions.moduleCache.invalidateSubDepTree([
runnerOptions.moduleCache!.invalidateSubDepTree([
fsPath,
runner.mocker.getMockPath(fsPath),
])
Expand All @@ -147,12 +148,12 @@ export function createWorkerConstructor(
debug('worker %s successfully initialized', this._vw_name)
})
})
.catch((e) => {
.catch((e: unknown) => {
debug('worker %s failed to initialize: %o', this._vw_name, e)
const EventConstructor = globalThis.ErrorEvent || globalThis.Event
const error = new EventConstructor('error', {
error: e,
message: e.message,
message: e instanceof Error ? e.message : undefined,
})
this.dispatchEvent(error)
this.onerror?.(error)
Expand All @@ -172,7 +173,7 @@ export function createWorkerConstructor(
}

postMessage(
...args: [any, StructuredSerializeOptions | Transferable[] | undefined]
...args: PostMessageArgs
): void {
if (!args.length) {
throw new SyntaxError('"postMessage" requires at least one argument.')
Expand Down
6 changes: 3 additions & 3 deletions packages/ws-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ export function createClient(url: string, options: VitestClientOptions = {}) {
}

const birpcHandlers: BirpcOptions<WebSocketHandlers> = {
post: msg => ctx.ws.send(msg),
post: (msg: string) => ctx.ws.send(msg),
on: fn => (onMessage = fn),
serialize: e =>
stringify(e, (_, v) => {
serialize: (e: unknown) =>
stringify(e, (_, v: unknown) => {
if (v instanceof Error) {
return {
name: v.name,
Expand Down
Loading
Loading