From 2786b549d959f6ae8edfd4f0f590c3b94873f7d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kh=E1=BA=A3i?= Date: Sun, 14 Apr 2024 18:37:50 +0700 Subject: [PATCH] chore(eslint): explicit-module-boundary-types (#7926) --- __utils__/eslint-config/eslint.json | 1 + __utils__/test-ipc-server/src/TestIpcServer.ts | 8 ++++---- .../src/reporterForClient/utils/zooming.ts | 2 +- config/config/src/index.ts | 2 +- env/node.fetcher/src/index.ts | 4 ++-- .../plugin-commands-script-runners/src/exec.ts | 4 ++-- .../src/runRecursive.ts | 2 +- .../daemon/src/createFuseHandlers.ts | 2 +- packages/plugin-commands-init/src/init.ts | 2 +- .../src/patchCommit.ts | 2 +- pnpm/test/utils/distTags.ts | 4 ++-- pnpm/test/utils/isPortInUse.ts | 2 +- pnpm/test/utils/localPkg.ts | 4 ++-- reviewing/plugin-commands-listing/src/ll.ts | 2 +- reviewing/plugin-commands-listing/src/why.ts | 2 +- .../plugin-commands-outdated/src/utils.ts | 14 +++++++------- store/cafs/src/getFilePathInCafs.ts | 2 +- .../src/catIndex.ts | 2 +- store/store-path/src/index.ts | 2 +- .../filter-workspace-packages/src/index.ts | 18 ++++++++++++------ 20 files changed, 44 insertions(+), 37 deletions(-) diff --git a/__utils__/eslint-config/eslint.json b/__utils__/eslint-config/eslint.json index 39841f9cf27..bcc7cb5a5fe 100644 --- a/__utils__/eslint-config/eslint.json +++ b/__utils__/eslint-config/eslint.json @@ -61,6 +61,7 @@ "@typescript-eslint/no-unnecessary-type-assertion": "off", "@typescript-eslint/type-annotation-spacing": "error", "@typescript-eslint/ban-ts-comment": "off", + "@typescript-eslint/explicit-module-boundary-types": "error", "brace-style": [ "error", "1tbs" diff --git a/__utils__/test-ipc-server/src/TestIpcServer.ts b/__utils__/test-ipc-server/src/TestIpcServer.ts index d26a1d91c7d..2630a30fb82 100644 --- a/__utils__/test-ipc-server/src/TestIpcServer.ts +++ b/__utils__/test-ipc-server/src/TestIpcServer.ts @@ -76,7 +76,7 @@ export class TestIpcServer implements AsyncDisposable { /** * Reset the buffer to an empty string. */ - public clear () { + public clear (): void { this.buffer = '' } @@ -84,7 +84,7 @@ export class TestIpcServer implements AsyncDisposable { * Generates a shell script that can used as a package manifest "scripts" * entry. Exits after sending the message. */ - public sendLineScript (message: string) { + public sendLineScript (message: string): string { return `node -e "const c = require('net').connect('${JSON.stringify(this.listenPath).slice(1, -1)}', () => { c.write('${message}\\n'); c.end(); })"` } @@ -92,11 +92,11 @@ export class TestIpcServer implements AsyncDisposable { * Generates a shell script that can used as a package manifest "scripts" * entry. This script consumes its stdin and sends it to the server. */ - public generateSendStdinScript () { + public generateSendStdinScript (): string { return `node -e "const c = require('net').connect('${JSON.stringify(this.listenPath).slice(1, -1)}', () => { process.stdin.pipe(c).on('end', () => { c.destroy(); }); })"` } - public [Symbol.asyncDispose] = async () => { + public [Symbol.asyncDispose] = async (): Promise => { const close = promisify(this.server.close).bind(this.server) await close() } diff --git a/cli/default-reporter/src/reporterForClient/utils/zooming.ts b/cli/default-reporter/src/reporterForClient/utils/zooming.ts index 52a492de8db..c0137cb3a14 100644 --- a/cli/default-reporter/src/reporterForClient/utils/zooming.ts +++ b/cli/default-reporter/src/reporterForClient/utils/zooming.ts @@ -16,6 +16,6 @@ export function autozoom ( return zoomOut(currentPrefix, logPrefix, line) } -export function zoomOut (currentPrefix: string, logPrefix: string, line: string) { +export function zoomOut (currentPrefix: string, logPrefix: string, line: string): string { return `${rightPad(formatPrefix(currentPrefix, logPrefix), PREFIX_MAX_LENGTH)} | ${line}` } diff --git a/config/config/src/index.ts b/config/config/src/index.ts index 0ab67b1917c..7d16294c5c2 100644 --- a/config/config/src/index.ts +++ b/config/config/src/index.ts @@ -149,7 +149,7 @@ export const types = Object.assign({ 'update-notifier': Boolean, 'registry-supports-time-field': Boolean, 'fail-if-no-match': Boolean, - // eslint-disable-next-line @typescript-eslint/no-explicit-any + }, npmTypes.types) export type CliOptions = Record & { dir?: string, json?: boolean } diff --git a/env/node.fetcher/src/index.ts b/env/node.fetcher/src/index.ts index bfbc20eedd9..e131d40fcae 100644 --- a/env/node.fetcher/src/index.ts +++ b/env/node.fetcher/src/index.ts @@ -21,7 +21,7 @@ export interface FetchNodeOptions { retry?: RetryTimeoutOptions } -export async function fetchNode (fetch: FetchFromRegistry, version: string, targetDir: string, opts: FetchNodeOptions) { +export async function fetchNode (fetch: FetchFromRegistry, version: string, targetDir: string, opts: FetchNodeOptions): Promise { if (await isNonGlibcLinux()) { throw new PnpmError('MUSL', 'The current system uses the "MUSL" C standard library. Node.js currently has prebuilt artifacts only for the "glibc" libc, so we can install Node.js only for glibc') } @@ -61,7 +61,7 @@ async function downloadAndUnpackZip ( zipUrl: string, targetDir: string, pkgName: string -) { +): Promise { const response = await fetchFromRegistry(zipUrl) const tmp = path.join(tempy.directory(), 'pnpm.zip') const dest = fs.createWriteStream(tmp) diff --git a/exec/plugin-commands-script-runners/src/exec.ts b/exec/plugin-commands-script-runners/src/exec.ts index 2c732fa9205..221a0943601 100644 --- a/exec/plugin-commands-script-runners/src/exec.ts +++ b/exec/plugin-commands-script-runners/src/exec.ts @@ -93,7 +93,7 @@ export function getResumedPackageChunks ({ resumeFrom: string chunks: string[][] selectedProjectsGraph: ProjectsGraph -}) { +}): string[][] { const resumeFromPackagePrefix = Object.keys(selectedProjectsGraph) .find((prefix) => selectedProjectsGraph[prefix]?.package.manifest.name === resumeFrom) @@ -105,7 +105,7 @@ export function getResumedPackageChunks ({ return chunks.slice(chunkPosition) } -export async function writeRecursiveSummary (opts: { dir: string, summary: RecursiveSummary }) { +export async function writeRecursiveSummary (opts: { dir: string, summary: RecursiveSummary }): Promise { await writeJsonFile(path.join(opts.dir, 'pnpm-exec-summary.json'), { executionStatus: opts.summary, }) diff --git a/exec/plugin-commands-script-runners/src/runRecursive.ts b/exec/plugin-commands-script-runners/src/runRecursive.ts index 0ccd5c75080..d817b37f57f 100644 --- a/exec/plugin-commands-script-runners/src/runRecursive.ts +++ b/exec/plugin-commands-script-runners/src/runRecursive.ts @@ -203,7 +203,7 @@ function formatSectionName ({ return `${name ?? 'unknown'}${version ? `@${version}` : ''} ${script ? `: ${script}` : ''} ${prefix}` } -export function getSpecifiedScripts (scripts: PackageScripts, scriptName: string) { +export function getSpecifiedScripts (scripts: PackageScripts, scriptName: string): string[] { // if scripts in package.json has script which is equal to scriptName a user passes, return it. if (scripts[scriptName]) { return [scriptName] diff --git a/modules-mounter/daemon/src/createFuseHandlers.ts b/modules-mounter/daemon/src/createFuseHandlers.ts index cb89613ac8b..5c1875b4536 100644 --- a/modules-mounter/daemon/src/createFuseHandlers.ts +++ b/modules-mounter/daemon/src/createFuseHandlers.ts @@ -37,7 +37,7 @@ export async function createFuseHandlers (lockfileDir: string, cafsDir: string): return createFuseHandlersFromLockfile(lockfile, cafsDir) } -export function createFuseHandlersFromLockfile (lockfile: Lockfile, cafsDir: string) { +export function createFuseHandlersFromLockfile (lockfile: Lockfile, cafsDir: string): FuseHandlers { const pkgSnapshotCache = new Map() const virtualNodeModules = makeVirtualNodeModules(lockfile) return { diff --git a/packages/plugin-commands-init/src/init.ts b/packages/plugin-commands-init/src/init.ts index 408a5d1e491..226e1c83e84 100644 --- a/packages/plugin-commands-init/src/init.ts +++ b/packages/plugin-commands-init/src/init.ts @@ -9,7 +9,7 @@ import { parseRawConfig } from './utils' export const rcOptionsTypes = cliOptionsTypes -export function cliOptionsTypes () { +export function cliOptionsTypes (): Record { return {} } diff --git a/patching/plugin-commands-patching/src/patchCommit.ts b/patching/plugin-commands-patching/src/patchCommit.ts index a878dd3209f..9abfd6177be 100644 --- a/patching/plugin-commands-patching/src/patchCommit.ts +++ b/patching/plugin-commands-patching/src/patchCommit.ts @@ -20,7 +20,7 @@ import { type GetPatchedDependencyOptions, getVersionsFromLockfile } from './get export const rcOptionsTypes = cliOptionsTypes -export function cliOptionsTypes () { +export function cliOptionsTypes (): Record { return pick(['patches-dir'], allTypes) } diff --git a/pnpm/test/utils/distTags.ts b/pnpm/test/utils/distTags.ts index 6cd437a7648..286a1af76d7 100644 --- a/pnpm/test/utils/distTags.ts +++ b/pnpm/test/utils/distTags.ts @@ -1,5 +1,5 @@ import { addDistTag } from '@pnpm/registry-mock' -export async function add (packageName: string, version: string, distTag: string) { - return addDistTag({ package: packageName, version, distTag }) +export async function add (packageName: string, version: string, distTag: string): Promise { + await addDistTag({ package: packageName, version, distTag }) } diff --git a/pnpm/test/utils/isPortInUse.ts b/pnpm/test/utils/isPortInUse.ts index e745b1857f6..be4803c65e1 100644 --- a/pnpm/test/utils/isPortInUse.ts +++ b/pnpm/test/utils/isPortInUse.ts @@ -1,6 +1,6 @@ import { createServer } from 'net' -export const isPortInUse = (port: number) => new Promise((resolve, reject) => { +export const isPortInUse = (port: number): Promise => new Promise((resolve, reject) => { const server = createServer() // eslint-disable-next-line @typescript-eslint/no-explicit-any server.once('error', (err: any) => { diff --git a/pnpm/test/utils/localPkg.ts b/pnpm/test/utils/localPkg.ts index d45f96789a6..54e52bdd2fc 100644 --- a/pnpm/test/utils/localPkg.ts +++ b/pnpm/test/utils/localPkg.ts @@ -1,9 +1,9 @@ import path from 'path' -export function pathToLocalPkg (pkgName: string) { +export function pathToLocalPkg (pkgName: string): string { return path.join(__dirname, '../../../../fixtures', pkgName) } -export function local (pkgName: string) { +export function local (pkgName: string): string { return `file:${pathToLocalPkg(pkgName)}` } diff --git a/reviewing/plugin-commands-listing/src/ll.ts b/reviewing/plugin-commands-listing/src/ll.ts index 628a6e79747..ad6c26b23b3 100644 --- a/reviewing/plugin-commands-listing/src/ll.ts +++ b/reviewing/plugin-commands-listing/src/ll.ts @@ -5,7 +5,7 @@ export const commandNames = ['ll', 'la'] export const rcOptionsTypes = list.rcOptionsTypes -export function cliOptionsTypes () { +export function cliOptionsTypes (): Record { return omit(['long'], list.cliOptionsTypes()) } diff --git a/reviewing/plugin-commands-listing/src/why.ts b/reviewing/plugin-commands-listing/src/why.ts index 5d7e3be3786..f70bacfcf74 100644 --- a/reviewing/plugin-commands-listing/src/why.ts +++ b/reviewing/plugin-commands-listing/src/why.ts @@ -100,7 +100,7 @@ For options that may be used with `-r`, see "pnpm help recursive"', export async function handler ( opts: ListCommandOptions, params: string[] -) { +): Promise { if (params.length === 0) { throw new PnpmError('MISSING_PACKAGE_NAME', '`pnpm why` requires the package name') } diff --git a/reviewing/plugin-commands-outdated/src/utils.ts b/reviewing/plugin-commands-outdated/src/utils.ts index bdaad726154..2425752417e 100644 --- a/reviewing/plugin-commands-outdated/src/utils.ts +++ b/reviewing/plugin-commands-outdated/src/utils.ts @@ -6,22 +6,22 @@ export interface OutdatedWithVersionDiff extends OutdatedPackage { diff?: [string[], string[]] } +export type Comparator = (o1: OutdatedWithVersionDiff, o2: OutdatedWithVersionDiff) => number + /** * Default comparators used as the argument to `ramda.sortWith()`. */ -export const DEFAULT_COMPARATORS = [ +export const DEFAULT_COMPARATORS: Comparator[] = [ sortBySemverChange, - (o1: OutdatedWithVersionDiff, o2: OutdatedWithVersionDiff) => - o1.packageName.localeCompare(o2.packageName), - (o1: OutdatedWithVersionDiff, o2: OutdatedWithVersionDiff) => - (o1.current && o2.current) ? o1.current.localeCompare(o2.current) : 0, + (o1, o2) => o1.packageName.localeCompare(o2.packageName), // eslint-disable-line @typescript-eslint/explicit-module-boundary-types + (o1, o2) => (o1.current && o2.current) ? o1.current.localeCompare(o2.current) : 0, // eslint-disable-line @typescript-eslint/explicit-module-boundary-types ] -export function sortBySemverChange (outdated1: OutdatedWithVersionDiff, outdated2: OutdatedWithVersionDiff) { +export function sortBySemverChange (outdated1: OutdatedWithVersionDiff, outdated2: OutdatedWithVersionDiff): number { return pkgPriority(outdated1) - pkgPriority(outdated2) } -function pkgPriority (pkg: OutdatedWithVersionDiff) { +function pkgPriority (pkg: OutdatedWithVersionDiff): number { switch (pkg.change) { case null: return 0 case 'fix': return 1 diff --git a/store/cafs/src/getFilePathInCafs.ts b/store/cafs/src/getFilePathInCafs.ts index 819c24c54f2..09ac34936a7 100644 --- a/store/cafs/src/getFilePathInCafs.ts +++ b/store/cafs/src/getFilePathInCafs.ts @@ -1,7 +1,7 @@ import path from 'path' import ssri, { type IntegrityLike } from 'ssri' -export const modeIsExecutable = (mode: number) => (mode & 0o111) === 0o111 +export const modeIsExecutable = (mode: number): boolean => (mode & 0o111) === 0o111 export type FileType = 'exec' | 'nonexec' | 'index' diff --git a/store/plugin-commands-store-inspecting/src/catIndex.ts b/store/plugin-commands-store-inspecting/src/catIndex.ts index fb1c195a45b..5417c664244 100644 --- a/store/plugin-commands-store-inspecting/src/catIndex.ts +++ b/store/plugin-commands-store-inspecting/src/catIndex.ts @@ -18,7 +18,7 @@ export const commandNames = ['cat-index'] export const rcOptionsTypes = cliOptionsTypes -export function cliOptionsTypes () { +export function cliOptionsTypes (): Record { return {} } diff --git a/store/store-path/src/index.ts b/store/store-path/src/index.ts index b6e5a1f308e..6b6b02b954b 100644 --- a/store/store-path/src/index.ts +++ b/store/store-path/src/index.ts @@ -21,7 +21,7 @@ export function getStorePath ( storePath?: string pnpmHomeDir: string } -) { +): string | Promise { if (!storePath) { if (!pnpmHomeDir) { throw new PnpmError('NO_PNPM_HOME_DIR', 'The pnpm home directory is unknown. Cannot calculate the store directory location.') diff --git a/workspace/filter-workspace-packages/src/index.ts b/workspace/filter-workspace-packages/src/index.ts index 110c61e31be..5f8be863730 100644 --- a/workspace/filter-workspace-packages/src/index.ts +++ b/workspace/filter-workspace-packages/src/index.ts @@ -69,6 +69,10 @@ export interface FilterPackagesOptions { sharedWorkspaceLockfile?: boolean } +export interface FilterPackagesFromDirResult extends FilterPackagesResult { + allProjects: Project[] +} + export async function filterPackagesFromDir ( workspaceDir: string, filter: WorkspaceFilter[], @@ -78,7 +82,7 @@ export async function filterPackagesFromDir ( patterns: string[] supportedArchitectures?: SupportedArchitectures } -) { +): Promise { const allProjects = await findWorkspacePackages(workspaceDir, { engineStrict: opts?.engineStrict, patterns: opts.patterns, @@ -92,15 +96,17 @@ export async function filterPackagesFromDir ( } } +export interface FilterPackagesResult { + allProjectsGraph: PackageGraph + selectedProjectsGraph: PackageGraph + unmatchedFilters: string[] +} + export async function filterPackages ( pkgs: Pkg[], filter: WorkspaceFilter[], opts: FilterPackagesOptions -): Promise<{ - allProjectsGraph: PackageGraph - selectedProjectsGraph: PackageGraph - unmatchedFilters: string[] - }> { +): Promise> { const packageSelectors = filter.map(({ filter: f, followProdDepsOnly }) => ({ ...parsePackageSelector(f, opts.prefix), followProdDepsOnly })) return filterPkgsBySelectorObjects(pkgs, packageSelectors, opts)