Minor typing and other cleanups extracted from
MODIFY
src/helpers/copyArray.js
src/helpers/copyArray.js
import '../types.js';
import './internal-types.js';
-/**
- * TODO Why do I need these?
- *
- * @typedef {import('./internal-types.js').PassStyleHelper} PassStyleHelper
- */
import '@agoric/assert/exported.js';
import { assertChecker, checkNormalProperty } from './passStyleHelpers.js';

MODIFY
src/helpers/copyRecord.js
src/helpers/copyRecord.js
@@ -11,11 +11,6 @@ import {
import '../types.js';
import './internal-types.js';
-/**
- * TODO Why do I need these?
- *
- * @typedef {import('./internal-types.js').PassStyleHelper} PassStyleHelper
- */
import '@agoric/assert/exported.js';
const { details: X } = assert;

MODIFY
src/helpers/error.js
src/helpers/error.js
import '../types.js';
import './internal-types.js';
-/**
- * TODO Why do I need these?
- *
- * @typedef {import('./internal-types.js').PassStyleHelper} PassStyleHelper
- */
import '@agoric/assert/exported.js';
import { assertChecker } from './passStyleHelpers.js';

MODIFY
src/helpers/internal-types.js
src/helpers/internal-types.js
// eslint-disable-next-line spaced-comment
/// <reference path="../extra-types.d.ts" />
-import '../types.js';
-
-/**
- * @callback Checker
- * @param {boolean} cond
- * @param {Details=} details
- * @returns {boolean}
- */
-
/**
* The PassStyleHelper are only used to make a `passStyleOf` function.
* Thus, it should not depend on an ambient one. Rather, each helper should be

MODIFY
src/helpers/passStyleHelpers.js
src/helpers/passStyleHelpers.js
import '../types.js';
import './internal-types.js';
-/**
- * TODO Why do I need these?
- *
- * @typedef {import('./internal-types.js').Checker} Checker
- */
import '@agoric/assert/exported.js';
const { details: X, quote: q } = assert;
@@ -106,6 +101,9 @@ export const checkNormalProperty = (
};
harden(checkNormalProperty);
+export const getTag = tagRecord => tagRecord[Symbol.toStringTag];
+harden(getTag);
+
/**
* @param {{ [PASS_STYLE]: string }} tagRecord
* @param {PassStyle} passStyle
@@ -138,7 +136,7 @@ export const checkTagRecord = (tagRecord, passStyle, check = x => x) => {
check,
) &&
check(
- typeof tagRecord[Symbol.toStringTag] === 'string',
+ typeof getTag(tagRecord) === 'string',
X`A [Symbol.toString]-named property must be a string: ${tagRecord}`,
)
);

MODIFY
src/helpers/remotable.js
src/helpers/remotable.js
import '../types.js';
import './internal-types.js';
-/**
- * TODO Why do I need these?
- *
- * @typedef {import('./internal-types.js').PassStyleHelper} PassStyleHelper
- * @typedef {import('./internal-types.js').Checker} Checker
- */
import '@agoric/assert/exported.js';
import {
assertChecker,
@@ -19,6 +13,7 @@ import {
PASS_STYLE,
checkTagRecord,
isObject,
+ getTag,
} from './passStyleHelpers.js';
import { getEnvironmentOption } from './environment-options.js';
@@ -49,7 +44,7 @@ export const ALLOW_IMPLICIT_REMOTABLES =
/**
* @param {InterfaceSpec} iface
- * @param {Checker} check
+ * @param {Checker=} check
*/
const checkIface = (iface, check = x => x) => {
return (
@@ -132,20 +127,16 @@ const checkRemotableProtoOf = (original, check = x => x) => {
}
const {
- // @ts-ignore https://github.com/microsoft/TypeScript/issues/1863
- [PASS_STYLE]: _passStyleDesc,
- // @ts-ignore https://github.com/microsoft/TypeScript/issues/1863
- [Symbol.toStringTag]: ifaceDesc,
- ...restDescs
- } = getOwnPropertyDescriptors(proto);
+ [PASS_STYLE]: _passStyle,
+ [Symbol.toStringTag]: iface,
+ ...rest
+ } = proto;
return (
check(
- ownKeys(restDescs).length === 0,
- X`Unexpected properties on Remotable Proto ${ownKeys(restDescs)}`,
- ) &&
- // @ts-ignore red highlights in vscode but `yarn test` clean.
- checkIface(ifaceDesc && ifaceDesc.value, check)
+ ownKeys(rest).length === 0,
+ X`Unexpected properties on Remotable Proto ${ownKeys(rest)}`,
+ ) && checkIface(iface, check)
);
};
@@ -188,7 +179,7 @@ export const getInterfaceOf = val => {
) {
return undefined;
}
- return val[Symbol.toStringTag];
+ return getTag(val);
};
harden(getInterfaceOf);

MODIFY
src/marshal.js
src/marshal.js
@@ -317,7 +317,7 @@ export function makeMarshal(
X`invalid qclass typeof ${q(typeof qclass)}`,
);
assert(!isArray(rawTree));
- // Switching on `encoded[QCLASS]` (or anything less direct, like
+ // Switching on `rawTree[QCLASS]` (or anything less direct, like
// `qclass`) does not discriminate rawTree in typescript@4.2.3 and
// earlier.
switch (rawTree['@qclass']) {
@@ -403,13 +403,6 @@ export function makeMarshal(
default: {
assert(
- // @ts-ignore Should be at-ts-expect-error, but see
- // https://github.com/Agoric/agoric-sdk/issues/3840
- //
- // This value indeed violates the current types.
- // We test for it to give a more informative diagnostic if we
- // receive it from a counterparty using an older version of the
- // protocol.
qclass !== 'ibid',
X`The protocol no longer supports ibid encoding: ${rawTree}.`,
);

MODIFY
src/passStyleOf.js
src/passStyleOf.js
@@ -18,13 +18,8 @@ const { details: X, quote: q } = assert;
const { ownKeys } = Reflect;
const { isFrozen } = Object;
-// TODO Why do I need to import the type this time? I still don't have a model.
/**
- * @typedef {import('./helpers/internal-types.js').PassStyleHelper} PassStyleHelper
- */
-
-/**
- * @param {PassStyleHelper[]} passStyleHelper The passStyleHelper to register,
+ * @param {PassStyleHelper[]} passStyleHelpers The passStyleHelpers to register,
* in priority order.
* NOTE These must all be "trusted",
* complete, and non-colliding. `makePassStyleOf` may *assume* that each helper
@@ -33,7 +28,7 @@ const { isFrozen } = Object;
* accidents.
* @returns {{passStyleOf: PassStyleOf, HelperTable: any}}
*/
-const makePassStyleOfKit = passStyleHelper => {
+const makePassStyleOfKit = passStyleHelpers => {
const HelperTable = {
__proto__: null,
copyArray: undefined,
@@ -41,16 +36,20 @@ const makePassStyleOfKit = passStyleHelper => {
error: undefined,
remotable: undefined,
};
- for (const helper of passStyleHelper) {
+ for (const helper of passStyleHelpers) {
const { styleName } = helper;
- assert(styleName in HelperTable);
- assert.equal(HelperTable[styleName], undefined);
+ assert(styleName in HelperTable, X`Unrecognized helper: ${q(styleName)}`);
+ assert.equal(
+ HelperTable[styleName],
+ undefined,
+ X`conflicting helpers for ${q(styleName)}`,
+ );
HelperTable[styleName] = helper;
}
for (const styleName of ownKeys(HelperTable)) {
assert(
HelperTable[styleName] !== undefined,
- X`missing helper for ${styleName}`,
+ X`missing helper for ${q(styleName)}`,
);
}
harden(HelperTable);
@@ -69,7 +68,7 @@ const makePassStyleOfKit = passStyleHelper => {
*
* @type {WeakMap<Passable, PassStyle>}
*/
- const passStyleOfCache = new WeakMap();
+ const passStyleMemo = new WeakMap();
/**
* @type {PassStyleOf}
@@ -86,9 +85,9 @@ const makePassStyleOfKit = passStyleHelper => {
const passStyleOfRecur = inner => {
const innerIsObject = isObject(inner);
if (innerIsObject) {
- if (passStyleOfCache.has(inner)) {
+ if (passStyleMemo.has(inner)) {
// @ts-ignore TypeScript doesn't know that `get` after `has` is safe
- return passStyleOfCache.get(inner);
+ return passStyleMemo.get(inner);
}
assert(
!inProgress.has(inner),
@@ -99,7 +98,7 @@ const makePassStyleOfKit = passStyleHelper => {
// eslint-disable-next-line no-use-before-define
const passStyle = passStyleOfInternal(inner);
if (innerIsObject) {
- passStyleOfCache.set(inner, passStyle);
+ passStyleMemo.set(inner, passStyle);
inProgress.delete(inner);
}
return passStyle;
@@ -145,7 +144,7 @@ const makePassStyleOfKit = passStyleHelper => {
helper.assertValid(inner, passStyleOfRecur);
return /** @type {PassStyle} */ (passStyleTag);
}
- for (const helper of passStyleHelper) {
+ for (const helper of passStyleHelpers) {
if (helper.canBeValid(inner)) {
helper.assertValid(inner, passStyleOfRecur);
return helper.styleName;
@@ -183,7 +182,6 @@ const { passStyleOf, HelperTable } = makePassStyleOfKit([
ErrorHelper,
RemotableHelper,
]);
-
export { passStyleOf };
export const everyPassableChild = (passable, fn) => {

MODIFY
src/types.js
src/types.js
* EncodingClass<'-Infinity'> |
* EncodingClass<'bigint'> & { digits: string } |
* EncodingClass<'@@asyncIterator'> |
- * EncodingClass<'error'> & { name: string, message: string, errorId?: string } |
+ * EncodingClass<'error'> & { name: string,
+ * message: string,
+ * errorId?: string
+ * } |
* EncodingClass<'slot'> & { index: number, iface?: InterfaceSpec } |
- * EncodingClass<'hilbert'> & { original: Encoding, rest?: Encoding }} EncodingUnion
- * @typedef {{ [index: string]: Encoding, '@qclass'?: undefined }} EncodingRecord
+ * EncodingClass<'hilbert'> & { original: Encoding,
+ * rest?: Encoding }
+ * } EncodingUnion
+ *
+ * @typedef {{ [index: string]: Encoding,
+ * '@qclass'?: undefined }
+ * } EncodingRecord
* We exclude '@qclass' as a property in encoding records.
- * @typedef {EncodingUnion | null | string | boolean | number | EncodingRecord} EncodingElement
+ *
+ * @typedef {EncodingUnion | null | string |
+ * boolean | number | EncodingRecord
+ * } EncodingElement
*/
/**
* @returns {InterfaceSpec|undefined} the interface specification, or undefined
* if not a deemed to be a Remotable
*/
+
+/**
+ * @callback Checker
+ * Internal to a useful pattern for writing checking logic
+ * (a "checkFoo" function) that can be used to implement a predicate
+ * (an "isFoo" function) or a validator (an "assertFoo" function).
+ *
+ * * A predicate ideally only returns `true` or `false` and rarely throws.
+ * * A validator throws an informative diagnostic when the predicate
+ * would have returned `false`, and simply returns `undefined` normally
+ * when the predicate would have returned `true`.
+ * * The internal checking function that they share is parameterized by a
+ * `Checker` that determines how to proceed with a failure condition.
+ * Predicates pass in an identity function as checker. Validators
+ * pass in `assertChecker` which is a trivial wrapper around `assert`.
+ *
+ * See the various uses for good examples.
+ *
+ * @param {boolean} cond
+ * @param {Details=} details
+ * @returns {boolean}
+ */

MODIFY
test/prepare-test-env-ava.js
test/prepare-test-env-ava.js
+import '@agoric/eventual-send/shim.js';
import 'ses';
import './lockdown.js';

MODIFY
test/test-marshal-far-function.js
test/test-marshal-far-function.js
import { test } from './prepare-test-env-ava.js';
-import { Far } from '../src/make-far.js';
-import { passStyleOf } from '../src/passStyleOf.js';
import { getInterfaceOf } from '../src/helpers/remotable.js';
+import { passStyleOf } from '../src/passStyleOf.js';
+import { Far } from '../src/make-far.js';
const { freeze, setPrototypeOf } = Object;

Minor typing and other cleanups extracted from #3795 (#3851)