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.8 KiB
TypeScript

import fs from 'node:fs'
import path from 'node:path'
import { expect, jest, test } from '@jest/globals'
import { fixtures } from '@pnpm/test-fixtures'
jest.unstable_mockModule('is-windows', () => ({ default: () => true }))
const { runLifecycleHook } = await import('../lib/index.js')
const testFixtures = fixtures(path.join(import.meta.dirname, 'fixtures'))
const rootModulesDir = path.join(import.meta.dirname, '..', 'node_modules')
// `is-windows` is mocked because the branch under test is otherwise
// reachable only from a Windows host.
test('runLifecycleHook() quotes arguments for the emulator rather than for cmd on Windows', async () => {
const pkgRoot = testFixtures.prepare('escape-args')
const { default: pkg } = await import(path.join(pkgRoot, 'package.json'))
const args = [
'C:\\Program Files\\tool\\',
'',
'a"b',
"it's",
'$PNPM_QUOTING_TEST',
'line\nbreak',
]
await runLifecycleHook('echo', pkg, {
args,
depPath: '/escape-args/1.0.0',
extraEnv: { PNPM_QUOTING_TEST: 'expanded' },
pkgRoot,
rootModulesDir,
shellEmulator: true,
unsafePerm: true,
})
const recorded = JSON.parse(await fs.promises.readFile(path.join(pkgRoot, 'output.json'), 'utf8'))
expect(recorded).toStrictEqual(args)
})
// cmd's quoting would leave `$PNPM_QUOTING_TEST` for sh to expand,
// so the recorded arguments show which shell the quoting was chosen for.
// The mocked `is-windows` already runs the Windows quoting branch here.
// A real Windows host has no POSIX shell at a fixed path to configure.
const skipOnRealWindows = process.platform === 'win32' ? test.skip : test
skipOnRealWindows.each([false, true])('runLifecycleHook() quotes arguments for a configured non-cmd scriptShell on Windows (shellEmulator: %s)', async (shellEmulator) => {
const pkgRoot = testFixtures.prepare('escape-args')
const { default: pkg } = await import(path.join(pkgRoot, 'package.json'))
const args = [
'C:\\Program Files\\tool\\',
'a"b',
"it's",
'$PNPM_QUOTING_TEST',
]
await runLifecycleHook('echo', pkg, {
args,
depPath: '/escape-args/1.0.0',
extraEnv: { PNPM_QUOTING_TEST: 'expanded' },
pkgRoot,
rootModulesDir,
scriptShell: '/bin/sh',
shellEmulator,
unsafePerm: true,
})
const recorded = JSON.parse(await fs.promises.readFile(path.join(pkgRoot, 'output.json'), 'utf8'))
expect(recorded).toStrictEqual(args)
})
test('runLifecycleHook() shows the arguments quoted the POSIX way when cmd quoting runs', async () => {
const pkgRoot = testFixtures.prepare('escape-args')
await expect(runLifecycleHook('fail', { name: 'fail', version: '1.0.0', scripts: { fail: 'node -e "process.exit(1)"' } }, {
args: ['a b', '%PATH%'],
depPath: '/fail/1.0.0',
pkgRoot,
rootModulesDir,
unsafePerm: true,
})).rejects.toThrow('fail@1.0.0 fail: `node -e "process.exit(1)" \'a b\' %PATH%`')
})