Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(patterns): improve pattern types. Flatten @imports #2256

Merged
merged 3 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions packages/patterns/src/keys/checkKey.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ import { checkBagEntries, makeBagOfEntries } from './copyBag.js';

const { ownKeys } = Reflect;

/** @import {Checker} from '@endo/marshal' */
/** @import {Passable} from '@endo/pass-style' */
/** @import {CopyBag, CopyMap, CopySet, Key} from '../types' */
/**
* @import {Passable} from '@endo/pass-style'
* @import {Checker} from '@endo/marshal'
* @import {CopyBag, CopyMap, CopySet, Key} from '../types.js'
*/

// ////////////////// Primitive and Scalar keys ////////////////////////////////

Expand Down
4 changes: 2 additions & 2 deletions packages/patterns/src/keys/compareKeys.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
} from './checkKey.js';
import { makeCompareCollection } from './keycollection-operators.js';

/** @import {CopySet} from '../types' */
/** @import {CopySet, KeyCompare} from '../types.js' */

/**
* CopySet X is smaller than CopySet Y iff all of these conditions hold:
Expand Down Expand Up @@ -86,7 +86,7 @@ const _mapCompare = makeCompareCollection(
);
harden(_mapCompare);

/** @type {import('../types').KeyCompare} */
/** @type {KeyCompare} */
export const compareKeys = (left, right) => {
assertKey(left);
assertKey(right);
Expand Down
9 changes: 5 additions & 4 deletions packages/patterns/src/keys/copyBag.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import {

import { X } from '@endo/errors';

/** @import {CopyBag} from '../types' */
/** @import {FullCompare} from '../types' */
/** @import {Checker} from '@endo/marshal' */
/** @import {Passable} from '@endo/pass-style' */
/**
* @import {Passable} from '@endo/pass-style'
* @import {Checker} from '@endo/marshal'
* @import {CopyBag, FullCompare} from '../types.js'
*/

/**
* @template T
Expand Down
4 changes: 2 additions & 2 deletions packages/patterns/src/keys/copySet.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import {
import { X } from '@endo/errors';

/**
* @import {CopySet, FullCompare} from '../types'
* @import {Checker} from '@endo/marshal'
* @import {Passable} from '@endo/pass-style'
* @import {Checker} from '@endo/marshal'
* @import {CopySet, FullCompare} from '../types.js'
*/

/**
Expand Down
21 changes: 10 additions & 11 deletions packages/patterns/src/keys/keycollection-operators.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ import {
import { makeIterator } from '@endo/common/make-iterator.js';
import { makeArrayIterator } from '@endo/common/make-array-iterator.js';

/** @import {RankCompare} from '@endo/marshal' */
/** @import {KeyComparison} from '../types' */
/** @import {KeyCompare} from '../types' */
/** @import {FullCompare} from '../types' */
/** @import {KeyCollection} from '../types' */
/**
* @import {RankCompare} from '@endo/marshal'
* @import {Key, KeyCompare, FullCompare, KeyComparison, KeyCollection} from '../types.js'
*/

import { q, Fail } from '@endo/errors';

Expand All @@ -22,10 +21,10 @@ import { q, Fail } from '@endo/errors';
* rank, into an iterable that resolves those ties using `fullCompare`.
*
* @template [V=unknown]
* @param {Array<[import('../types.js').Key, V]>} entries
* @param {Array<[Key, V]>} entries
* @param {RankCompare} rankCompare
* @param {FullCompare} fullCompare
* @returns {IterableIterator<[import('../types.js').Key, V]>}
* @returns {IterableIterator<[Key, V]>}
*/
const generateFullSortedEntries = (entries, rankCompare, fullCompare) => {
assertRankSorted(entries, rankCompare);
Expand Down Expand Up @@ -82,9 +81,9 @@ harden(generateFullSortedEntries);
* @template [V=unknown]
* @param {C} c1
* @param {C} c2
* @param {(collection: C) => Array<[import('../types.js').Key, V]>} getEntries
* @param {(collection: C) => Array<[Key, V]>} getEntries
* @param {any} absentValue
* @returns {IterableIterator<[import('../types.js').Key, V | absentValue, V | absentValue]>}
* @returns {IterableIterator<[Key, V | absentValue, V | absentValue]>}
*/
export const generateCollectionPairEntries = (
c1,
Expand Down Expand Up @@ -127,7 +126,7 @@ export const generateCollectionPairEntries = (
nextY();
return makeIterator(() => {
let done = false;
/** @type {[import('../types.js').Key, V | absentValue, V | absentValue]} */
/** @type {[Key, V | absentValue, V | absentValue]} */
let value;
if (xDone && yDone) {
done = true;
Expand Down Expand Up @@ -178,7 +177,7 @@ harden(generateCollectionPairEntries);
*
* @template [C=KeyCollection]
* @template [V=unknown]
* @param {(collection: C) => Array<[import('../types.js').Key, V]>} getEntries
* @param {(collection: C) => Array<[Key, V]>} getEntries
* @param {any} absentValue
* @param {KeyCompare} compareValues
* @returns {(left: C, right: C) => KeyComparison}
Expand Down
7 changes: 4 additions & 3 deletions packages/patterns/src/keys/merge-bag-operators.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import {
import { q, Fail } from '@endo/errors';
import { assertNoDuplicateKeys, makeBagOfEntries } from './copyBag.js';

/** @import {KeyComparison} from '../types' */
/** @import {FullCompare} from '../types' */
/** @import {RankCompare} from '@endo/marshal' */
/**
* @import {RankCompare} from '@endo/marshal'
* @import {FullCompare} from '../types.js'
*/

// Based on merge-set-operators.js, but altered for the bag representation.
// TODO share more code with that file and keycollection-operators.js.
Expand Down
7 changes: 4 additions & 3 deletions packages/patterns/src/keys/merge-set-operators.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import {
import { q, Fail } from '@endo/errors';
import { assertNoDuplicates, makeSetOfElements } from './copySet.js';

/** @import {KeyComparison} from '../types' */
/** @import {FullCompare} from '../types' */
/** @import {RankCompare} from '@endo/marshal' */
/**
* @import {RankCompare} from '@endo/marshal'
* @import {FullCompare, KeyComparison} from '../types.js'
*/

// TODO share more code with keycollection-operators.js.

Expand Down
4 changes: 3 additions & 1 deletion packages/patterns/src/patterns/getGuardPayloads.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import {
} from './patternMatchers.js';
import { getCopyMapKeys, makeCopyMap } from '../keys/checkKey.js';

/** @import {AwaitArgGuard, AwaitArgGuardPayload, InterfaceGuard, InterfaceGuardPayload, MethodGuard, MethodGuardPayload} from '../types.js' */
/**
* @import {AwaitArgGuard, AwaitArgGuardPayload, InterfaceGuard, InterfaceGuardPayload, MethodGuard, MethodGuardPayload} from '../types.js'
*/

// The get*GuardPayload functions exist to adapt to the worlds both
// before and after https://github.com/endojs/endo/pull/1712 . When
Expand Down
32 changes: 15 additions & 17 deletions packages/patterns/src/patterns/patternMatchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ import {
} from '../keys/checkKey.js';
import { generateCollectionPairEntries } from '../keys/keycollection-operators.js';

/// <reference types="ses"/>

/** @import {Checker, CopyRecord, CopyTagged, Passable} from '@endo/pass-style' */
/** @import {ArgGuard, AwaitArgGuard, CheckPattern, GetRankCover, InterfaceGuard, MatcherNamespace, MethodGuard, MethodGuardMaker, Pattern, RawGuard, SyncValueGuard} from '../types.js' */
/** @import {Kind, MatchHelper} from './types.js' */
/**
* @import {Checker, CopyRecord, CopyTagged, Passable} from '@endo/pass-style'
* @import {ArgGuard, AwaitArgGuard, CheckPattern, GetRankCover, InterfaceGuard, MatcherNamespace, MethodGuard, MethodGuardMaker, Pattern, RawGuard, SyncValueGuard, Kind, Limits, AllLimits, Key, DefaultGuardType} from '../types.js'
* @import {MatchHelper, PatternKit} from './types.js'
*/

const { entries, values } = Object;
const { ownKeys } = Reflect;

/** @type {WeakSet<import('../types.js').Pattern>} */
/** @type {WeakSet<Pattern>} */
const patternMemo = new WeakSet();

// /////////////////////// Match Helpers Helpers /////////////////////////////
Expand Down Expand Up @@ -78,13 +78,11 @@ export const defaultLimits = harden({
* Thus, the result only needs to support destructuring. The current
* implementation uses inheritance as a cheap hack.
*
* @param {import('../types.js').Limits} [limits]
* @returns {import('../types.js').AllLimits}
* @param {Limits} [limits]
* @returns {AllLimits}
*/
const limit = (limits = {}) =>
/** @type {import('../types.js').AllLimits} */ (
harden({ __proto__: defaultLimits, ...limits })
);
/** @type {AllLimits} */ (harden({ __proto__: defaultLimits, ...limits }));

const checkIsWellFormedWithLimit = (
payload,
Expand Down Expand Up @@ -149,15 +147,15 @@ const checkDecimalDigitsLimit = (specimen, decimalDigitsLimit, check) => {
};

/**
* @returns {import('./types.js').PatternKit}
* @returns {PatternKit}
*/
const makePatternKit = () => {
/**
* If this is a recognized match tag, return the MatchHelper.
* Otherwise result undefined.
*
* @param {string} tag
* @returns {import('./types.js').MatchHelper | undefined}
* @returns {MatchHelper | undefined}
*/
const maybeMatchHelper = tag =>
// eslint-disable-next-line no-use-before-define
Expand Down Expand Up @@ -301,7 +299,7 @@ const makePatternKit = () => {

/**
* @param {Passable} specimen
* @param {import('../types.js').Key} keyAsPattern
* @param {Key} keyAsPattern
* @param {Checker} check
* @returns {boolean}
*/
Expand Down Expand Up @@ -1697,7 +1695,7 @@ const makePatternKit = () => {
split: (base, rest = undefined) => {
if (passStyleOf(harden(base)) === 'copyArray') {
// TODO at-ts-expect-error works locally but not from @endo/exo
// @ts-ignore We know it should be an array
// @ts-expect-error We know it should be an array
return M.splitArray(base, rest && [], rest);
} else {
return M.splitRecord(base, rest && {}, rest);
Expand All @@ -1706,7 +1704,7 @@ const makePatternKit = () => {
partial: (base, rest = undefined) => {
if (passStyleOf(harden(base)) === 'copyArray') {
// TODO at-ts-expect-error works locally but not from @endo/exo
// @ts-ignore We know it should be an array
// @ts-expect-error We know it should be an array
return M.splitArray([], base, rest);
} else {
return M.splitRecord({}, base, rest);
Expand Down Expand Up @@ -1933,7 +1931,7 @@ harden(assertInterfaceGuard);
* @template {Record<PropertyKey, MethodGuard>} [M = Record<PropertyKey, MethodGuard>]
* @param {string} interfaceName
* @param {M} methodGuards
* @param {{ sloppy?: boolean, defaultGuards?: import('../types.js').DefaultGuardType }} [options]
* @param {{ sloppy?: boolean, defaultGuards?: DefaultGuardType }} [options]
* @returns {InterfaceGuard<M>}
*/
const makeInterfaceGuard = (interfaceName, methodGuards, options = {}) => {
Expand Down
22 changes: 3 additions & 19 deletions packages/patterns/src/patterns/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,9 @@

export {};

/** @import {Passable} from '@endo/pass-style' */
/** @import {PassStyle} from '@endo/pass-style' */
/** @import {Checker} from '@endo/pass-style' */
/** @import {MethodGuard} from '../types.js' */

/** @import {MatcherNamespace} from '../types' */
/** @import {Pattern} from '../types' */
/** @import {GetRankCover} from '../types' */

/**
* @typedef {Exclude<PassStyle, 'tagged'> |
* 'copySet' | 'copyBag' | 'copyMap' |
* `match:${any}` | `guard:${any}`
* } Kind
* It is either a PassStyle other than 'tagged', or, if the underlying
* PassStyle is 'tagged', then the `getTag` value for tags that are
* recognized at the @endo/patterns level of abstraction. For each of those
* tags, a tagged record only has that kind if it satisfies the invariants
* that the @endo/patterns level associates with that kind.
* @import {Passable, Checker} from '@endo/pass-style'
* @import {MatcherNamespace, Pattern, GetRankCover, Kind} from '../types.js'
*/

/**
Expand All @@ -42,7 +26,7 @@ export {};
* Assuming validity of `matcherPayload` as the payload of a Matcher corresponding
* with this MatchHelper, reports whether `specimen` is matched by that Matcher.
*
* @property {import('../types').GetRankCover} getRankCover
* @property {GetRankCover} getRankCover
* Assumes this is the payload of a CopyTagged with the corresponding
* matchTag. Return a RankCover to bound from below and above,
* in rank order, all possible Passables that would match this Matcher.
Expand Down
19 changes: 16 additions & 3 deletions packages/patterns/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

export {};

/** @import {Checker, CopyArray, CopyRecord, CopyTagged, Passable} from '@endo/pass-style' */
/** @import {PassStyle} from '@endo/pass-style' */
/** @import {RankCompare, RankCover} from '@endo/marshal' */
/**
* @import {CopyArray, CopyRecord, CopyTagged, Passable, PassStyle, Checker} from '@endo/pass-style'
* @import {RankCompare, RankCover} from '@endo/marshal'
*/

/**
* @typedef {Passable} Key
Expand Down Expand Up @@ -218,6 +219,18 @@ export {};
* @typedef {Partial<AllLimits>} Limits
*/

/**
* @typedef {Exclude<PassStyle, 'tagged'> |
* 'copySet' | 'copyBag' | 'copyMap' |
* `match:${any}` | `guard:${any}`
* } Kind
* It is either a PassStyle other than 'tagged', or, if the underlying
* PassStyle is 'tagged', then the `getTag` value for tags that are
* recognized at the @endo/patterns level of abstraction. For each of those
* tags, a tagged record only has that kind if it satisfies the invariants
* that the @endo/patterns level associates with that kind.
*/

/**
* @typedef {object} PatternMatchers
*
Expand Down
8 changes: 5 additions & 3 deletions packages/patterns/test/test-pattern-limits.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import {
defaultLimits,
} from '../src/patterns/patternMatchers.js';

/** @import {Passable} from '@endo/marshal' */
/** @import {Pattern} from '../src/types.js' */
/**
* @import {Passable} from '@endo/marshal'
* @import {Pattern} from '../src/types.js'
*/

/**
* @typedef MatchTest
* @typedef {object} MatchTest
* @property {Passable} specimen
* @property {Pattern[]} yesPatterns
* @property {[Pattern, RegExp|string][]} noPatterns
Expand Down
9 changes: 1 addition & 8 deletions packages/ses/src/error/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,7 @@ import './internal-types.js';
import { makeNoteLogArgsArrayKit } from './note-log-args.js';

/**
* @import {BaseAssert,
* Assert,
* AssertionFunctions,
* AssertionUtilities,
* StringablePayload,
* DetailsToken,
* MakeAssert,
* } from '../../types.js'
* @import {BaseAssert, Assert, AssertionFunctions, AssertionUtilities, StringablePayload, DetailsToken, MakeAssert} from '../../types.js'
*/

// For our internal debugging purposes, uncomment
Expand Down
Loading