Files
Zoltan Kochan 4edeef9176 style(ts): port the perfectionist lint rules to ESLint (#16415)
Add TypeScript ports of the perfectionist rules that the Rust workspace
enforces through dylint.toml to @pnpm/eslint-config, with the same limits,
and fix every finding that needs no refactor.

The size and shape rules (function length, cognitive complexity, nesting,
local bindings, file length) are enforced for packages that already meet
them. The packages that do not are listed in PENDING_SIZE_AND_SHAPE_REFACTOR
in eslint.config.mjs and are refactored in follow-up changes.

The custom rules mirror perfectionist's semantics rather than reusing
sonarjs or max-lines-per-function, so that one limit means the same thing
in both codebases: a closure is a level of the body that contains it, a
nested function declaration is measured on its own, and a test-framework
callback plays the role of a #[test] function.

Bugs found and fixed along the way:
- a registry error body cut at exactly 64 KiB lost its truncation marker
- removing a dependency could leave dangling .bin shims
- constructor/__proto__ names crashed the projects graph, were skipped by
  the injected-deps syncer, and were dropped from writeWantedLockfile's
  returned lockfile

The CI chunk runner learns to run the node:test suite of the ESLint
config package.
2026-09-30 15:10:08 +02:00

84 lines
2.6 KiB
TypeScript

import { type ChildProcess, spawn } from 'node:child_process'
import path from 'node:path'
import { expect, test } from '@jest/globals'
import { killTrackedProcessTrees, trackChildProcess } from '@pnpm/exec.lifecycle'
import isWindows from 'is-windows'
const parentScript = path.join(import.meta.dirname, 'fixtures/process-tree/parent.cjs')
test('killTrackedProcessTrees() kills a tracked child process and, on Windows, its descendants', async () => {
const child = spawn(process.execPath, [parentScript], { stdio: ['ignore', 'pipe', 'ignore'] })
// Wait for 'close' rather than 'exit': the tracker untracks the PID on
// 'close', so awaiting it guarantees the registry is empty before the next
// test, whose assertion relies on it being so.
const childClosed = new Promise<void>((resolve) => {
child.once('close', () => {
resolve()
})
})
const grandchildPid = await readGrandchildPid(child)
try {
trackChildProcess(child)
await killTrackedProcessTrees()
await childClosed
expect(child.exitCode !== 0 || child.signalCode != null).toBe(true)
if (isWindows()) {
expect(await exited(grandchildPid)).toBe(true)
}
} finally {
killSilently(child.pid!)
killSilently(grandchildPid)
}
})
test('killTrackedProcessTrees() is a no-op for a tracked child process that already exited', async () => {
const child = spawn(process.execPath, ['-e', ''], { stdio: 'ignore' })
trackChildProcess(child)
await new Promise<void>((resolve) => {
child.once('close', () => {
resolve()
})
})
await killTrackedProcessTrees()
})
async function readGrandchildPid (child: ChildProcess): Promise<number> {
const firstLine = await new Promise<string>((resolve, reject) => {
let output = ''
child.stdout!.on('data', (data: Buffer) => {
output += data.toString()
if (output.includes('\n')) {
resolve(output)
}
})
child.once('error', reject)
child.once('exit', () => {
reject(new Error(`the spawned process tree exited early: ${output}`))
})
})
return parseInt(firstLine, 10)
}
async function exited (pid: number, timeoutMs: number = 10_000): Promise<boolean> {
const deadline = Date.now() + timeoutMs
while (Date.now() < deadline) {
try {
process.kill(pid, 0)
} catch {
return true
}
await new Promise<void>((resolve) => setTimeout(resolve, 100)) // eslint-disable-line no-await-in-loop -- polling: each check must wait for the previous delay
}
return false
}
function killSilently (pid: number): void {
try {
process.kill(pid, 'SIGKILL')
} catch {
// the process has already exited
}
}