import { AST_NODE_TYPES, ASTUtils, type TSESLint, type TSESTree, } from '@typescript-eslint/utils'; import { bindValueImports, buildCalleeResolver, getImportDeclarations, hasForeignBinding, parenthesesDepth, type ValueImportBinding, } from 'eslint-rule-forge'; import { ARRAY_FN, BOUNDED_LENGTH_TUPLE_FN, FIXED_LENGTH_TUPLE_FN, MAX_LENGTH_TUPLE_FN, MIN_LENGTH_ARRAY_FN, MIN_LENGTH_TUPLE_FN, NON_EMPTY_ARRAY_FN, STRUCTURAL_PREFIX_CAP, TS_FORTRESS_MODULE, } from './constants.mjs'; type Options = readonly []; type MessageIds = 'useCanonicalType'; /** How one leading length argument of a combinator call must look. */ type BoundMatcher = number | 'anyLength' | 'sameAsFirst'; type Rewrite = Readonly<{ /** The canonical ts-fortress combinator the call resolves to. */ from: string; /** One matcher per leading length argument the call must have. */ bounds: readonly BoundMatcher[]; /** The combinator the call is rewritten to. */ to: string; /** Indices into `bounds` of the length arguments the rewrite keeps, in order. */ keep: readonly number[]; /** * Whether an options object carrying `defaultValue` blocks the rewrite, * because the two combinators type that option differently. */ blockedByDefaultValue: boolean; }>; /** * Combinator calls whose length arguments make them a longhand spelling of * another combinator. * * Every entry is **type-identical**, not merely assignable — the two calls * produce the very same `Type`, verified against the ts-type-forge * definitions: * * | call | canonical form | why | * | :---------------------------- | :------------------------ | :-- | * | `minLengthArray(1, x)` | `nonEmptyArray(x)` | `NonEmptyArray` is defined as `MinLengthArray<1, A>` | * | `minLengthTuple(0, x)` | `array(x)` | `MinLengthTuple<0, A>` is `readonly A[]` — no constraint at all | * | `maxLengthTuple(0, x)` | `fixedLengthTuple(0, x)` | both are `readonly []` | * | `boundedLengthTuple(n, n, x)` | `fixedLengthTuple(n, x)` | the union collapses to its single longest member | * | `boundedLengthTuple(0, n, x)` | `maxLengthTuple(n, x)` | `MaxLengthTuple` is defined as `BoundedLengthTuple<0, N, A>` | * * The rewrite also preserves `defaultValue` and every accepted/rejected value; * only the default `typeName` (and the `details.kind` of the length error it * reports) changes, to the one that names the constraint being checked. * * The `*Array` family has no analogous entries on purpose — see the README: * its length constraint lives in a brand, and `BoundedLengthArray` * is an intersection of both bounds' brands, so neither * `boundedLengthArray(0, n, x)` → `maxLengthArray(n, x)` (which drops the * `MinLengthArray<0>` brand) nor `boundedLengthArray(n, n, x)` → * `fixedLengthArray(n, x)` (which adds an exact tuple for `n <= 10`) keeps the * type unchanged. */ const REWRITES = [ { from: MIN_LENGTH_ARRAY_FN, bounds: [1], to: NON_EMPTY_ARRAY_FN, keep: [], blockedByDefaultValue: true, }, { from: MIN_LENGTH_TUPLE_FN, bounds: [0], to: ARRAY_FN, keep: [], blockedByDefaultValue: false, }, { from: MAX_LENGTH_TUPLE_FN, bounds: [0], to: FIXED_LENGTH_TUPLE_FN, keep: [0], blockedByDefaultValue: false, }, // Listed before the `0, n` entry so that `boundedLengthTuple(0, 0, x)` // normalizes to `fixedLengthTuple(0, x)`, the more specific of the two. { from: BOUNDED_LENGTH_TUPLE_FN, bounds: ['anyLength', 'sameAsFirst'], to: FIXED_LENGTH_TUPLE_FN, keep: [0], blockedByDefaultValue: false, }, { from: BOUNDED_LENGTH_TUPLE_FN, bounds: [0, 'anyLength'], to: MAX_LENGTH_TUPLE_FN, keep: [1], blockedByDefaultValue: false, }, ] as const satisfies readonly Rewrite[]; export const preferCanonicalLengthConstrainedType: TSESLint.RuleModule< MessageIds, Options > = { meta: { type: 'suggestion', docs: { description: 'Normalize a length-constrained ts-fortress array combinator whose bounds are degenerate (e.g. `minLengthArray(1, x)`, `boundedLengthTuple(n, n, x)`) to the combinator that names that constraint directly.', }, fixable: 'code', schema: [], messages: { useCanonicalType: 'Use `{{replacement}}(...)` instead of `{{original}}({{boundsText}}, ...)`: with these bounds both build exactly the same type, and `{{replacement}}` names the constraint directly (and reports it on validation failure).', }, defaultOptions: [], }, create: (context) => { const sourceCode = context.sourceCode; const tsFortressImports = getImportDeclarations( sourceCode.ast, TS_FORTRESS_MODULE, ); const resolveCallee = buildCalleeResolver(tsFortressImports); const mut_matches: { node: TSESTree.CallExpression; propertyNode: TSESTree.Node; isNamespace: boolean; rewrite: Rewrite; }[] = []; return { CallExpression: (node) => { const resolved = resolveCallee(node.callee); if (resolved === undefined) { return; } const rewrite = REWRITES.find( (candidate) => candidate.from === resolved.canonicalName && matchesRewrite(node, candidate), ); if (rewrite === undefined) { return; } mut_matches.push({ node, propertyNode: resolved.propertyNode, isNamespace: resolved.isNamespace, rewrite, }); }, 'Program:exit': () => { for (const { node, propertyNode, isNamespace, rewrite, } of mut_matches) { // A namespace call (`t.maxLengthTuple(...)`) only swaps the property // name, so it never needs a binding of its own. const binding = isNamespace ? ({ localNames: [rewrite.to], edits: () => [], } satisfies ValueImportBinding) : bindValueImports(sourceCode, TS_FORTRESS_MODULE, [rewrite.to]); const localName = binding?.localNames[0] ?? rewrite.to; // Another binding takes the name where the call is: the rewritten // call would call that instead, so nothing is reported. if ( !isNamespace && hasForeignBinding(sourceCode, node, localName, TS_FORTRESS_MODULE) ) { continue; } context.report({ node, messageId: 'useCanonicalType', data: { original: rewrite.from, replacement: localName, boundsText: boundsText(node, rewrite, sourceCode), }, // Every fix brings the name into scope itself, so that one applied // alone (an editor's quick fix) is complete. An import that cannot // bind it as a value (`import type`) leaves no fix. fix: binding === undefined ? null : (fixer) => [ ...binding.edits(fixer), fixer.replaceText(propertyNode, localName), ...dropBoundsFix(fixer, node, rewrite, sourceCode), ], }); } }, }; }, } as const; /* eslint-disable @typescript-eslint/prefer-readonly-parameter-types */ const isConstAssertion = (node: TSESTree.TSAsExpression): boolean => node.typeAnnotation.type === AST_NODE_TYPES.TSTypeReference && node.typeAnnotation.typeName.type === AST_NODE_TYPES.Identifier && node.typeAnnotation.typeName.name === 'const'; /** `node` without the `satisfies` and `as const` around it. */ const skipLiteralPreservingWrappers = (node: TSESTree.Node): TSESTree.Node => node.type === AST_NODE_TYPES.TSSatisfiesExpression || (node.type === AST_NODE_TYPES.TSAsExpression && isConstAssertion(node)) ? skipLiteralPreservingWrappers(node.expression) : node; /** * A non-negative integer literal's value, or `undefined` for anything else — a * computed bound cannot be matched against the rewrite table. * * Read through `satisfies` and `as const`, which leave the literal's type as it * is, but not through any other `as`: `1 as number` widens the bound the * combinator infers, so the call no longer builds the type the table names. */ const getLengthLiteral = (node: TSESTree.Node): number | undefined => { const literal = skipLiteralPreservingWrappers(node); return literal.type === AST_NODE_TYPES.Literal && typeof literal.value === 'number' && Number.isSafeInteger(literal.value) && literal.value >= 0 ? literal.value : undefined; }; /** * Whether the options argument may carry a `defaultValue`. Anything that is not * a plain object literal of statically known keys counts, since its contents * cannot be ruled out. */ const passesDefaultValue = (node: TSESTree.Node | undefined): boolean => node !== undefined && (node.type !== AST_NODE_TYPES.ObjectExpression || node.properties.some( (property) => property.type !== AST_NODE_TYPES.Property || property.computed || property.key.type !== AST_NODE_TYPES.Identifier || property.key.name === 'defaultValue', )); /** * Whether `node` is a `(...bounds, elementType, options?)` call * whose bounds match `rewrite` exactly as written. */ const matchesRewrite = ( node: TSESTree.CallExpression, rewrite: Rewrite, ): boolean => { const args = node.arguments; const boundCount = rewrite.bounds.length; if ( args.length < boundCount + 1 || boundCount + 2 < args.length || // Explicit type arguments pin the bound independently of the literal, so // dropping or moving the literal could change the resulting type. node.typeArguments !== undefined || args.some((arg) => arg.type === AST_NODE_TYPES.SpreadElement) ) { return false; } const bounds = args.slice(0, boundCount).map(getLengthLiteral); const [first] = bounds; const boundsMatch = rewrite.bounds.every((matcher, index) => { const value = bounds[index]; if (value === undefined) { return false; } switch (matcher) { case 'anyLength': return value <= STRUCTURAL_PREFIX_CAP; case 'sameAsFirst': return value === first && value <= STRUCTURAL_PREFIX_CAP; default: return value === matcher; } }); return ( boundsMatch && (!rewrite.blockedByDefaultValue || !passesDefaultValue(args[boundCount + 1])) ); }; /** The bounds of the matched call, as written, for the report message. */ const boundsText = ( node: TSESTree.CallExpression, rewrite: Rewrite, sourceCode: TSESLint.SourceCode, ): string => node.arguments .slice(0, rewrite.bounds.length) .map((arg) => sourceCode.getText(arg)) .join(', '); /** * Removes the length arguments the rewrite drops. Each removal runs from the * dropped argument's first token — the outermost of the parentheses the source * puts around it, which `node.range` leaves out — up to the token after the * comma that follows it. So the argument's own parentheses, the comma and any * whitespace and comments after it go with it, while the next argument keeps * its parentheses; the ranges never overlap. */ const dropBoundsFix = ( fixer: TSESLint.RuleFixer, node: TSESTree.CallExpression, rewrite: Rewrite, sourceCode: TSESLint.SourceCode, ): readonly TSESLint.RuleFix[] => rewrite.bounds.flatMap((_, index) => { const dropped = node.arguments[index]; if (dropped === undefined || rewrite.keep.includes(index)) { return []; } const depth = parenthesesDepth(sourceCode, dropped); const first = sourceCode.getFirstToken(dropped); const start = first === null || depth === 0 ? first : sourceCode.getTokenBefore(first, { skip: depth - 1 }); // The first comma after the argument, not the token after it, which for // `(1)` is the closing parenthesis. const comma = sourceCode.getTokenAfter(dropped, ASTUtils.isCommaToken); const end = comma === null ? null : sourceCode.getTokenAfter(comma); return start === null || end === null ? [] : [fixer.removeRange([start.range[0], end.range[0]])]; });