eslint-plugin-ts-fortress 0.4.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"plugin.d.mts","sourceRoot":"","sources":["../src/plugin.mts"],"names":[],"mappings":"AACA,OAAO,EAAyB,KAAK,YAAY,EAAE,MAAM,aAAa,CAAC;AA8BvE,eAAO,MAAM,sBAAsB;;iBAE/B,IAAI,EAAE,2BAA2B;;;;;;QAIjC,oDAAoD;iBACpD,WAAW;2BAtBP,yBAAyB;;yBAQzB,aAAa,EAAI,YAAY;;;iFAdqB,OAAO;;;;CA8BhC,CAAC"}
1
+ {"version":3,"file":"plugin.d.mts","sourceRoot":"","sources":["../src/plugin.mts"],"names":[],"mappings":"AAEA,OAAO,EAAyB,KAAK,YAAY,EAAE,MAAM,aAAa,CAAC;AA+BvE,eAAO,MAAM,sBAAsB;;iBAE/B,IAAI,EAAE,2BAA2B;;;;;;QAIjC,oDAAoD;iBACpD,WAAW;2BAtBP,yBAAyB;;yBAQzB,aAAa,EAAI,YAAY;;;iFAfqB,OAAO;;;;CA+BhC,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"plugin.mjs","sources":["../src/plugin.mts"],"sourcesContent":["import { tsFortressRules } from './rules/index.mjs';\nimport { type ESLintFlatConfig, type ESLintPlugin } from './types.mjs';\n\n/**\n * Every rule this plugin ships, at `error`.\n *\n * The `satisfies` clause below is keyed off {@link tsFortressRules}, so adding\n * a rule without listing it here fails to type-check.\n */\nconst recommendedRules = {\n 'ts-fortress/prefer-canonical-length-constrained-type': 'error',\n} as const satisfies Readonly<\n Record<`ts-fortress/${keyof typeof tsFortressRules}`, 'error'>\n>;\n\nconst recommendedConfig = {\n name: 'ts-fortress/recommended',\n plugins: {\n // Resolved lazily so that this config registers the *same* object that\n // `eslintPluginTsFortress` refers to. ESLint rejects a plugin name that\n // maps to two different objects with `Cannot redefine plugin`, which is\n // what a user combining this preset with their own\n // `plugins: { 'ts-fortress': eslintPluginTsFortress }` entry would hit if\n // the preset embedded a separate copy of the plugin.\n get 'ts-fortress'(): ESLintPlugin {\n return eslintPluginTsFortress;\n },\n },\n rules: recommendedRules,\n} as const satisfies ESLintFlatConfig;\n\nexport const eslintPluginTsFortress = {\n meta: {\n name: 'eslint-plugin-ts-fortress',\n },\n rules: tsFortressRules,\n configs: {\n /** Enables every rule of this plugin at `error`. */\n recommended: recommendedConfig,\n },\n} as const satisfies ESLintPlugin;\n"],"names":[],"mappings":";;AASA,MAAM,gBAAA,GAAmB;AAAA,EACvB,sDAAA,EAAwD;AAC1D,CAAA;AAIA,MAAM,iBAAA,GAAoB;AAAA,EACxB,IAAA,EAAM,yBAAA;AAAA,EACN,OAAA,EAAS;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAOP,IAAI,aAAA,GAA8B;AAChC,MAAA,OAAO,sBAAA;AAAA,IACT;AAAA,GACF;AAAA,EACA,KAAA,EAAO;AACT,CAAA;AAEO,MAAM,sBAAA,GAAyB;AAAA,EACpC,IAAA,EAAM;AAAA,IACJ,IAAA,EAAM;AAAA,GACR;AAAA,EACA,KAAA,EAAO,eAAA;AAAA,EACP,OAAA,EAAS;AAAA;AAAA,IAEP,WAAA,EAAa;AAAA;AAEjB;;;;"}
1
+ {"version":3,"file":"plugin.mjs","sources":["../src/plugin.mts"],"sourcesContent":["import { type ReadonlyRecord } from 'ts-type-forge';\nimport { tsFortressRules } from './rules/index.mjs';\nimport { type ESLintFlatConfig, type ESLintPlugin } from './types.mjs';\n\n/**\n * Every rule this plugin ships, at `error`.\n *\n * The `satisfies` clause below is keyed off {@link tsFortressRules}, so adding\n * a rule without listing it here fails to type-check.\n */\nconst recommendedRules = {\n 'ts-fortress/prefer-canonical-length-constrained-type': 'error',\n} as const satisfies ReadonlyRecord<\n `ts-fortress/${keyof typeof tsFortressRules}`,\n 'error'\n>;\n\nconst recommendedConfig = {\n name: 'ts-fortress/recommended',\n plugins: {\n // Resolved lazily so that this config registers the *same* object that\n // `eslintPluginTsFortress` refers to. ESLint rejects a plugin name that\n // maps to two different objects with `Cannot redefine plugin`, which is\n // what a user combining this preset with their own\n // `plugins: { 'ts-fortress': eslintPluginTsFortress }` entry would hit if\n // the preset embedded a separate copy of the plugin.\n get 'ts-fortress'(): ESLintPlugin {\n return eslintPluginTsFortress;\n },\n },\n rules: recommendedRules,\n} as const satisfies ESLintFlatConfig;\n\nexport const eslintPluginTsFortress = {\n meta: {\n name: 'eslint-plugin-ts-fortress',\n },\n rules: tsFortressRules,\n configs: {\n /** Enables every rule of this plugin at `error`. */\n recommended: recommendedConfig,\n },\n} as const satisfies ESLintPlugin;\n"],"names":[],"mappings":";;AAUA,MAAM,gBAAA,GAAmB;AAAA,EACvB,sDAAA,EAAwD;AAC1D,CAAA;AAKA,MAAM,iBAAA,GAAoB;AAAA,EACxB,IAAA,EAAM,yBAAA;AAAA,EACN,OAAA,EAAS;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAOP,IAAI,aAAA,GAA8B;AAChC,MAAA,OAAO,sBAAA;AAAA,IACT;AAAA,GACF;AAAA,EACA,KAAA,EAAO;AACT,CAAA;AAEO,MAAM,sBAAA,GAAyB;AAAA,EACpC,IAAA,EAAM;AAAA,IACJ,IAAA,EAAM;AAAA,GACR;AAAA,EACA,KAAA,EAAO,eAAA;AAAA,EACP,OAAA,EAAS;AAAA;AAAA,IAEP,WAAA,EAAa;AAAA;AAEjB;;;;"}
@@ -1,5 +1,5 @@
1
1
  import { type Linter } from 'eslint';
2
- import { type TypeEq } from 'ts-type-forge';
2
+ import { type ReadonlyRecord, type TypeEq } from 'ts-type-forge';
3
3
  import { type tsFortressRules } from './rules/index.mjs';
4
4
  /**
5
5
  * @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.
@@ -18,7 +18,7 @@ declare namespace PreferCanonicalLengthConstrainedType {
18
18
  export type EslintTsFortressRules = Readonly<{
19
19
  'ts-fortress/prefer-canonical-length-constrained-type': PreferCanonicalLengthConstrainedType.RuleEntry;
20
20
  }>;
21
- export type EslintTsFortressRulesOption = Readonly<Record<never, never>>;
21
+ export type EslintTsFortressRulesOption = ReadonlyRecord<never, never>;
22
22
  type AssertTrue<B extends true> = B;
23
23
  export type RuleNamesAreInSync = AssertTrue<TypeEq<keyof EslintTsFortressRules, `ts-fortress/${keyof typeof tsFortressRules}`>>;
24
24
  export {};
@@ -1 +1 @@
1
- {"version":3,"file":"rule-types.d.mts","sourceRoot":"","sources":["../src/rule-types.mts"],"names":[],"mappings":"AAKA,OAAO,EAAE,KAAK,MAAM,EAAE,MAAM,QAAQ,CAAC;AACrC,OAAO,EAAE,KAAK,MAAM,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,KAAK,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAEzD;;;;;;;;;;GAUG;AACH,kBAAU,oCAAoC,CAAC;IAC7C,KAAY,SAAS,GAAG,MAAM,CAAC,cAAc,CAAC;CAC/C;AAED,MAAM,MAAM,qBAAqB,GAAG,QAAQ,CAAC;IAC3C,sDAAsD,EAAE,oCAAoC,CAAC,SAAS,CAAC;CACxG,CAAC,CAAC;AAEH,MAAM,MAAM,2BAA2B,GAAG,QAAQ,CAAC,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC;AAIzE,KAAK,UAAU,CAAC,CAAC,SAAS,IAAI,IAAI,CAAC,CAAC;AAEpC,MAAM,MAAM,kBAAkB,GAAG,UAAU,CACzC,MAAM,CACJ,MAAM,qBAAqB,EAC3B,eAAe,MAAM,OAAO,eAAe,EAAE,CAC9C,CACF,CAAC"}
1
+ {"version":3,"file":"rule-types.d.mts","sourceRoot":"","sources":["../src/rule-types.mts"],"names":[],"mappings":"AAKA,OAAO,EAAE,KAAK,MAAM,EAAE,MAAM,QAAQ,CAAC;AACrC,OAAO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;;;GAQG;AACH,eAAO,MAAM,iBAAiB,eAChB,QAAQ,CAAC,UAAU,QACzB,QAAQ,CAAC,IAAI,QACb,MAAM,KACX,OAgBF,CAAC;AAEF;;;;;;;;GAQG;AACH,eAAO,MAAM,eAAe,UACnB,QAAQ,CAAC,SAAS,WAChB,QAAQ,CAAC,OAAO,SAClB,SAAS,MAAM,EAAE,KACvB,SAAS,QAAQ,CAAC,OAAO,EAUX,CAAC"}
@@ -1,4 +1,5 @@
1
1
  import { AST_NODE_TYPES } from '@typescript-eslint/utils';
2
+ import { Arr } from 'ts-data-forge';
2
3
  import { TS_FORTRESS_MODULE } from './constants.mjs';
3
4
 
4
5
  const getTsFortressImports = (program) => program.body.filter(
@@ -44,7 +45,7 @@ const hasForeignBinding = (sourceCode, node, name) => {
44
45
  }
45
46
  return false;
46
47
  };
47
- const buildImportsFix = (fixer, program, names) => names.length === 0 ? [] : [
48
+ const buildImportsFix = (fixer, program, names) => Arr.isEmpty(names) ? [] : [
48
49
  fixer.insertTextBefore(
49
50
  program,
50
51
  names.map((name) => `import { ${name} } from '${TS_FORTRESS_MODULE}';
@@ -1 +1 @@
1
- {"version":3,"file":"import-utils.mjs","sources":["../../src/rules/import-utils.mts"],"sourcesContent":["import {\n AST_NODE_TYPES,\n type TSESLint,\n type TSESTree,\n} from '@typescript-eslint/utils';\nimport { TS_FORTRESS_MODULE } from './constants.mjs';\n\n/* eslint-disable @typescript-eslint/prefer-readonly-parameter-types */\n\nexport type ResolvedCallee = Readonly<{\n /** The canonical (imported) ts-fortress name, regardless of local alias. */\n canonicalName: string;\n /** The identifier node to rewrite when replacing the callee name. */\n propertyNode: TSESTree.Node;\n /** Whether the call was made through a namespace import (`t.foo(...)`). */\n isNamespace: boolean;\n}>;\n\n/** Every `import ... from 'ts-fortress'` declaration in the file. */\nexport const getTsFortressImports = (\n program: TSESTree.Program,\n): readonly TSESTree.ImportDeclaration[] =>\n program.body.filter(\n (node): node is TSESTree.ImportDeclaration =>\n node.type === AST_NODE_TYPES.ImportDeclaration &&\n node.source.value === TS_FORTRESS_MODULE,\n );\n\n/**\n * Returns the in-scope local name a given ts-fortress export is imported as\n * (handling aliases such as `import { nonEmptyArray as nea }` → `'nea'`), or\n * `undefined` when it is not imported. Used so autofixes reference the binding\n * that actually exists rather than the canonical name.\n */\nexport const getImportedLocalName = (\n importDeclarations: readonly TSESTree.ImportDeclaration[],\n importedName: string,\n): string | undefined =>\n importDeclarations\n .flatMap((declaration) => declaration.specifiers)\n .find(\n (specifier) =>\n specifier.type === AST_NODE_TYPES.ImportSpecifier &&\n (specifier.imported.type === AST_NODE_TYPES.Identifier\n ? specifier.imported.name\n : specifier.imported.value) === importedName,\n )?.local.name;\n\n/**\n * Builds a resolver that maps a call expression's callee to the canonical\n * ts-fortress function it refers to. Handles named imports (including aliases)\n * and namespace imports (`import * as t` → `t.foo(...)`) — the latter being the\n * idiomatic way to consume ts-fortress. Returns `undefined` for any callee that\n * does not reference the given ts-fortress imports.\n */\nexport const buildCalleeResolver = (\n importDeclarations: readonly TSESTree.ImportDeclaration[],\n): ((callee: TSESTree.Expression) => ResolvedCallee | undefined) => {\n const specifiers = importDeclarations.flatMap(\n (declaration) => declaration.specifiers,\n );\n\n const namespaceNames = new Set(\n specifiers\n .filter(\n (specifier) =>\n specifier.type === AST_NODE_TYPES.ImportNamespaceSpecifier,\n )\n .map((specifier) => specifier.local.name),\n );\n\n const localToCanonical = new Map<string, string>(\n specifiers.flatMap((specifier) => {\n if (specifier.type !== AST_NODE_TYPES.ImportSpecifier) return [];\n\n const importedName =\n specifier.imported.type === AST_NODE_TYPES.Identifier\n ? specifier.imported.name\n : specifier.imported.value;\n\n return typeof importedName === 'string'\n ? [[specifier.local.name, importedName] as const]\n : [];\n }),\n );\n\n return (callee) => {\n if (callee.type === AST_NODE_TYPES.Identifier) {\n const canonicalName = localToCanonical.get(callee.name);\n\n return canonicalName === undefined\n ? undefined\n : { canonicalName, propertyNode: callee, isNamespace: false };\n }\n\n return callee.type === AST_NODE_TYPES.MemberExpression &&\n !callee.computed &&\n callee.object.type === AST_NODE_TYPES.Identifier &&\n namespaceNames.has(callee.object.name) &&\n callee.property.type === AST_NODE_TYPES.Identifier\n ? {\n canonicalName: callee.property.name,\n propertyNode: callee.property,\n isNamespace: true,\n }\n : undefined;\n };\n};\n\n/**\n * Whether `name` resolves, at `node`, to a binding that is not a ts-fortress\n * import — a local declaration, a parameter, or an import from another module.\n * An autofix that makes a call reference `name` must bail out in that case,\n * because the rewritten call would resolve to the wrong binding.\n *\n * Scope-based rather than a scan of the top-level statements, so a binding that\n * merely shadows the ts-fortress one around the call site is caught too.\n */\nexport const hasForeignBinding = (\n sourceCode: TSESLint.SourceCode,\n node: TSESTree.Node,\n name: string,\n): boolean => {\n for (\n let mut_scope: TSESLint.Scope.Scope | null = sourceCode.getScope(node);\n mut_scope !== null;\n mut_scope = mut_scope.upper\n ) {\n const variable = mut_scope.set.get(name);\n\n // The innermost binding wins, so the first scope that declares `name`\n // decides — an outer ts-fortress import does not rescue a shadowed name.\n if (variable !== undefined) {\n return variable.defs.some((def) => !isTsFortressImportDef(def.node));\n }\n }\n\n return false;\n};\n\n/**\n * Inserts `import { <name> } from 'ts-fortress';` for each name at the top of\n * the file. Placement is deliberately naive: prettier-plugin-organize-imports\n * (or any equivalent) merges and sorts them afterwards.\n *\n * All names go into a single fix on purpose: ESLint merges the fixes of one\n * report into one edit span, so two reports each inserting at the start of the\n * file would conflict and one of them would be dropped.\n */\nexport const buildImportsFix = (\n fixer: TSESLint.RuleFixer,\n program: TSESTree.Program,\n names: readonly string[],\n): readonly TSESLint.RuleFix[] =>\n names.length === 0\n ? ([] as const)\n : ([\n fixer.insertTextBefore(\n program,\n names\n .map((name) => `import { ${name} } from '${TS_FORTRESS_MODULE}';\\n`)\n .join(''),\n ),\n ] as const);\n\nconst isTsFortressImportDef = (node: TSESTree.Node): boolean =>\n (node.type === AST_NODE_TYPES.ImportSpecifier ||\n node.type === AST_NODE_TYPES.ImportDefaultSpecifier ||\n node.type === AST_NODE_TYPES.ImportNamespaceSpecifier) &&\n node.parent.type === AST_NODE_TYPES.ImportDeclaration &&\n node.parent.source.value === TS_FORTRESS_MODULE;\n\n/* eslint-enable @typescript-eslint/prefer-readonly-parameter-types */\n"],"names":[],"mappings":";;;AAmBO,MAAM,oBAAA,GAAuB,CAClC,OAAA,KAEA,OAAA,CAAQ,IAAA,CAAK,MAAA;AAAA,EACX,CAAC,SACC,IAAA,CAAK,IAAA,KAAS,eAAe,iBAAA,IAC7B,IAAA,CAAK,OAAO,KAAA,KAAU;AAC1B;AAQK,MAAM,oBAAA,GAAuB,CAClC,kBAAA,EACA,YAAA,KAEA,kBAAA,CACG,QAAQ,CAAC,WAAA,KAAgB,WAAA,CAAY,UAAU,CAAA,CAC/C,IAAA;AAAA,EACC,CAAC,SAAA,KACC,SAAA,CAAU,IAAA,KAAS,cAAA,CAAe,oBACjC,SAAA,CAAU,QAAA,CAAS,IAAA,KAAS,cAAA,CAAe,aACxC,SAAA,CAAU,QAAA,CAAS,IAAA,GACnB,SAAA,CAAU,SAAS,KAAA,MAAW;AACtC,CAAA,EAAG,KAAA,CAAM;AASN,MAAM,mBAAA,GAAsB,CACjC,kBAAA,KACkE;AAClE,EAAA,MAAM,aAAa,kBAAA,CAAmB,OAAA;AAAA,IACpC,CAAC,gBAAgB,WAAA,CAAY;AAAA,GAC/B;AAEA,EAAA,MAAM,iBAAiB,IAAI,GAAA;AAAA,IACzB,UAAA,CACG,MAAA;AAAA,MACC,CAAC,SAAA,KACC,SAAA,CAAU,IAAA,KAAS,cAAA,CAAe;AAAA,MAErC,GAAA,CAAI,CAAC,SAAA,KAAc,SAAA,CAAU,MAAM,IAAI;AAAA,GAC5C;AAEA,EAAA,MAAM,mBAAmB,IAAI,GAAA;AAAA,IAC3B,UAAA,CAAW,OAAA,CAAQ,CAAC,SAAA,KAAc;AAChC,MAAA,IAAI,SAAA,CAAU,IAAA,KAAS,cAAA,CAAe,eAAA,SAAwB,EA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M,QAAA,GAAW,SAAA,CAAU,GAAA,CAAI,GAAA,CAAI,IAAI,CAAA;AAIvC,IAAA,IAAI,aAAa,MAAA,EAAW;AAC1B,MAAA,OAAO,QAAA,CAAS,KAAK,IAAA,CAAK,CAAC,QAAQ,CAAC,qBAAA,CAAsB,GAAA,CAAI,IAAI,CAAC,CAAA;AAAA,IACrE;AAAA,EACF;AAEA,EAAA,OAAO,KAAA;AACT;AAWO,MAAM,eAAA,GAAkB,CAC7B,KAAA,EACA,OAAA,EACA,UAEA,KAAA,CAAM,MAAA,KAAW,CAAA,GACZ,EAAC,GACD;AAAA,EACC,KAAA,CAAM,gBAAA;AAAA,IACJ,OAAA;AAAA,IACA,MACG,GAAA,CAAI,CAAC,SAAS,CAAA,SAAA,EAAY,IAAI,YAAY,kBAAkB,CAAA;AAAA,CAAM,CAAA,CAClE,KAAK,EAAE;AAAA;AAEd;AAEN,MAAM,qBAAA,GAAwB,CAAC,IAAA,KAAA,CAC5B,IAAA,CAAK,SAAS,cAAA,CAAe,eAAA,IAC5B,IAAA,CAAK,IAAA,KAAS,cAAA,CAAe,sBAAA,IAC7B,KAAK,IAAA,KAAS,cAAA,CAAe,wBAAA,KAC/B,IAAA,CAAK,MAAA,CAAO,IAAA,KAAS,eAAe,iBAAA,IACpC,IAAA,CAAK,MAAA,CAAO,MAAA,CAAO,KAAA,KAAU,kBAAA;;;;"}
1
+ {"version":3,"file":"import-utils.mjs","sources":["../../src/rules/import-utils.mts"],"sourcesContent":["import {\n AST_NODE_TYPES,\n type TSESLint,\n type TSESTree,\n} from '@typescript-eslint/utils';\nimport { Arr } from 'ts-data-forge';\nimport { TS_FORTRESS_MODULE } from './constants.mjs';\n\n/* eslint-disable @typescript-eslint/prefer-readonly-parameter-types */\n\nexport type ResolvedCallee = Readonly<{\n /** The canonical (imported) ts-fortress name, regardless of local alias. */\n canonicalName: string;\n /** The identifier node to rewrite when replacing the callee name. */\n propertyNode: TSESTree.Node;\n /** Whether the call was made through a namespace import (`t.foo(...)`). */\n isNamespace: boolean;\n}>;\n\n/** Every `import ... from 'ts-fortress'` declaration in the file. */\nexport const getTsFortressImports = (\n program: TSESTree.Program,\n): readonly TSESTree.ImportDeclaration[] =>\n program.body.filter(\n (node): node is TSESTree.ImportDeclaration =>\n node.type === AST_NODE_TYPES.ImportDeclaration &&\n node.source.value === TS_FORTRESS_MODULE,\n );\n\n/**\n * Returns the in-scope local name a given ts-fortress export is imported as\n * (handling aliases such as `import { nonEmptyArray as nea }` → `'nea'`), or\n * `undefined` when it is not imported. Used so autofixes reference the binding\n * that actually exists rather than the canonical name.\n */\nexport const getImportedLocalName = (\n importDeclarations: readonly TSESTree.ImportDeclaration[],\n importedName: string,\n): string | undefined =>\n importDeclarations\n .flatMap((declaration) => declaration.specifiers)\n .find(\n (specifier) =>\n specifier.type === AST_NODE_TYPES.ImportSpecifier &&\n (specifier.imported.type === AST_NODE_TYPES.Identifier\n ? specifier.imported.name\n : specifier.imported.value) === importedName,\n )?.local.name;\n\n/**\n * Builds a resolver that maps a call expression's callee to the canonical\n * ts-fortress function it refers to. Handles named imports (including aliases)\n * and namespace imports (`import * as t` → `t.foo(...)`) — the latter being the\n * idiomatic way to consume ts-fortress. Returns `undefined` for any callee that\n * does not reference the given ts-fortress imports.\n */\nexport const buildCalleeResolver = (\n importDeclarations: readonly TSESTree.ImportDeclaration[],\n): ((callee: TSESTree.Expression) => ResolvedCallee | undefined) => {\n const specifiers = importDeclarations.flatMap(\n (declaration) => declaration.specifiers,\n );\n\n const namespaceNames = new Set(\n specifiers\n .filter(\n (specifier) =>\n specifier.type === AST_NODE_TYPES.ImportNamespaceSpecifier,\n )\n .map((specifier) => specifier.local.name),\n );\n\n const localToCanonical = new Map<string, string>(\n specifiers.flatMap((specifier) => {\n if (specifier.type !== AST_NODE_TYPES.ImportSpecifier) return [];\n\n const importedName =\n specifier.imported.type === AST_NODE_TYPES.Identifier\n ? specifier.imported.name\n : specifier.imported.value;\n\n return typeof importedName === 'string'\n ? [[specifier.local.name, importedName] as const]\n : [];\n }),\n );\n\n return (callee) => {\n if (callee.type === AST_NODE_TYPES.Identifier) {\n const canonicalName = localToCanonical.get(callee.name);\n\n return canonicalName === undefined\n ? undefined\n : { canonicalName, propertyNode: callee, isNamespace: false };\n }\n\n return callee.type === AST_NODE_TYPES.MemberExpression &&\n !callee.computed &&\n callee.object.type === AST_NODE_TYPES.Identifier &&\n namespaceNames.has(callee.object.name) &&\n callee.property.type === AST_NODE_TYPES.Identifier\n ? {\n canonicalName: callee.property.name,\n propertyNode: callee.property,\n isNamespace: true,\n }\n : undefined;\n };\n};\n\n/**\n * Whether `name` resolves, at `node`, to a binding that is not a ts-fortress\n * import — a local declaration, a parameter, or an import from another module.\n * An autofix that makes a call reference `name` must bail out in that case,\n * because the rewritten call would resolve to the wrong binding.\n *\n * Scope-based rather than a scan of the top-level statements, so a binding that\n * merely shadows the ts-fortress one around the call site is caught too.\n */\nexport const hasForeignBinding = (\n sourceCode: TSESLint.SourceCode,\n node: TSESTree.Node,\n name: string,\n): boolean => {\n for (\n let mut_scope: TSESLint.Scope.Scope | null = sourceCode.getScope(node);\n mut_scope !== null;\n mut_scope = mut_scope.upper\n ) {\n const variable = mut_scope.set.get(name);\n\n // The innermost binding wins, so the first scope that declares `name`\n // decides — an outer ts-fortress import does not rescue a shadowed name.\n if (variable !== undefined) {\n return variable.defs.some((def) => !isTsFortressImportDef(def.node));\n }\n }\n\n return false;\n};\n\n/**\n * Inserts `import { <name> } from 'ts-fortress';` for each name at the top of\n * the file. Placement is deliberately naive: prettier-plugin-organize-imports\n * (or any equivalent) merges and sorts them afterwards.\n *\n * All names go into a single fix on purpose: ESLint merges the fixes of one\n * report into one edit span, so two reports each inserting at the start of the\n * file would conflict and one of them would be dropped.\n */\nexport const buildImportsFix = (\n fixer: TSESLint.RuleFixer,\n program: TSESTree.Program,\n names: readonly string[],\n): readonly TSESLint.RuleFix[] =>\n Arr.isEmpty(names)\n ? ([] as const)\n : ([\n fixer.insertTextBefore(\n program,\n names\n .map((name) => `import { ${name} } from '${TS_FORTRESS_MODULE}';\\n`)\n .join(''),\n ),\n ] as const);\n\nconst isTsFortressImportDef = (node: TSESTree.Node): boolean =>\n (node.type === AST_NODE_TYPES.ImportSpecifier ||\n node.type === AST_NODE_TYPES.ImportDefaultSpecifier ||\n node.type === AST_NODE_TYPES.ImportNamespaceSpecifier) &&\n node.parent.type === AST_NODE_TYPES.ImportDeclaration &&\n node.parent.source.value === TS_FORTRESS_MODULE;\n"],"names":[],"mappings":";;;;AAoBO,MAAM,oBAAA,GAAuB,CAClC,OAAA,KAEA,OAAA,CAAQ,IAAA,CAAK,MAAA;AAAA,EACX,CAAC,SACC,IAAA,CAAK,IAAA,KAAS,eAAe,iBAAA,IAC7B,IAAA,CAAK,OAAO,KAAA,KAAU;AAC1B;AAQK,MAAM,oBAAA,GAAuB,CAClC,kBAAA,EACA,YAAA,KAEA,kBAAA,CACG,QAAQ,CAAC,WAAA,KAAgB,WAAA,CAAY,UAAU,CAAA,CAC/C,IAAA;AAAA,EACC,CAAC,SAAA,KACC,SAAA,CAAU,IAAA,KAAS,cAAA,CAAe,oBACjC,SAAA,CAAU,QAAA,CAAS,IAAA,KAAS,cAAA,CAAe,aACxC,SAAA,CAAU,QAAA,CAAS,IAAA,GACnB,SAAA,CAAU,SAAS,KAAA,MAAW;AACtC,CAAA,EAAG,KAAA,CAAM;AASN,MAAM,mBAAA,GAAsB,CACjC,kBAAA,KACkE;AAClE,EAAA,MAAM,aAAa,kBAAA,CAAmB,OAAA;AAAA,IACpC,CAAC,gBAAgB,WAAA,CAAY;AAAA,GAC/B;AAEA,EAAA,MAAM,iBAAiB,IAAI,GAAA;AAAA,IACzB,UAAA,CACG,MAAA;AAAA,MACC,CAAC,SAAA,KACC,SAAA,CAAU,IAAA,KAAS,cAAA,CAAe;AAAA,MAErC,GAAA,CAAI,CAAC,SAAA,KAAc,SAAA,CAAU,MAAM,IAAI;AAAA,GAC5C;AAEA,EAAA,MAAM,m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,KACY;AACZ,EAAA,KAAA,IACM,SAAA,GAAyC,WAAW,QAAA,CAAS,IAAI,GACrE,SAAA,KAAc,IAAA,EACd,SAAA,GAAY,SAAA,CAAU,KAAA,EACtB;AACA,IAAA,MAAM,QAAA,GAAW,SAAA,CAAU,GAAA,CAAI,GAAA,CAAI,IAAI,CAAA;AAIvC,IAAA,IAAI,aAAa,MAAA,EAAW;AAC1B,MAAA,OAAO,QAAA,CAAS,KAAK,IAAA,CAAK,CAAC,QAAQ,CAAC,qBAAA,CAAsB,GAAA,CAAI,IAAI,CAAC,CAAA;AAAA,IACrE;AAAA,EACF;AAEA,EAAA,OAAO,KAAA;AACT;AAWO,MAAM,eAAA,GAAkB,CAC7B,KAAA,EACA,OAAA,EACA,KAAA,KAEA,IAAI,OAAA,CAAQ,KAAK,CAAA,GACZ,EAAC,GACD;AAAA,EACC,KAAA,CAAM,gBAAA;AAAA,IACJ,OAAA;AAAA,IACA,MACG,GAAA,CAAI,CAAC,SAAS,CAAA,SAAA,EAAY,IAAI,YAAY,kBAAkB,CAAA;AAAA,CAAM,CAAA,CAClE,KAAK,EAAE;AAAA;AAEd;AAEN,MAAM,qBAAA,GAAwB,CAAC,IAAA,KAAA,CAC5B,IAAA,CAAK,SAAS,cAAA,CAAe,eAAA,IAC5B,IAAA,CAAK,IAAA,KAAS,cAAA,CAAe,sBAAA,IAC7B,KAAK,IAAA,KAAS,cAAA,CAAe,wBAAA,KAC/B,IAAA,CAAK,MAAA,CAAO,IAAA,KAAS,eAAe,iBAAA,IACpC,IAAA,CAAK,MAAA,CAAO,MAAA,CAAO,KAAA,KAAU,kBAAA;;;;"}
@@ -1 +1 @@
1
- {"version":3,"file":"prefer-canonical-length-constrained-type.mjs","sources":["../../src/rules/prefer-canonical-length-constrained-type.mts"],"sourcesContent":["import {\n AST_NODE_TYPES,\n type TSESLint,\n type TSESTree,\n} from '@typescript-eslint/utils';\nimport {\n ARRAY_FN,\n BOUNDED_LENGTH_TUPLE_FN,\n FIXED_LENGTH_TUPLE_FN,\n MAX_LENGTH_TUPLE_FN,\n MIN_LENGTH_ARRAY_FN,\n MIN_LENGTH_TUPLE_FN,\n NON_EMPTY_ARRAY_FN,\n STRUCTURAL_PREFIX_CAP,\n} from './constants.mjs';\nimport {\n buildCalleeResolver,\n buildImportsFix,\n getImportedLocalName,\n getTsFortressImports,\n hasForeignBinding,\n} from './import-utils.mjs';\n\ntype Options = readonly [];\n\ntype MessageIds = 'useCanonicalType';\n\n/** How one leading length argument of a combinator call must look. */\ntype BoundMatcher = number | 'anyLength' | 'sameAsFirst';\n\ntype Rewrite = Readonly<{\n /** The canonical ts-fortress combinator the call resolves to. */\n from: string;\n /** One matcher per leading length argument the call must have. */\n bounds: readonly BoundMatcher[];\n /** The combinator the call is rewritten to. */\n to: string;\n /** Indices into `bounds` of the length arguments the rewrite keeps, in order. */\n keep: readonly number[];\n /**\n * Whether an options object carrying `defaultValue` blocks the rewrite,\n * because the two combinators type that option differently.\n */\n blockedByDefaultValue: boolean;\n}>;\n\n/**\n * Combinator calls whose length arguments make them a longhand spelling of\n * another combinator.\n *\n * Every entry is **type-identical**, not merely assignable — the two calls\n * produce the very same `Type<T>`, verified against the ts-type-forge\n * definitions:\n *\n * | call | canonical form | why |\n * | :---------------------------- | :------------------------ | :-- |\n * | `minLengthArray(1, x)` | `nonEmptyArray(x)` | `NonEmptyArray<A>` is defined as `MinLengthArray<1, A>` |\n * | `minLengthTuple(0, x)` | `array(x)` | `MinLengthTuple<0, A>` is `readonly A[]` — no constraint at all |\n * | `maxLengthTuple(0, x)` | `fixedLengthTuple(0, x)` | both are `readonly []` |\n * | `boundedLengthTuple(n, n, x)` | `fixedLengthTuple(n, x)` | the union collapses to its single longest member |\n * | `boundedLengthTuple(0, n, x)` | `maxLengthTuple(n, x)` | `MaxLengthTuple<N, A>` is defined as `BoundedLengthTuple<0, N, A>` |\n *\n * The rewrite also preserves `defaultValue` and every accepted/rejected value;\n * only the default `typeName` (and the `details.kind` of the length error it\n * reports) changes, to the one that names the constraint being checked.\n *\n * The `*Array` family has no analogous entries on purpose — see the README:\n * its length constraint lives in a brand, and `BoundedLengthArray<Min, Max, A>`\n * is an intersection of both bounds' brands, so neither\n * `boundedLengthArray(0, n, x)` → `maxLengthArray(n, x)` (which drops the\n * `MinLengthArray<0>` brand) nor `boundedLengthArray(n, n, x)` →\n * `fixedLengthArray(n, x)` (which adds an exact tuple for `n <= 10`) keeps the\n * type unchanged.\n */\nconst REWRITES = [\n {\n from: MIN_LENGTH_ARRAY_FN,\n bounds: [1],\n to: NON_EMPTY_ARRAY_FN,\n keep: [],\n blockedByDefaultValue: true,\n },\n {\n from: MIN_LENGTH_TUPLE_FN,\n bounds: [0],\n to: ARRAY_FN,\n keep: [],\n blockedByDefaultValue: false,\n },\n {\n from: MAX_LENGTH_TUPLE_FN,\n bounds: [0],\n to: FIXED_LENGTH_TUPLE_FN,\n keep: [0],\n blockedByDefaultValue: false,\n },\n // Listed before the `0, n` entry so that `boundedLengthTuple(0, 0, x)`\n // normalizes to `fixedLengthTuple(0, x)`, the more specific of the two.\n {\n from: BOUNDED_LENGTH_TUPLE_FN,\n bounds: ['anyLength', 'sameAsFirst'],\n to: FIXED_LENGTH_TUPLE_FN,\n keep: [0],\n blockedByDefaultValue: false,\n },\n {\n from: BOUNDED_LENGTH_TUPLE_FN,\n bounds: [0, 'anyLength'],\n to: MAX_LENGTH_TUPLE_FN,\n keep: [1],\n blockedByDefaultValue: false,\n },\n] as const satisfies readonly Rewrite[];\n\nexport const preferCanonicalLengthConstrainedType: TSESLint.RuleModule<\n MessageIds,\n Options\n> = {\n meta: {\n type: 'suggestion',\n docs: {\n description:\n '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.',\n },\n fixable: 'code',\n schema: [],\n messages: {\n useCanonicalType:\n '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).',\n },\n },\n\n create: (context) => {\n const sourceCode = context.sourceCode;\n\n const program = sourceCode.ast;\n\n const tsFortressImports = getTsFortressImports(program);\n\n const resolveCallee = buildCalleeResolver(tsFortressImports);\n\n const mut_matches: {\n node: TSESTree.CallExpression;\n propertyNode: TSESTree.Node;\n isNamespace: boolean;\n rewrite: Rewrite;\n /** The name the rewritten call will use; `undefined` when unavailable. */\n localName: string | undefined;\n needsImport: boolean;\n }[] = [];\n\n return {\n CallExpression: (node) => {\n const resolved = resolveCallee(node.callee);\n\n if (resolved === undefined) return;\n\n const rewrite = REWRITES.find(\n (candidate) =>\n candidate.from === resolved.canonicalName &&\n matchesRewrite(node, candidate),\n );\n\n if (rewrite === undefined) return;\n\n // A namespace call (`t.maxLengthTuple(...)`) only swaps the property\n // name, so it never needs a binding of its own.\n if (resolved.isNamespace) {\n mut_matches.push({\n node,\n propertyNode: resolved.propertyNode,\n isNamespace: true,\n rewrite,\n localName: rewrite.to,\n needsImport: false,\n });\n\n return;\n }\n\n const importedLocalName = getImportedLocalName(\n tsFortressImports,\n rewrite.to,\n );\n\n const localName = importedLocalName ?? rewrite.to;\n\n mut_matches.push({\n node,\n propertyNode: resolved.propertyNode,\n isNamespace: false,\n rewrite,\n localName: hasForeignBinding(sourceCode, node, localName)\n ? undefined\n : localName,\n needsImport: importedLocalName === undefined,\n });\n },\n 'Program:exit': () => {\n const reportable = mut_matches.filter(\n (match) => match.localName !== undefined,\n );\n\n const namesNeedingImport = Array.from(\n new Set(\n reportable\n .filter((match) => match.needsImport)\n .map((match) => match.rewrite.to),\n ),\n );\n\n // Every missing import rides on a single report; see `buildImportsFix`.\n const importFixIndex = reportable.findIndex(\n (match) => match.needsImport,\n );\n\n for (const [\n index,\n { node, propertyNode, rewrite, localName },\n ] of reportable.entries()) {\n if (localName === undefined) continue;\n\n context.report({\n node,\n messageId: 'useCanonicalType',\n data: {\n original: rewrite.from,\n replacement: localName,\n boundsText: boundsText(node, rewrite, sourceCode),\n },\n fix: (fixer) => [\n ...(index === importFixIndex\n ? buildImportsFix(fixer, program, namesNeedingImport)\n : []),\n fixer.replaceText(propertyNode, localName),\n ...dropBoundsFix(fixer, node, rewrite),\n ],\n });\n }\n },\n };\n },\n defaultOptions: [],\n} as const;\n\n/* eslint-disable @typescript-eslint/prefer-readonly-parameter-types */\n\n/**\n * Whether `node` is a `<combinator>(...bounds, elementType, options?)` call\n * whose bounds match `rewrite` exactly as written.\n */\nconst matchesRewrite = (\n node: TSESTree.CallExpression,\n rewrite: Rewrite,\n): boolean => {\n const args = node.arguments;\n\n const boundCount = rewrite.bounds.length;\n\n if (\n args.length < boundCount + 1 ||\n args.length > boundCount + 2 ||\n // Explicit type arguments pin the bound independently of the literal, so\n // dropping or moving the literal could change the resulting type.\n node.typeArguments !== undefined ||\n args.some((arg) => arg.type === AST_NODE_TYPES.SpreadElement)\n ) {\n return false;\n }\n\n const bounds = args.slice(0, boundCount).map(getLengthLiteral);\n\n const [first] = bounds;\n\n const boundsMatch = rewrite.bounds.every((matcher, index) => {\n const value = bounds[index];\n\n if (value === undefined) return false;\n\n switch (matcher) {\n case 'anyLength':\n return value <= STRUCTURAL_PREFIX_CAP;\n\n case 'sameAsFirst':\n return value === first && value <= STRUCTURAL_PREFIX_CAP;\n\n default:\n return value === matcher;\n }\n });\n\n return (\n boundsMatch &&\n (!rewrite.blockedByDefaultValue ||\n !passesDefaultValue(args[boundCount + 1]))\n );\n};\n\n/** The bounds of the matched call, as written, for the report message. */\nconst boundsText = (\n node: TSESTree.CallExpression,\n rewrite: Rewrite,\n sourceCode: TSESLint.SourceCode,\n): string =>\n node.arguments\n .slice(0, rewrite.bounds.length)\n .map((arg) => sourceCode.getText(arg))\n .join(', ');\n\n/**\n * Removes the length arguments the rewrite drops. Each removal runs from the\n * dropped argument up to the start of the next one, so the comma, whitespace\n * and any comments in between go with it; the ranges never overlap.\n */\nconst dropBoundsFix = (\n fixer: TSESLint.RuleFixer,\n node: TSESTree.CallExpression,\n rewrite: Rewrite,\n): readonly TSESLint.RuleFix[] =>\n rewrite.bounds.flatMap((_, index) => {\n if (rewrite.keep.includes(index)) return [];\n\n const dropped = node.arguments[index];\n\n const next = node.arguments[index + 1];\n\n return dropped === undefined || next === undefined\n ? []\n : [fixer.removeRange([dropped.range[0], next.range[0]])];\n });\n\n/**\n * A non-negative integer literal's value, or `undefined` for anything else —\n * a computed bound cannot be matched against the rewrite table.\n */\nconst getLengthLiteral = (node: TSESTree.Node): number | undefined =>\n node.type === AST_NODE_TYPES.Literal &&\n typeof node.value === 'number' &&\n Number.isSafeInteger(node.value) &&\n node.value >= 0\n ? node.value\n : undefined;\n\n/**\n * Whether the options argument may carry a `defaultValue`. Anything that is not\n * a plain object literal of statically known keys counts, since its contents\n * cannot be ruled out.\n */\nconst passesDefaultValue = (node: TSESTree.Node | undefined): boolean =>\n node !== undefined &&\n (node.type !== AST_NODE_TYPES.ObjectExpression ||\n node.properties.some(\n (property) =>\n property.type !== AST_NODE_TYPES.Property ||\n property.computed ||\n property.key.type !== AST_NODE_TYPES.Identifier ||\n property.key.name === 'defaultValue',\n ));\n\n/* eslint-enable @typescript-eslint/prefer-readonly-parameter-types */\n"],"names":[],"mappings":";;;;AA0EA,MAAM,QAAA,GAAW;AAAA,EACf;AAAA,IACE,IAAA,EAAM,mBAAA;AAAA,IACN,MAAA,EAAQ,CAAC,CAAC,CAAA;AAAA,IACV,EAAA,EAAI,kBAAA;AAAA,IACJ,MAAM,EAAC;AAAA,IACP,qBAAA,EAAuB;AAAA,GACzB;AAAA,EACA;AAAA,IACE,IAAA,EAAM,mBAAA;AAAA,IACN,MAAA,EAAQ,CAAC,CAAC,CAAA;AAAA,IACV,EAAA,EAAI,QAAA;AAAA,IACJ,MAAM,EAAC;AAAA,IACP,qBAAA,EAAuB;AAAA,GACzB;AAAA,EACA;AAAA,IACE,IAAA,EAAM,mBAAA;AAAA,IACN,MAAA,EAAQ,CAAC,CAAC,CA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,KAAK,CAAA,SAAU,EAAC;AAE1C,EAAA,MAAM,OAAA,GAAU,IAAA,CAAK,SAAA,CAAU,KAAK,CAAA;AAEpC,EAAA,MAAM,IAAA,GAAO,IAAA,CAAK,SAAA,CAAU,KAAA,GAAQ,CAAC,CAAA;AAErC,EAAA,OAAO,YAAY,MAAA,IAAa,IAAA,KAAS,SACrC,EAAC,GACD,CAAC,KAAA,CAAM,WAAA,CAAY,CAAC,OAAA,CAAQ,KAAA,CAAM,CAAC,CAAA,EAAG,IAAA,CAAK,MAAM,CAAC,CAAC,CAAC,CAAC,CAAA;AAC3D,CAAC,CAAA;AAMH,MAAM,gBAAA,GAAmB,CAAC,IAAA,KACxB,IAAA,CAAK,SAAS,cAAA,CAAe,OAAA,IAC7B,OAAO,IAAA,CAAK,KAAA,KAAU,YACtB,MAAA,CAAO,aAAA,CAAc,KAAK,KAAK,CAAA,IAC/B,KAAK,KAAA,IAAS,CAAA,GACV,KAAK,KAAA,GACL,MAAA;AAON,MAAM,kBAAA,GAAqB,CAAC,IAAA,KAC1B,IAAA,KAAS,MAAA,KACR,KAAK,IAAA,KAAS,cAAA,CAAe,gBAAA,IAC5B,IAAA,CAAK,UAAA,CAAW,IAAA;AAAA,EACd,CAAC,QAAA,KACC,QAAA,CAAS,IAAA,KAAS,eAAe,QAAA,IACjC,QAAA,CAAS,QAAA,IACT,QAAA,CAAS,IAAI,IAAA,KAAS,cAAA,CAAe,UAAA,IACrC,QAAA,CAAS,IAAI,IAAA,KAAS;AAC1B,CAAA,CAAA;;;;"}
1
+ {"version":3,"file":"prefer-canonical-length-constrained-type.mjs","sources":["../../src/rules/prefer-canonical-length-constrained-type.mts"],"sourcesContent":["import {\n AST_NODE_TYPES,\n type TSESLint,\n type TSESTree,\n} from '@typescript-eslint/utils';\nimport {\n ARRAY_FN,\n BOUNDED_LENGTH_TUPLE_FN,\n FIXED_LENGTH_TUPLE_FN,\n MAX_LENGTH_TUPLE_FN,\n MIN_LENGTH_ARRAY_FN,\n MIN_LENGTH_TUPLE_FN,\n NON_EMPTY_ARRAY_FN,\n STRUCTURAL_PREFIX_CAP,\n} from './constants.mjs';\nimport {\n buildCalleeResolver,\n buildImportsFix,\n getImportedLocalName,\n getTsFortressImports,\n hasForeignBinding,\n} from './import-utils.mjs';\n\ntype Options = readonly [];\n\ntype MessageIds = 'useCanonicalType';\n\n/** How one leading length argument of a combinator call must look. */\ntype BoundMatcher = number | 'anyLength' | 'sameAsFirst';\n\ntype Rewrite = Readonly<{\n /** The canonical ts-fortress combinator the call resolves to. */\n from: string;\n /** One matcher per leading length argument the call must have. */\n bounds: readonly BoundMatcher[];\n /** The combinator the call is rewritten to. */\n to: string;\n /** Indices into `bounds` of the length arguments the rewrite keeps, in order. */\n keep: readonly number[];\n /**\n * Whether an options object carrying `defaultValue` blocks the rewrite,\n * because the two combinators type that option differently.\n */\n blockedByDefaultValue: boolean;\n}>;\n\n/**\n * Combinator calls whose length arguments make them a longhand spelling of\n * another combinator.\n *\n * Every entry is **type-identical**, not merely assignable — the two calls\n * produce the very same `Type<T>`, verified against the ts-type-forge\n * definitions:\n *\n * | call | canonical form | why |\n * | :---------------------------- | :------------------------ | :-- |\n * | `minLengthArray(1, x)` | `nonEmptyArray(x)` | `NonEmptyArray<A>` is defined as `MinLengthArray<1, A>` |\n * | `minLengthTuple(0, x)` | `array(x)` | `MinLengthTuple<0, A>` is `readonly A[]` — no constraint at all |\n * | `maxLengthTuple(0, x)` | `fixedLengthTuple(0, x)` | both are `readonly []` |\n * | `boundedLengthTuple(n, n, x)` | `fixedLengthTuple(n, x)` | the union collapses to its single longest member |\n * | `boundedLengthTuple(0, n, x)` | `maxLengthTuple(n, x)` | `MaxLengthTuple<N, A>` is defined as `BoundedLengthTuple<0, N, A>` |\n *\n * The rewrite also preserves `defaultValue` and every accepted/rejected value;\n * only the default `typeName` (and the `details.kind` of the length error it\n * reports) changes, to the one that names the constraint being checked.\n *\n * The `*Array` family has no analogous entries on purpose — see the README:\n * its length constraint lives in a brand, and `BoundedLengthArray<Min, Max, A>`\n * is an intersection of both bounds' brands, so neither\n * `boundedLengthArray(0, n, x)` → `maxLengthArray(n, x)` (which drops the\n * `MinLengthArray<0>` brand) nor `boundedLengthArray(n, n, x)` →\n * `fixedLengthArray(n, x)` (which adds an exact tuple for `n <= 10`) keeps the\n * type unchanged.\n */\nconst REWRITES = [\n {\n from: MIN_LENGTH_ARRAY_FN,\n bounds: [1],\n to: NON_EMPTY_ARRAY_FN,\n keep: [],\n blockedByDefaultValue: true,\n },\n {\n from: MIN_LENGTH_TUPLE_FN,\n bounds: [0],\n to: ARRAY_FN,\n keep: [],\n blockedByDefaultValue: false,\n },\n {\n from: MAX_LENGTH_TUPLE_FN,\n bounds: [0],\n to: FIXED_LENGTH_TUPLE_FN,\n keep: [0],\n blockedByDefaultValue: false,\n },\n // Listed before the `0, n` entry so that `boundedLengthTuple(0, 0, x)`\n // normalizes to `fixedLengthTuple(0, x)`, the more specific of the two.\n {\n from: BOUNDED_LENGTH_TUPLE_FN,\n bounds: ['anyLength', 'sameAsFirst'],\n to: FIXED_LENGTH_TUPLE_FN,\n keep: [0],\n blockedByDefaultValue: false,\n },\n {\n from: BOUNDED_LENGTH_TUPLE_FN,\n bounds: [0, 'anyLength'],\n to: MAX_LENGTH_TUPLE_FN,\n keep: [1],\n blockedByDefaultValue: false,\n },\n] as const satisfies readonly Rewrite[];\n\nexport const preferCanonicalLengthConstrainedType: TSESLint.RuleModule<\n MessageIds,\n Options\n> = {\n meta: {\n type: 'suggestion',\n docs: {\n description:\n '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.',\n },\n fixable: 'code',\n schema: [],\n messages: {\n useCanonicalType:\n '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).',\n },\n },\n\n create: (context) => {\n const sourceCode = context.sourceCode;\n\n const program = sourceCode.ast;\n\n const tsFortressImports = getTsFortressImports(program);\n\n const resolveCallee = buildCalleeResolver(tsFortressImports);\n\n const mut_matches: {\n node: TSESTree.CallExpression;\n propertyNode: TSESTree.Node;\n isNamespace: boolean;\n rewrite: Rewrite;\n /** The name the rewritten call will use; `undefined` when unavailable. */\n localName: string | undefined;\n needsImport: boolean;\n }[] = [];\n\n return {\n CallExpression: (node) => {\n const resolved = resolveCallee(node.callee);\n\n if (resolved === undefined) return;\n\n const rewrite = REWRITES.find(\n (candidate) =>\n candidate.from === resolved.canonicalName &&\n matchesRewrite(node, candidate),\n );\n\n if (rewrite === undefined) return;\n\n // A namespace call (`t.maxLengthTuple(...)`) only swaps the property\n // name, so it never needs a binding of its own.\n if (resolved.isNamespace) {\n mut_matches.push({\n node,\n propertyNode: resolved.propertyNode,\n isNamespace: true,\n rewrite,\n localName: rewrite.to,\n needsImport: false,\n });\n\n return;\n }\n\n const importedLocalName = getImportedLocalName(\n tsFortressImports,\n rewrite.to,\n );\n\n const localName = importedLocalName ?? rewrite.to;\n\n mut_matches.push({\n node,\n propertyNode: resolved.propertyNode,\n isNamespace: false,\n rewrite,\n localName: hasForeignBinding(sourceCode, node, localName)\n ? undefined\n : localName,\n needsImport: importedLocalName === undefined,\n });\n },\n 'Program:exit': () => {\n const reportable = mut_matches.filter(\n (match) => match.localName !== undefined,\n );\n\n const namesNeedingImport = Array.from(\n new Set(\n reportable\n .filter((match) => match.needsImport)\n .map((match) => match.rewrite.to),\n ),\n );\n\n // Every missing import rides on a single report; see `buildImportsFix`.\n const importFixIndex = reportable.findIndex(\n (match) => match.needsImport,\n );\n\n for (const [\n index,\n { node, propertyNode, rewrite, localName },\n ] of reportable.entries()) {\n if (localName === undefined) continue;\n\n context.report({\n node,\n messageId: 'useCanonicalType',\n data: {\n original: rewrite.from,\n replacement: localName,\n boundsText: boundsText(node, rewrite, sourceCode),\n },\n fix: (fixer) => [\n ...(index === importFixIndex\n ? buildImportsFix(fixer, program, namesNeedingImport)\n : []),\n fixer.replaceText(propertyNode, localName),\n ...dropBoundsFix(fixer, node, rewrite),\n ],\n });\n }\n },\n };\n },\n defaultOptions: [],\n} as const;\n\n/* eslint-disable @typescript-eslint/prefer-readonly-parameter-types */\n\n/**\n * Whether `node` is a `<combinator>(...bounds, elementType, options?)` call\n * whose bounds match `rewrite` exactly as written.\n */\nconst matchesRewrite = (\n node: TSESTree.CallExpression,\n rewrite: Rewrite,\n): boolean => {\n const args = node.arguments;\n\n const boundCount = rewrite.bounds.length;\n\n if (\n args.length < boundCount + 1 ||\n args.length > boundCount + 2 ||\n // Explicit type arguments pin the bound independently of the literal, so\n // dropping or moving the literal could change the resulting type.\n node.typeArguments !== undefined ||\n args.some((arg) => arg.type === AST_NODE_TYPES.SpreadElement)\n ) {\n return false;\n }\n\n const bounds = args.slice(0, boundCount).map(getLengthLiteral);\n\n const [first] = bounds;\n\n const boundsMatch = rewrite.bounds.every((matcher, index) => {\n const value = bounds[index];\n\n if (value === undefined) return false;\n\n switch (matcher) {\n case 'anyLength':\n return value <= STRUCTURAL_PREFIX_CAP;\n\n case 'sameAsFirst':\n return value === first && value <= STRUCTURAL_PREFIX_CAP;\n\n default:\n return value === matcher;\n }\n });\n\n return (\n boundsMatch &&\n (!rewrite.blockedByDefaultValue ||\n !passesDefaultValue(args[boundCount + 1]))\n );\n};\n\n/** The bounds of the matched call, as written, for the report message. */\nconst boundsText = (\n node: TSESTree.CallExpression,\n rewrite: Rewrite,\n sourceCode: TSESLint.SourceCode,\n): string =>\n node.arguments\n .slice(0, rewrite.bounds.length)\n .map((arg) => sourceCode.getText(arg))\n .join(', ');\n\n/**\n * Removes the length arguments the rewrite drops. Each removal runs from the\n * dropped argument up to the start of the next one, so the comma, whitespace\n * and any comments in between go with it; the ranges never overlap.\n */\nconst dropBoundsFix = (\n fixer: TSESLint.RuleFixer,\n node: TSESTree.CallExpression,\n rewrite: Rewrite,\n): readonly TSESLint.RuleFix[] =>\n rewrite.bounds.flatMap((_, index) => {\n if (rewrite.keep.includes(index)) return [];\n\n const dropped = node.arguments[index];\n\n const next = node.arguments[index + 1];\n\n return dropped === undefined || next === undefined\n ? []\n : [fixer.removeRange([dropped.range[0], next.range[0]])];\n });\n\n/**\n * A non-negative integer literal's value, or `undefined` for anything else —\n * a computed bound cannot be matched against the rewrite table.\n */\nconst getLengthLiteral = (node: TSESTree.Node): number | undefined =>\n node.type === AST_NODE_TYPES.Literal &&\n typeof node.value === 'number' &&\n Number.isSafeInteger(node.value) &&\n node.value >= 0\n ? node.value\n : undefined;\n\n/**\n * Whether the options argument may carry a `defaultValue`. Anything that is not\n * a plain object literal of statically known keys counts, since its contents\n * cannot be ruled out.\n */\nconst passesDefaultValue = (node: TSESTree.Node | undefined): boolean =>\n node !== undefined &&\n (node.type !== AST_NODE_TYPES.ObjectExpression ||\n node.properties.some(\n (property) =>\n property.type !== AST_NODE_TYPES.Property ||\n property.computed ||\n property.key.type !== AST_NODE_TYPES.Identifier ||\n property.key.name === 'defaultValue',\n ));\n"],"names":[],"mappings":";;;;AA0EA,MAAM,QAAA,GAAW;AAAA,EACf;AAAA,IACE,IAAA,EAAM,mBAAA;AAAA,IACN,MAAA,EAAQ,CAAC,CAAC,CAAA;AAAA,IACV,EAAA,EAAI,kBAAA;AAAA,IACJ,MAAM,EAAC;AAAA,IACP,qBAAA,EAAuB;AAAA,GACzB;AAAA,EACA;AAAA,IACE,IAAA,EAAM,mBAAA;AAAA,IACN,MAAA,EAAQ,CAAC,CAAC,CAAA;AAAA,IACV,EAAA,EAAI,QAAA;AAAA,IACJ,MAAM,EAAC;AAAA,IACP,qBAAA,EAAuB;AAAA,GACzB;AAAA,EACA;AAAA,IACE,IAAA,EAAM,mBAAA;AAAA,IACN,MAAA,EAAQ,CAAC,CAAC,CAAA;AAAA,IACV,EAAA,EAAI,qBAAA;AAAA,IACJ,IAAA,EAAM,CAAC,CAAC,CAAA;AAAA,IACR,qBAAA,EAAuB;AAAA,GACzB;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AAA,GAAO,IAAA,CAAK,SAAA,CAAU,KAAA,GAAQ,CAAC,CAAA;AAErC,EAAA,OAAO,YAAY,MAAA,IAAa,IAAA,KAAS,SACrC,EAAC,GACD,CAAC,KAAA,CAAM,WAAA,CAAY,CAAC,OAAA,CAAQ,KAAA,CAAM,CAAC,CAAA,EAAG,IAAA,CAAK,MAAM,CAAC,CAAC,CAAC,CAAC,CAAA;AAC3D,CAAC,CAAA;AAMH,MAAM,gBAAA,GAAmB,CAAC,IAAA,KACxB,IAAA,CAAK,SAAS,cAAA,CAAe,OAAA,IAC7B,OAAO,IAAA,CAAK,KAAA,KAAU,YACtB,MAAA,CAAO,aAAA,CAAc,KAAK,KAAK,CAAA,IAC/B,KAAK,KAAA,IAAS,CAAA,GACV,KAAK,KAAA,GACL,MAAA;AAON,MAAM,kBAAA,GAAqB,CAAC,IAAA,KAC1B,IAAA,KAAS,MAAA,KACR,KAAK,IAAA,KAAS,cAAA,CAAe,gBAAA,IAC5B,IAAA,CAAK,UAAA,CAAW,IAAA;AAAA,EACd,CAAC,QAAA,KACC,QAAA,CAAS,IAAA,KAAS,eAAe,QAAA,IACjC,QAAA,CAAS,QAAA,IACT,QAAA,CAAS,IAAI,IAAA,KAAS,cAAA,CAAe,UAAA,IACrC,QAAA,CAAS,IAAI,IAAA,KAAS;AAC1B,CAAA,CAAA;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-ts-fortress",
3
- "version": "0.4.0",
3
+ "version": "1.0.0",
4
4
  "private": false,
5
5
  "description": "ESLint rules that steer schema definitions toward ts-fortress idioms.",
6
6
  "keywords": [
@@ -36,7 +36,8 @@
36
36
  ],
37
37
  "dependencies": {
38
38
  "@typescript-eslint/utils": "8.65.0",
39
- "ts-type-forge": "7.x"
39
+ "ts-data-forge": "~14.1.0",
40
+ "ts-type-forge": "~9.1.3"
40
41
  },
41
42
  "devDependencies": {
42
43
  "@typescript-eslint/parser": "8.65.0",
package/src/plugin.mts CHANGED
@@ -1,3 +1,4 @@
1
+ import { type ReadonlyRecord } from 'ts-type-forge';
1
2
  import { tsFortressRules } from './rules/index.mjs';
2
3
  import { type ESLintFlatConfig, type ESLintPlugin } from './types.mjs';
3
4
 
@@ -9,8 +10,9 @@ import { type ESLintFlatConfig, type ESLintPlugin } from './types.mjs';
9
10
  */
10
11
  const recommendedRules = {
11
12
  'ts-fortress/prefer-canonical-length-constrained-type': 'error',
12
- } as const satisfies Readonly<
13
- Record<`ts-fortress/${keyof typeof tsFortressRules}`, 'error'>
13
+ } as const satisfies ReadonlyRecord<
14
+ `ts-fortress/${keyof typeof tsFortressRules}`,
15
+ 'error'
14
16
  >;
15
17
 
16
18
  const recommendedConfig = {
@@ -4,7 +4,7 @@
4
4
  // (`scripts/cmd/gen-rule-types.mts`). Do not edit it manually.
5
5
 
6
6
  import { type Linter } from 'eslint';
7
- import { type TypeEq } from 'ts-type-forge';
7
+ import { type ReadonlyRecord, type TypeEq } from 'ts-type-forge';
8
8
  import { type tsFortressRules } from './rules/index.mjs';
9
9
 
10
10
  /**
@@ -26,7 +26,7 @@ export type EslintTsFortressRules = Readonly<{
26
26
  'ts-fortress/prefer-canonical-length-constrained-type': PreferCanonicalLengthConstrainedType.RuleEntry;
27
27
  }>;
28
28
 
29
- export type EslintTsFortressRulesOption = Readonly<Record<never, never>>;
29
+ export type EslintTsFortressRulesOption = ReadonlyRecord<never, never>;
30
30
 
31
31
  // If this assertion fails to type-check, this generated file has drifted from
32
32
  // the actual plugin rules — re-run `pnpm run gen:rule-types`.
@@ -3,6 +3,7 @@ import {
3
3
  type TSESLint,
4
4
  type TSESTree,
5
5
  } from '@typescript-eslint/utils';
6
+ import { Arr } from 'ts-data-forge';
6
7
  import { TS_FORTRESS_MODULE } from './constants.mjs';
7
8
 
8
9
  /* eslint-disable @typescript-eslint/prefer-readonly-parameter-types */
@@ -152,7 +153,7 @@ export const buildImportsFix = (
152
153
  program: TSESTree.Program,
153
154
  names: readonly string[],
154
155
  ): readonly TSESLint.RuleFix[] =>
155
- names.length === 0
156
+ Arr.isEmpty(names)
156
157
  ? ([] as const)
157
158
  : ([
158
159
  fixer.insertTextBefore(
@@ -169,5 +170,3 @@ const isTsFortressImportDef = (node: TSESTree.Node): boolean =>
169
170
  node.type === AST_NODE_TYPES.ImportNamespaceSpecifier) &&
170
171
  node.parent.type === AST_NODE_TYPES.ImportDeclaration &&
171
172
  node.parent.source.value === TS_FORTRESS_MODULE;
172
-
173
- /* eslint-enable @typescript-eslint/prefer-readonly-parameter-types */
@@ -356,5 +356,3 @@ const passesDefaultValue = (node: TSESTree.Node | undefined): boolean =>
356
356
  property.key.type !== AST_NODE_TYPES.Identifier ||
357
357
  property.key.name === 'defaultValue',
358
358
  ));
359
-
360
- /* eslint-enable @typescript-eslint/prefer-readonly-parameter-types */