Lint rule · ADN0502

adorn/contract-spec-shape

contract() params are typed slots matching the function.

IdADN0502, the prefix of every message
recommendederror
stricterror
Fixableno
Needs type informationno: it also runs in an untyped config

What it reports

A contract() or contract.checked() spec whose params slot is null, undefined, a hole, a closure or a bare array; a required slot after an optional one; a function with more parameters than slots (unless extraArgs: 'allow'); and a TypeScript-optional or defaulted parameter on a required slot.

Why

params is the function's signature, one typed slot per parameter (DEC-115, DEC-118). Each of these shapes is refused when the spec is declared, or leaves a parameter no call can reach: a parameter with no slot is past the signature, so every call passing it is refused, and a default on a required slot can never apply.

Examples

Reported

reported by adorn/contract-spec-shape
import { contract, NumberContracts as N } from 'adorn';

export const add = contract(
    { params: [N.Finite()] },
    (x: number, y: number) => x + y,     // y has no slot
);

Accepted

accepted
import { contract, NumberContracts as N } from 'adorn';

export const add = contract(
    { params: [N.Finite(), { descriptor: N.Finite(), optional: true }] },
    (x: number, y = 0) => x + y,
);

Details

More slots than parameters is not reported: every slot is still checked, and it is how a version keeps validating a trailing input it no longer reads. A stack at one position is {descriptor: [a, b]}. For a value adorn can't type, use Contracts.defined().

To keep a finding on purpose, disable it with a reason, which justify-opt-out checks: // eslint-disable-next-line adorn/contract-spec-shape -- <why>.

Messages

The text ESLint prints, read from the rule itself. {{…}} is filled in for each finding.

Message idText
nullSlot[ADN0502] params[{{i}}] is null: every slot is typed (DEC-118). Use a descriptor, Contracts.defined() for any value, or {descriptor: Contracts.defined(), optional: true} for the old meaning.
hole[ADN0502] params[{{i}}] is a hole: every slot is typed (DEC-118).
undefinedSlot[ADN0502] params[{{i}}] is undefined: every slot is typed (DEC-118).
closureSlot[ADN0502] params[{{i}}] is a closure, not a descriptor (DEC-118). Wrap a check with defineConstraint, or use a descriptor.
bareStack[ADN0502] params[{{i}}] is a bare array; a stack is {descriptor: [a, b]} (DEC-118).
requiredAfterOptional[ADN0502] params[{{i}}] is required but follows the optional params[{{j}}]: a caller can omit only trailing arguments (DEC-118).
short[ADN0502] The function declares {{n}} parameters but params has {{m}} slots, so every call passing the others is refused (DEC-115). Give each parameter a slot, or pass extraArgs: 'allow'.
optionalMismatch[ADN0502] Parameter '{{name}}' is optional in TypeScript but params[{{i}}] is a required slot, so its default can never apply. Make the slot {descriptor, optional: true}.

All rules · the adorn/eslint guide