From ec5fa38cbd299ac91b4fa30d023ea701eac6dd0f Mon Sep 17 00:00:00 2001 From: Richard Gibson Date: Thu, 14 Nov 2024 01:22:55 -0500 Subject: [PATCH 1/2] chore(agoric-cli): Type-check *.js files --- packages/agoric-cli/tsconfig.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/agoric-cli/tsconfig.json b/packages/agoric-cli/tsconfig.json index 392e85ad58f..b0a3738ead7 100644 --- a/packages/agoric-cli/tsconfig.json +++ b/packages/agoric-cli/tsconfig.json @@ -2,7 +2,7 @@ { "extends": "../../tsconfig.json", "compilerOptions": { - "checkJs": false, + "checkJs": true, }, "include": [ "*.js", From 7a783fa04f229cfa86273c948b406e4c0f9191e5 Mon Sep 17 00:00:00 2001 From: Richard Gibson Date: Thu, 14 Nov 2024 01:22:55 -0500 Subject: [PATCH 2/2] chore(agoric-cli): Fix type errors --- packages/agoric-cli/src/anylogger-agoric.js | 4 +-- packages/agoric-cli/src/chain-config.js | 25 ++++++++++++++++--- packages/agoric-cli/src/commands/wallet.js | 4 ++- packages/agoric-cli/src/cosmos.js | 2 +- packages/agoric-cli/src/helpers.js | 10 ++++---- packages/agoric-cli/src/install.js | 12 ++++++--- packages/agoric-cli/src/lib/network-config.js | 4 +++ packages/agoric-cli/src/main.js | 4 ++- packages/agoric-cli/src/scripts.js | 2 +- packages/agoric-cli/src/start.js | 14 +++++------ .../agoric-cli/test/bundles-regExp.test.js | 1 + packages/agoric-cli/test/main.test.js | 1 + .../test/upgrade-contract/init-proposal.js | 2 +- 13 files changed, 59 insertions(+), 26 deletions(-) diff --git a/packages/agoric-cli/src/anylogger-agoric.js b/packages/agoric-cli/src/anylogger-agoric.js index d4db4f2a315..87bc1bb679f 100644 --- a/packages/agoric-cli/src/anylogger-agoric.js +++ b/packages/agoric-cli/src/anylogger-agoric.js @@ -28,8 +28,8 @@ const selectedCode = anylogger.levels[selectedLevel]; const globalCode = selectedCode === undefined ? -Infinity : selectedCode; const oldExt = anylogger.ext; -anylogger.ext = (l, o) => { - l = oldExt(l, o); +anylogger.ext = (l, ...rest) => { + l = oldExt(l, ...rest); l.enabledFor = lvl => globalCode >= anylogger.levels[lvl]; const prefix = l.name.replace(/:/g, ': '); diff --git a/packages/agoric-cli/src/chain-config.js b/packages/agoric-cli/src/chain-config.js index e9bc39f7376..6f90e854b33 100644 --- a/packages/agoric-cli/src/chain-config.js +++ b/packages/agoric-cli/src/chain-config.js @@ -81,7 +81,12 @@ export const DEFAULT_RPC_PORT = 26657; export const DEFAULT_PROM_PORT = 26660; export const DEFAULT_API_PORT = 1317; -// Rewrite the app.toml. +/** + * Rewrite the app.toml. + * + * @param {{ appToml: string, enableCors?: boolean, exportMetrics?: boolean, portNum?: string, chainId?: string, enableRosetta?: boolean, rosettaPort?: string }} input + * @returns {string} toml + */ export function finishCosmosApp({ appToml, enableCors, @@ -92,6 +97,8 @@ export function finishCosmosApp({ rosettaPort = `${DEFAULT_ROSETTA_PORT}`, }) { const rpcPort = Number(portNum); + // TODO: Use an accurate narrow type. + /** @type {Record} */ const app = TOML.parse(appToml); if (enableCors) { @@ -138,7 +145,12 @@ export function finishCosmosApp({ return TOML.stringify(app); } -// Rewrite the config.toml. +/** + * Rewrite the config.toml. + * + * @param {{ configToml: string, enableCors?: boolean, exportMetrics?: boolean, portNum?: string, persistentPeers?: string, seeds?: string, unconditionalPeerIds?: string }} input + * @returns {string} toml + */ export function finishTendermintConfig({ configToml, enableCors, @@ -151,6 +163,8 @@ export function finishTendermintConfig({ const rpcPort = Number(portNum); // Adjust the config.toml. + // TODO: Use an accurate narrow type. + /** @type {Record} */ const config = TOML.parse(configToml); config.proxy_app = 'kvstore'; @@ -189,7 +203,12 @@ export function finishTendermintConfig({ return TOML.stringify(config); } -// Rewrite/import the genesis.json. +/** + * Rewrite/import the genesis.json. + * + * @param {{ genesisJson: string, exportedGenesisJson?: string }} input + * @returns {string} json + */ export function finishCosmosGenesis({ genesisJson, exportedGenesisJson }) { const genesis = JSON.parse(genesisJson); const exported = exportedGenesisJson ? JSON.parse(exportedGenesisJson) : {}; diff --git a/packages/agoric-cli/src/commands/wallet.js b/packages/agoric-cli/src/commands/wallet.js index 440fe2452d5..d6747b43dc0 100644 --- a/packages/agoric-cli/src/commands/wallet.js +++ b/packages/agoric-cli/src/commands/wallet.js @@ -175,7 +175,9 @@ export const makeWalletCommand = async command => { keyring: { home, backend }, from, gas: - gas === 'auto' ? ['auto', parseFiniteNumber(gasAdjustment)] : gas, + gas === 'auto' + ? ['auto', parseFiniteNumber(gasAdjustment)] + : parseFiniteNumber(gas), dryRun, verbose, }, diff --git a/packages/agoric-cli/src/cosmos.js b/packages/agoric-cli/src/cosmos.js index 147cda627d0..5c4aefeca55 100644 --- a/packages/agoric-cli/src/cosmos.js +++ b/packages/agoric-cli/src/cosmos.js @@ -50,7 +50,7 @@ export default async function cosmosMain(progname, rawArgs, powers, opts) { }, ); // Ensure the build doesn't mess up stdout. - ps.childProcess.stdout.pipe(process.stderr); + ps.childProcess.stdout?.pipe(process.stderr); return ps; } throw e; diff --git a/packages/agoric-cli/src/helpers.js b/packages/agoric-cli/src/helpers.js index 845da5536ae..37183a0ba35 100644 --- a/packages/agoric-cli/src/helpers.js +++ b/packages/agoric-cli/src/helpers.js @@ -42,12 +42,12 @@ export const makePspawn = ({ * * @param {string} cmd command name to run * @param {Array} cargs arguments to the command - * @param {object} param2 - * @param {string} [param2.cwd] - * @param {string | [string, string, string]} [param2.stdio] standard IO + * @param {object} [opts] + * @param {string} [opts.cwd] + * @param {string | [string, string, string]} [opts.stdio] standard IO * specification - * @param {Record} [param2.env] environment - * @param {boolean} [param2.detached] whether the child process should be detached + * @param {Record} [opts.env] environment + * @param {boolean} [opts.detached] whether the child process should be detached * @returns {Promise & { childProcess: ChildProcess }}} promise for * exit status. The return result has a `childProcess` property to obtain * control over the running process diff --git a/packages/agoric-cli/src/install.js b/packages/agoric-cli/src/install.js index ed549517db4..c42eb125780 100644 --- a/packages/agoric-cli/src/install.js +++ b/packages/agoric-cli/src/install.js @@ -37,7 +37,7 @@ export default async function installMain(progname, rawArgs, powers, opts) { stdio: ['inherit', 'pipe', 'inherit'], }); const stdout = []; - p.childProcess.stdout.on('data', out => stdout.push(out)); + p.childProcess.stdout?.on('data', out => stdout.push(out)); await p; const d = JSON.parse(Buffer.concat(stdout).toString('utf-8')); for (const [name, { location }] of Object.entries(d)) { @@ -49,7 +49,7 @@ export default async function installMain(progname, rawArgs, powers, opts) { let subdirs; const workTrees = ['.']; let sdkWorktree; - /** @type {Map} */ + /** @type {Map} */ const sdkPackageToPath = new Map(); const linkFolder = path.resolve(`_agstate/yarn-links`); const linkFlags = []; @@ -170,7 +170,7 @@ export default async function installMain(progname, rawArgs, powers, opts) { .then(results => { // After all have settled, throw any errors. const failures = results.filter( - ({ status }) => status !== 'fulfilled', + result => result.status !== 'fulfilled', ); if (failures.length) { throw AggregateError( @@ -284,6 +284,10 @@ export default async function installMain(progname, rawArgs, powers, opts) { // Create symlinks to the SDK packages. await Promise.all( [...sdkPackageToPath.entries()].map(async ([pjName, dir]) => { + if (typeof dir !== 'string') { + throw Error(`unexpected incomplete package mapping: ${pjName}`); + } + const SUBOPTIMAL = false; await null; if (SUBOPTIMAL) { @@ -301,7 +305,7 @@ export default async function installMain(progname, rawArgs, powers, opts) { log('linking', linkName); return fs .mkdir(linkDir, { recursive: true }) - .then(_ => fs.symlink(path.relative(linkDir, dir), linkName)); + .then(() => fs.symlink(path.relative(linkDir, dir), linkName)); }), ); diff --git a/packages/agoric-cli/src/lib/network-config.js b/packages/agoric-cli/src/lib/network-config.js index ca0485383f8..8fae9b51f2d 100644 --- a/packages/agoric-cli/src/lib/network-config.js +++ b/packages/agoric-cli/src/lib/network-config.js @@ -1,5 +1,9 @@ import { NonNullish } from '@agoric/internal'; +/** + * @import {MinimalNetworkConfig} from '@agoric/client-utils'; + */ + export const networkConfigUrl = agoricNetSubdomain => `https://${agoricNetSubdomain}.agoric.net/network-config`; export const rpcUrl = agoricNetSubdomain => diff --git a/packages/agoric-cli/src/main.js b/packages/agoric-cli/src/main.js index c9df6c9f903..af834f16964 100644 --- a/packages/agoric-cli/src/main.js +++ b/packages/agoric-cli/src/main.js @@ -66,7 +66,9 @@ const main = async (progname, rawArgs, powers) => { 'verbosity that can be increased', (_value, _previous) => (cmdOpts.verbose += 1), ); - const baseCmd = (...args) => addCmdOpts(program.command(...args)); + /** @type {typeof program.command} */ + const baseCmd = (nameAndParams, ...rest) => + addCmdOpts(program.command(nameAndParams, ...rest)); addCmdOpts( program diff --git a/packages/agoric-cli/src/scripts.js b/packages/agoric-cli/src/scripts.js index 9be651984ba..bb921c20739 100644 --- a/packages/agoric-cli/src/scripts.js +++ b/packages/agoric-cli/src/scripts.js @@ -56,7 +56,7 @@ export const makeLookup = /** * @param {string[]} scripts - * @param {{ allowUnsafePlugins: boolean, progname: string, rawArgs: string[], endowments?: Record }} opts + * @param {{ allowUnsafePlugins?: boolean, progname: string, rawArgs: string[], endowments?: Record }} opts * @param {{ fs: import('fs/promises'), console: Console }} powers */ export const makeScriptLoader = diff --git a/packages/agoric-cli/src/start.js b/packages/agoric-cli/src/start.js index 5f5f81382a5..4832c7cd555 100644 --- a/packages/agoric-cli/src/start.js +++ b/packages/agoric-cli/src/start.js @@ -276,13 +276,13 @@ export default async function startMain(progname, rawArgs, powers, opts) { await rmVerbose(serverDir); } + /** @type {(args: string[], spawnOpts?: Parameters[2], dockerArgs?: string[]) => ReturnType} */ let chainSpawn; if (!popts.dockerTag) { - chainSpawn = (args, spawnOpts = undefined) => { - return pspawn(cosmosChain, [...args, `--home=${serverDir}`], spawnOpts); - }; + chainSpawn = (args, spawnOpts) => + pspawn(cosmosChain, [...args, `--home=${serverDir}`], spawnOpts); } else { - chainSpawn = (args, spawnOpts = undefined, dockerArgs = []) => + chainSpawn = (args, spawnOpts, dockerArgs = []) => pspawn( 'docker', [ @@ -482,12 +482,12 @@ export default async function startMain(progname, rawArgs, powers, opts) { await rmVerbose(serverDir); } + /** @type {(args: string[], spawnOpts?: Parameters[2], dockerArgs?: string[]) => ReturnType} */ let soloSpawn; if (!popts.dockerTag) { - soloSpawn = (args, spawnOpts = undefined) => - pspawn(agSolo, args, spawnOpts); + soloSpawn = (args, spawnOpts) => pspawn(agSolo, args, spawnOpts); } else { - soloSpawn = (args, spawnOpts = undefined, dockerArgs = []) => + soloSpawn = (args, spawnOpts, dockerArgs = []) => pspawn( 'docker', [ diff --git a/packages/agoric-cli/test/bundles-regExp.test.js b/packages/agoric-cli/test/bundles-regExp.test.js index 87b6f040b14..49aeb63e39d 100644 --- a/packages/agoric-cli/test/bundles-regExp.test.js +++ b/packages/agoric-cli/test/bundles-regExp.test.js @@ -1,6 +1,7 @@ import test from 'ava'; import { PACKAGE_NAME_RE } from '../src/lib/bundles.js'; +/** @type {Array<[name: string, spec?: string]>} */ const goodPatterns = [ ['@agoric/assert-v0.6.0'], ['@agoric/base-zone-v0.1.0/', '@agoric/base-zone-v0.1.0'], diff --git a/packages/agoric-cli/test/main.test.js b/packages/agoric-cli/test/main.test.js index d2b17a170a5..a6ec076e8e7 100644 --- a/packages/agoric-cli/test/main.test.js +++ b/packages/agoric-cli/test/main.test.js @@ -19,6 +19,7 @@ test('sanity', async t => { const myMain = args => { const oldConsole = console; try { + // @ts-expect-error globalThis.console = stubAnylogger(); return main('foo', args, { anylogger: stubAnylogger, diff --git a/packages/agoric-cli/test/upgrade-contract/init-proposal.js b/packages/agoric-cli/test/upgrade-contract/init-proposal.js index ff3238455ea..9ab07c75b62 100644 --- a/packages/agoric-cli/test/upgrade-contract/init-proposal.js +++ b/packages/agoric-cli/test/upgrade-contract/init-proposal.js @@ -3,7 +3,7 @@ import { E } from '@endo/far'; /** * Initialize contractRef the first time. * - * @param {BootstrapSpace} param0 + * @param {{ [K in keyof BootstrapSpace]: object }} promiseSpace */ export const initContract = async ({ consume: { zoe, myStatus },