eslint-plugin-functype 2.111.0 → 2.111.1

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.
package/README.md CHANGED
@@ -165,6 +165,18 @@ Not followed: aliases (`const t = s; t.add(x)`), `useRef(new Set()).current.add(
165
165
 
166
166
  functype's collections are immutable, so a cache, registry or in-place accumulator can't use them. A native collection that is only read (`.has`, `.get`, iteration) is still reported.
167
167
 
168
+ **A declared native read-only contract is also exempt.** The rules never report a `ReadonlyMap` / `ReadonlySet` annotation, so they don't report the constructor such an annotation requires. A `new Map(...)` / `new Set(...)` is skipped when its value flows directly into:
169
+
170
+ - a `return` in the nearest function declared to return `ReadonlyMap<…>` (or `Promise<ReadonlyMap<…>>`);
171
+ - the expression body of an arrow function with that return type;
172
+ - a variable, class field or parameter default annotated `ReadonlyMap<…>`;
173
+ - an `as ReadonlyMap<…>` / `satisfies ReadonlyMap<…>` assertion;
174
+ - any of the above through a ternary branch or a `??` / `||` / `&&` operand.
175
+
176
+ This is the escape for an interface that takes a native `ReadonlyMap` — declare the type, don't disable the rule. A callback nested inside such a function doesn't inherit its signature, and a mutable `Map<K, V>` annotation is still reported. To hand a functype `Map` to such an interface, put the conversion in one typed helper: `const toReadonly = <K, V>(m: FMap<K, V>): ReadonlyMap<K, V> => new Map(m)`.
177
+
178
+ **Imports are resolved where `Map` / `Set` is used.** `import { Map as FMap } from "functype"` no longer silences the file: the bare `Map` there is still the native one and is reported, and the suggestions use your name (`FMap(entries)`, `FMap.empty()`) without adding a second import.
179
+
168
180
  ### `functype/no-imperative-loops` / `functype/prefer-map`
169
181
 
170
182
  These rules don't report loops that have no functional equivalent:
@@ -1,2 +1,2 @@
1
- import{getFunctypeImportsLegacy as e,isAlreadyUsingFunctype as t}from"../utils/functype-detection.js";import{createImportFixer as n,hasFunctypeSymbol as r}from"../utils/import-fixer.js";import{MAP_MUTATORS as i,annotatesMutatedBinding as a,isMutatedCollection as o}from"../utils/mutable-collection.js";const s={meta:{type:`suggestion`,hasSuggestions:!0,docs:{description:`Prefer functype Map<K, V> over native Map for immutable key-value collections`,recommended:!0},schema:[{type:`object`,properties:{allowInTests:{type:`boolean`,default:!0},allowMutable:{type:`boolean`,default:!0}},additionalProperties:!1}],messages:{preferFunctypeMap:`Prefer functype Map<{{keyType}}, {{valueType}}> over native Map`,preferFunctypeMapLiteral:`Prefer Map.of(...) or Map.empty() over new Map()`,suggestMapEmpty:`Replace with Map.empty()`,suggestMapOf:`Replace with Map.of(...)`,suggestMapFrom:`Replace with Map(...)`,suggestAddImport:`Add {{symbol}} import from functype`}},create(s){let c=s.options[0]||{},l=c.allowInTests!==!1,u=c.allowMutable!==!1;function d(){let e=s.filename;return/\.(test|spec)\.(ts|js|tsx|jsx)$/.test(e)||e.includes(`__tests__`)||e.includes(`/test/`)||e.includes(`/tests/`)}let f=e(s);function p(){return r(s.sourceCode,`Map`)}return{NewExpression(e){if(l&&d()||!e.callee||e.callee.type!==`Identifier`||e.callee.name!==`Map`||u&&o(e,i,s.sourceCode)||p()||t(e,f))return;let r=s.sourceCode,a=e.arguments,c=[];if(a.length===0)c.push({messageId:`suggestMapEmpty`,fix(t){return t.replaceText(e,`Map.empty()`)}});else if(a.length===1&&a[0].type===`ArrayExpression`){let t=a[0].elements.map(e=>r.getText(e)).join(`, `);c.push({messageId:`suggestMapOf`,fix(n){return n.replaceText(e,`Map.of(${t})`)}})}else if(a.length===1){let t=r.getText(a[0]);c.push({messageId:`suggestMapFrom`,fix(n){return n.replaceText(e,`Map(${t})`)}})}else c.push({messageId:`suggestMapEmpty`,fix(t){return t.replaceText(e,`Map.empty()`)}});p()||c.push({messageId:`suggestAddImport`,data:{symbol:`Map`},fix:n(r,`Map`)}),s.report({node:e,messageId:`preferFunctypeMapLiteral`,suggest:c})},TSTypeReference(e){if(l&&d()||!e.typeName)return;let t=s.sourceCode;if((e.typeName.type===`Identifier`?e.typeName.name:t.getText(e.typeName))!==`Map`||u&&a(e,i,s.sourceCode)||p())return;let r=e.typeArguments?.params??e.typeParameters?.params,o=r?.[0]?t.getText(r[0]):`K`,c=r&&r.length>=2?t.getText(r[1]):`V`,f=[{messageId:`suggestAddImport`,data:{symbol:`Map`},fix:n(t,`Map`)}];s.report({node:e,messageId:`preferFunctypeMap`,data:{keyType:o,valueType:c},suggest:f})}}}};export{s as default};
1
+ import{getFunctypeImportsLegacy as e,isAlreadyUsingFunctype as t}from"../utils/functype-detection.js";import{createImportFixer as n}from"../utils/import-fixer.js";import{bindingOf as r,functypeLocalName as i}from"../utils/collection-binding.js";import{MAP_MUTATORS as a,annotatesMutatedBinding as o,isMutatedCollection as s}from"../utils/mutable-collection.js";import{flowsIntoReadonlyContract as c}from"../utils/readonly-contract.js";const l={meta:{type:`suggestion`,hasSuggestions:!0,docs:{description:`Prefer functype Map<K, V> over native Map for immutable key-value collections`,recommended:!0},schema:[{type:`object`,properties:{allowInTests:{type:`boolean`,default:!0},allowMutable:{type:`boolean`,default:!0}},additionalProperties:!1}],messages:{preferFunctypeMap:`Prefer functype Map<{{keyType}}, {{valueType}}> over native Map`,preferFunctypeMapLiteral:`Prefer Map.of(...) or Map.empty() over new Map()`,suggestMapEmpty:`Replace with {{name}}.empty()`,suggestMapOf:`Replace with {{name}}.of(...)`,suggestMapFrom:`Replace with {{name}}(...)`,suggestUseLocalName:`Replace with {{name}}`,suggestAddImport:`Add {{symbol}} import from functype`}},create(l){let u=l.options[0]||{},d=u.allowInTests!==!1,f=u.allowMutable!==!1;function p(){let e=l.filename;return/\.(test|spec)\.(ts|js|tsx|jsx)$/.test(e)||e.includes(`__tests__`)||e.includes(`/test/`)||e.includes(`/tests/`)}let m=e(l);return{NewExpression(e){if(d&&p()||!e.callee||e.callee.type!==`Identifier`||e.callee.name!==`Map`||f&&s(e,a,l.sourceCode)||r(e.callee,`Map`,l.sourceCode)!==`native`||c(e,`ReadonlyMap`)||t(e,m))return;let o=l.sourceCode,u=i(o,`Map`),h=u??`Map`,g=e.arguments,_=[];if(g.length===0)_.push({messageId:`suggestMapEmpty`,data:{name:h},fix(t){return t.replaceText(e,`${h}.empty()`)}});else if(g.length===1&&g[0].type===`ArrayExpression`){let t=g[0].elements.map(e=>o.getText(e)).join(`, `);_.push({messageId:`suggestMapOf`,data:{name:h},fix(n){return n.replaceText(e,`${h}.of(${t})`)}})}else if(g.length===1){let t=o.getText(g[0]);_.push({messageId:`suggestMapFrom`,data:{name:h},fix(n){return n.replaceText(e,`${h}(${t})`)}})}else _.push({messageId:`suggestMapEmpty`,data:{name:h},fix(t){return t.replaceText(e,`${h}.empty()`)}});u===null&&_.push({messageId:`suggestAddImport`,data:{symbol:`Map`},fix:n(o,`Map`)}),l.report({node:e,messageId:`preferFunctypeMapLiteral`,suggest:_})},TSTypeReference(e){if(d&&p()||!e.typeName)return;let t=l.sourceCode;if((e.typeName.type===`Identifier`?e.typeName.name:t.getText(e.typeName))!==`Map`||f&&o(e,a,l.sourceCode)||e.typeName.type===`Identifier`&&r(e.typeName,`Map`,l.sourceCode)!==`native`)return;let s=e.typeArguments?.params??e.typeParameters?.params,c=s?.[0]?t.getText(s[0]):`K`,u=s&&s.length>=2?t.getText(s[1]):`V`,m=i(t,`Map`),h=m===null?[{messageId:`suggestAddImport`,data:{symbol:`Map`},fix:n(t,`Map`)}]:[{messageId:`suggestUseLocalName`,data:{name:m},fix:t=>t.replaceText(e.typeName,m)}];l.report({node:e,messageId:`preferFunctypeMap`,data:{keyType:c,valueType:u},suggest:h})}}}};export{l as default};
2
2
  //# sourceMappingURL=prefer-functype-map.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"prefer-functype-map.js","names":["rule","options","context","allowInTests","allowMutable","isInTestFile","filename","functypeImports","getFunctypeImportsLegacy","isMapImportedFromFunctype","hasFunctypeSymbol","node","isMutatedCollection","MAP_MUTATORS","isAlreadyUsingFunctype","sourceCode","args","suggestions","fixer","mapOfArgs","el","argText","createImportFixer","annotatesMutatedBinding","typeParams","keyType","valueType"],"sources":["../../src/rules/prefer-functype-map.ts"],"sourcesContent":["import type { Rule } from \"eslint\"\n\nimport type { ASTNode } from \"../types/ast\"\nimport { getFunctypeImportsLegacy, isAlreadyUsingFunctype } from \"../utils/functype-detection\"\nimport { createImportFixer, hasFunctypeSymbol } from \"../utils/import-fixer\"\nimport { annotatesMutatedBinding, isMutatedCollection, MAP_MUTATORS } from \"../utils/mutable-collection\"\n\nconst rule: Rule.RuleModule = {\n meta: {\n type: \"suggestion\",\n hasSuggestions: true,\n docs: {\n description: \"Prefer functype Map<K, V> over native Map for immutable key-value collections\",\n recommended: true,\n },\n schema: [\n {\n type: \"object\",\n properties: {\n allowInTests: {\n type: \"boolean\",\n default: true,\n },\n allowMutable: {\n type: \"boolean\",\n default: true,\n },\n },\n additionalProperties: false,\n },\n ],\n messages: {\n preferFunctypeMap: \"Prefer functype Map<{{keyType}}, {{valueType}}> over native Map\",\n preferFunctypeMapLiteral: \"Prefer Map.of(...) or Map.empty() over new Map()\",\n suggestMapEmpty: \"Replace with Map.empty()\",\n suggestMapOf: \"Replace with Map.of(...)\",\n suggestMapFrom: \"Replace with Map(...)\",\n suggestAddImport: \"Add {{symbol}} import from functype\",\n },\n },\n\n create(context) {\n const options = context.options[0] || {}\n const allowInTests = options.allowInTests !== false\n // A native Map the code mutates on purpose (cache, registry, accumulator) is a deliberate choice —\n // functype's Map is immutable and cannot express it (#324).\n const allowMutable = options.allowMutable !== false\n\n function isInTestFile() {\n const filename = context.filename\n return (\n /\\.(test|spec)\\.(ts|js|tsx|jsx)$/.test(filename) ||\n filename.includes(\"__tests__\") ||\n filename.includes(\"/test/\") ||\n filename.includes(\"/tests/\")\n )\n }\n\n const functypeImports = getFunctypeImportsLegacy(context)\n\n function isMapImportedFromFunctype(): boolean {\n return hasFunctypeSymbol(context.sourceCode, \"Map\")\n }\n\n return {\n NewExpression(node: ASTNode) {\n if (allowInTests && isInTestFile()) return\n\n // Only flag `new Map(...)` calls\n if (!node.callee || node.callee.type !== \"Identifier\" || node.callee.name !== \"Map\") return\n\n if (allowMutable && isMutatedCollection(node, MAP_MUTATORS, context.sourceCode)) return\n\n // Skip if Map is already imported from functype\n if (isMapImportedFromFunctype()) return\n\n // Skip if already using functype\n if (isAlreadyUsingFunctype(node, functypeImports)) return\n\n const sourceCode = context.sourceCode\n const args = node.arguments as ASTNode[]\n\n const suggestions: Rule.SuggestionReportDescriptor[] = []\n\n if (args.length === 0) {\n // new Map() → Map.empty()\n suggestions.push({\n messageId: \"suggestMapEmpty\",\n fix(fixer) {\n return fixer.replaceText(node, \"Map.empty()\")\n },\n })\n } else if (args.length === 1 && args[0].type === \"ArrayExpression\") {\n // new Map([[\"a\", 1], [\"b\", 2]]) → Map.of([\"a\", 1], [\"b\", 2])\n const arrayArg = args[0] as ASTNode\n const elements = arrayArg.elements as ASTNode[]\n const tupleTexts = elements.map((el: ASTNode) => sourceCode.getText(el))\n const mapOfArgs = tupleTexts.join(\", \")\n suggestions.push({\n messageId: \"suggestMapOf\",\n fix(fixer) {\n return fixer.replaceText(node, `Map.of(${mapOfArgs})`)\n },\n })\n } else if (args.length === 1) {\n // new Map(someVar) → Map(someVar)\n const argText = sourceCode.getText(args[0])\n suggestions.push({\n messageId: \"suggestMapFrom\",\n fix(fixer) {\n return fixer.replaceText(node, `Map(${argText})`)\n },\n })\n } else {\n // Generic fallback for any other args\n suggestions.push({\n messageId: \"suggestMapEmpty\",\n fix(fixer) {\n return fixer.replaceText(node, \"Map.empty()\")\n },\n })\n }\n\n if (!isMapImportedFromFunctype()) {\n suggestions.push({\n messageId: \"suggestAddImport\",\n data: { symbol: \"Map\" },\n fix: createImportFixer(sourceCode, \"Map\"),\n })\n }\n\n context.report({\n node,\n messageId: \"preferFunctypeMapLiteral\",\n suggest: suggestions,\n })\n },\n\n TSTypeReference(node: ASTNode) {\n if (allowInTests && isInTestFile()) return\n\n if (!node.typeName) return\n\n const sourceCode = context.sourceCode\n const typeName = node.typeName.type === \"Identifier\" ? node.typeName.name : sourceCode.getText(node.typeName)\n\n if (typeName !== \"Map\") return\n\n if (allowMutable && annotatesMutatedBinding(node, MAP_MUTATORS, context.sourceCode)) return\n\n // Skip if Map is already imported from functype\n if (isMapImportedFromFunctype()) return\n\n // Extract key/value type params if present (typeArguments for newer TS-ESLint, typeParameters for older)\n const typeParams = node.typeArguments?.params ?? node.typeParameters?.params\n const keyType = typeParams?.[0] ? sourceCode.getText(typeParams[0]) : \"K\"\n const valueType = typeParams && typeParams.length >= 2 ? sourceCode.getText(typeParams[1]) : \"V\"\n\n const suggestions: Rule.SuggestionReportDescriptor[] = [\n {\n messageId: \"suggestAddImport\",\n data: { symbol: \"Map\" },\n fix: createImportFixer(sourceCode, \"Map\"),\n },\n ]\n\n context.report({\n node,\n messageId: \"preferFunctypeMap\",\n data: { keyType, valueType },\n suggest: suggestions,\n })\n },\n }\n },\n}\n\nexport default rule\n"],"mappings":"8SAOA,MAAMA,EAAwB,CAC5B,KAAM,CACJ,KAAM,aACN,eAAgB,GAChB,KAAM,CACJ,YAAa,gFACb,YAAa,EACf,EACA,OAAQ,CACN,CACE,KAAM,SACN,WAAY,CACV,aAAc,CACZ,KAAM,UACN,QAAS,EACX,EACA,aAAc,CACZ,KAAM,UACN,QAAS,EACX,CACF,EACA,qBAAsB,EACxB,CACF,EACA,SAAU,CACR,kBAAmB,kEACnB,yBAA0B,mDAC1B,gBAAiB,2BACjB,aAAc,2BACd,eAAgB,wBAChB,iBAAkB,qCACpB,CACF,EAEA,OAAO,EAAS,CACd,IAAMC,EAAUC,EAAQ,QAAQ,IAAM,CAAC,EACjCC,EAAeF,EAAQ,eAAiB,GAGxCG,EAAeH,EAAQ,eAAiB,GAE9C,SAASI,GAAe,CACtB,IAAMC,EAAWJ,EAAQ,SACzB,MACE,kCAAkC,KAAKI,CAAQ,GAC/CA,EAAS,SAAS,WAAW,GAC7BA,EAAS,SAAS,QAAQ,GAC1BA,EAAS,SAAS,SAAS,CAE/B,CAEA,IAAMC,EAAkBC,EAAyBN,CAAO,EAExD,SAASO,GA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,OACA,UAAW,2BACX,QAASe,CACX,CAAC,CACH,EAEA,gBAAgB,EAAe,CAG7B,GAFId,GAAgBE,EAAa,GAE7B,CAACM,EAAK,SAAU,OAEpB,IAAMI,EAAab,EAAQ,WAQ3B,IAPiBS,EAAK,SAAS,OAAS,aAAeA,EAAK,SAAS,KAAOI,EAAW,QAAQJ,EAAK,QAAQ,KAE3F,OAEbP,GAAgBmB,EAAwBZ,EAAME,EAAcX,EAAQ,UAAU,GAG9EO,EAA0B,EAAG,OAGjC,IAAMe,EAAab,EAAK,eAAe,QAAUA,EAAK,gBAAgB,OAChEc,EAAUD,IAAa,GAAKT,EAAW,QAAQS,EAAW,EAAE,EAAI,IAChEE,EAAYF,GAAcA,EAAW,QAAU,EAAIT,EAAW,QAAQS,EAAW,EAAE,EAAI,IAEvFP,EAAiD,CACrD,CACE,UAAW,mBACX,KAAM,CAAE,OAAQ,KAAM,EACtB,IAAKK,EAAkBP,EAAY,KAAK,CAC1C,CACF,EAEAb,EAAQ,OAAO,CACb,OACA,UAAW,oBACX,KAAM,CAAE,UAAS,WAAU,EAC3B,QAASe,CACX,CAAC,CACH,CACF,CACF,CACF"}
1
+ {"version":3,"file":"prefer-functype-map.js","names":["rule","options","context","allowInTests","allowMutable","isInTestFile","filename","functypeImports","getFunctypeImportsLegacy","node","isMutatedCollection","MAP_MUTATORS","bindingOf","flowsIntoReadonlyContract","isAlreadyUsingFunctype","sourceCode","imported","functypeLocalName","name","args","suggestions","fixer","mapOfArgs","el","argText","createImportFixer","annotatesMutatedBinding","typeParams","keyType","valueType"],"sources":["../../src/rules/prefer-functype-map.ts"],"sourcesContent":["import type { Rule } from \"eslint\"\n\nimport type { ASTNode } from \"../types/ast\"\nimport { bindingOf, functypeLocalName } from \"../utils/collection-binding\"\nimport { getFunctypeImportsLegacy, isAlreadyUsingFunctype } from \"../utils/functype-detection\"\nimport { createImportFixer } from \"../utils/import-fixer\"\nimport { annotatesMutatedBinding, isMutatedCollection, MAP_MUTATORS } from \"../utils/mutable-collection\"\nimport { flowsIntoReadonlyContract } from \"../utils/readonly-contract\"\n\nconst rule: Rule.RuleModule = {\n meta: {\n type: \"suggestion\",\n hasSuggestions: true,\n docs: {\n description: \"Prefer functype Map<K, V> over native Map for immutable key-value collections\",\n recommended: true,\n },\n schema: [\n {\n type: \"object\",\n properties: {\n allowInTests: {\n type: \"boolean\",\n default: true,\n },\n allowMutable: {\n type: \"boolean\",\n default: true,\n },\n },\n additionalProperties: false,\n },\n ],\n messages: {\n preferFunctypeMap: \"Prefer functype Map<{{keyType}}, {{valueType}}> over native Map\",\n preferFunctypeMapLiteral: \"Prefer Map.of(...) or Map.empty() over new Map()\",\n suggestMapEmpty: \"Replace with {{name}}.empty()\",\n suggestMapOf: \"Replace with {{name}}.of(...)\",\n suggestMapFrom: \"Replace with {{name}}(...)\",\n suggestUseLocalName: \"Replace with {{name}}\",\n suggestAddImport: \"Add {{symbol}} import from functype\",\n },\n },\n\n create(context) {\n const options = context.options[0] || {}\n const allowInTests = options.allowInTests !== false\n // A native Map the code mutates on purpose (cache, registry, accumulator) is a deliberate choice —\n // functype's Map is immutable and cannot express it (#324).\n const allowMutable = options.allowMutable !== false\n\n function isInTestFile() {\n const filename = context.filename\n return (\n /\\.(test|spec)\\.(ts|js|tsx|jsx)$/.test(filename) ||\n filename.includes(\"__tests__\") ||\n filename.includes(\"/test/\") ||\n filename.includes(\"/tests/\")\n )\n }\n\n const functypeImports = getFunctypeImportsLegacy(context)\n\n return {\n NewExpression(node: ASTNode) {\n if (allowInTests && isInTestFile()) return\n\n // Only flag `new Map(...)` calls\n if (!node.callee || node.callee.type !== \"Identifier\" || node.callee.name !== \"Map\") return\n\n if (allowMutable && isMutatedCollection(node, MAP_MUTATORS, context.sourceCode)) return\n\n // `Map` may be functype's import (under any name), a local shadow, or the native built-in (#350).\n if (bindingOf(node.callee, \"Map\", context.sourceCode) !== \"native\") return\n\n // A native Map flowing into a declared `ReadonlyMap` contract is what that contract requires (#350).\n if (flowsIntoReadonlyContract(node, \"ReadonlyMap\")) return\n\n // Skip if already using functype\n if (isAlreadyUsingFunctype(node, functypeImports)) return\n\n const sourceCode = context.sourceCode\n const imported = functypeLocalName(sourceCode, \"Map\")\n const name = imported ?? \"Map\"\n const args = node.arguments as ASTNode[]\n\n const suggestions: Rule.SuggestionReportDescriptor[] = []\n\n if (args.length === 0) {\n // new Map() → Map.empty()\n suggestions.push({\n messageId: \"suggestMapEmpty\",\n data: { name },\n fix(fixer) {\n return fixer.replaceText(node, `${name}.empty()`)\n },\n })\n } else if (args.length === 1 && args[0].type === \"ArrayExpression\") {\n // new Map([[\"a\", 1], [\"b\", 2]]) → Map.of([\"a\", 1], [\"b\", 2])\n const arrayArg = args[0] as ASTNode\n const elements = arrayArg.elements as ASTNode[]\n const tupleTexts = elements.map((el: ASTNode) => sourceCode.getText(el))\n const mapOfArgs = tupleTexts.join(\", \")\n suggestions.push({\n messageId: \"suggestMapOf\",\n data: { name },\n fix(fixer) {\n return fixer.replaceText(node, `${name}.of(${mapOfArgs})`)\n },\n })\n } else if (args.length === 1) {\n // new Map(someVar) → Map(someVar)\n const argText = sourceCode.getText(args[0])\n suggestions.push({\n messageId: \"suggestMapFrom\",\n data: { name },\n fix(fixer) {\n return fixer.replaceText(node, `${name}(${argText})`)\n },\n })\n } else {\n // Generic fallback for any other args\n suggestions.push({\n messageId: \"suggestMapEmpty\",\n data: { name },\n fix(fixer) {\n return fixer.replaceText(node, `${name}.empty()`)\n },\n })\n }\n\n if (imported === null) {\n suggestions.push({\n messageId: \"suggestAddImport\",\n data: { symbol: \"Map\" },\n fix: createImportFixer(sourceCode, \"Map\"),\n })\n }\n\n context.report({\n node,\n messageId: \"preferFunctypeMapLiteral\",\n suggest: suggestions,\n })\n },\n\n TSTypeReference(node: ASTNode) {\n if (allowInTests && isInTestFile()) return\n\n if (!node.typeName) return\n\n const sourceCode = context.sourceCode\n const typeName = node.typeName.type === \"Identifier\" ? node.typeName.name : sourceCode.getText(node.typeName)\n\n if (typeName !== \"Map\") return\n\n if (allowMutable && annotatesMutatedBinding(node, MAP_MUTATORS, context.sourceCode)) return\n\n if (node.typeName.type === \"Identifier\" && bindingOf(node.typeName, \"Map\", context.sourceCode) !== \"native\")\n return\n\n // Extract key/value type params if present (typeArguments for newer TS-ESLint, typeParameters for older)\n const typeParams = node.typeArguments?.params ?? node.typeParameters?.params\n const keyType = typeParams?.[0] ? sourceCode.getText(typeParams[0]) : \"K\"\n const valueType = typeParams && typeParams.length >= 2 ? sourceCode.getText(typeParams[1]) : \"V\"\n\n // Already imported under another name (`Map as FMap`): point at that name instead of adding a\n // second import that would shadow the native `Map` the rest of the file uses.\n const imported = functypeLocalName(sourceCode, \"Map\")\n const suggestions: Rule.SuggestionReportDescriptor[] =\n imported === null\n ? [{ messageId: \"suggestAddImport\", data: { symbol: \"Map\" }, fix: createImportFixer(sourceCode, \"Map\") }]\n : [\n {\n messageId: \"suggestUseLocalName\",\n data: { name: imported },\n fix: (fixer) => fixer.replaceText(node.typeName, imported),\n },\n ]\n\n context.report({\n node,\n messageId: \"preferFunctypeMap\",\n data: { keyType, valueType },\n suggest: suggestions,\n })\n },\n }\n },\n}\n\nexport default rule\n"],"mappings":"mbASA,MAAMA,EAAwB,CAC5B,KAAM,CACJ,KAAM,aACN,eAAgB,GAChB,KAAM,CACJ,YAAa,gFACb,YAAa,EACf,EACA,OAAQ,CACN,CACE,KAAM,SACN,WAAY,CACV,aAAc,CACZ,KAAM,UACN,QAAS,EACX,EACA,aAAc,CACZ,KAAM,UACN,QAAS,EACX,CACF,EACA,qBAAsB,EACxB,CACF,EACA,SAAU,CACR,kBAAmB,kEACnB,yBAA0B,mDAC1B,gBAAiB,gCACjB,aAAc,gCACd,eAAgB,6BAChB,oBAAqB,wBACrB,iBAAkB,qCACpB,CACF,EAEA,OAAO,EAAS,CACd,IAAMC,EAAUC,EAAQ,QAAQ,IAAM,CAAC,EACjCC,EAAeF,EAAQ,eAAiB,GAGxCG,EAAeH,EAAQ,eA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AAYZ,EAAK,SAAUO,CAAQ,CAC3D,CACF,EAENd,EAAQ,OAAO,CACb,OACA,UAAW,oBACX,KAAM,CAAE,UAAS,WAAU,EAC3B,QAASkB,CACX,CAAC,CACH,CACF,CACF,CACF"}
@@ -1,2 +1,2 @@
1
- import{getFunctypeImportsLegacy as e,isAlreadyUsingFunctype as t}from"../utils/functype-detection.js";import{createImportFixer as n,hasFunctypeSymbol as r}from"../utils/import-fixer.js";import{SET_MUTATORS as i,annotatesMutatedBinding as a,isMutatedCollection as o}from"../utils/mutable-collection.js";const s={meta:{type:`suggestion`,hasSuggestions:!0,docs:{description:`Prefer functype Set<T> over native Set for immutable collections`,recommended:!0},schema:[{type:`object`,properties:{allowInTests:{type:`boolean`,default:!0},allowMutable:{type:`boolean`,default:!0}},additionalProperties:!1}],messages:{preferFunctypeSet:`Prefer functype Set<{{type}}> over native Set`,preferFunctypeSetLiteral:`Prefer Set.of(...) or Set.empty() over new Set()`,suggestSetEmpty:`Replace with Set.empty()`,suggestSetOf:`Replace with Set.of(...)`,suggestSetFrom:`Replace with Set(...)`,suggestAddImport:`Add {{symbol}} import from functype`}},create(s){let c=s.options[0]||{},l=c.allowInTests!==!1,u=c.allowMutable!==!1;function d(){let e=s.filename;return/\.(test|spec)\.(ts|js|tsx|jsx)$/.test(e)||e.includes(`__tests__`)||e.includes(`/test/`)||e.includes(`/tests/`)}let f=e(s);function p(){return r(s.sourceCode,`Set`)}return{NewExpression(e){if(l&&d()||!e.callee||e.callee.type!==`Identifier`||e.callee.name!==`Set`||u&&o(e,i,s.sourceCode)||p()||t(e,f))return;let r=s.sourceCode,a=e.arguments||[],c=[];if(a.length===0)c.push({messageId:`suggestSetEmpty`,fix(t){return t.replaceText(e,`Set.empty()`)}});else if(a.length===1&&a[0].type===`ArrayExpression`){let t=(a[0].elements||[]).map(e=>r.getText(e)).join(`, `);c.push({messageId:`suggestSetOf`,fix(n){return n.replaceText(e,`Set.of(${t})`)}})}else if(a.length===1){let t=r.getText(a[0]);c.push({messageId:`suggestSetFrom`,fix(n){return n.replaceText(e,`Set(${t})`)}})}else c.push({messageId:`suggestSetEmpty`,fix(t){return t.replaceText(e,`Set.empty()`)}});p()||c.push({messageId:`suggestAddImport`,data:{symbol:`Set`},fix:n(r,`Set`)}),s.report({node:e,messageId:`preferFunctypeSetLiteral`,suggest:c})},TSTypeReference(e){if(l&&d()||!e.typeName)return;let t=s.sourceCode;if((e.typeName.type===`Identifier`?e.typeName.name:t.getText(e.typeName))!==`Set`||u&&a(e,i,s.sourceCode)||p())return;let r=e.typeParameters?.params?.[0]??e.typeArguments?.params?.[0],o=r?t.getText(r):`T`,c=[{messageId:`suggestAddImport`,data:{symbol:`Set`},fix:n(t,`Set`)}];s.report({node:e,messageId:`preferFunctypeSet`,data:{type:o},suggest:c})}}}};export{s as default};
1
+ import{getFunctypeImportsLegacy as e,isAlreadyUsingFunctype as t}from"../utils/functype-detection.js";import{createImportFixer as n}from"../utils/import-fixer.js";import{bindingOf as r,functypeLocalName as i}from"../utils/collection-binding.js";import{SET_MUTATORS as a,annotatesMutatedBinding as o,isMutatedCollection as s}from"../utils/mutable-collection.js";import{flowsIntoReadonlyContract as c}from"../utils/readonly-contract.js";const l={meta:{type:`suggestion`,hasSuggestions:!0,docs:{description:`Prefer functype Set<T> over native Set for immutable collections`,recommended:!0},schema:[{type:`object`,properties:{allowInTests:{type:`boolean`,default:!0},allowMutable:{type:`boolean`,default:!0}},additionalProperties:!1}],messages:{preferFunctypeSet:`Prefer functype Set<{{type}}> over native Set`,preferFunctypeSetLiteral:`Prefer Set.of(...) or Set.empty() over new Set()`,suggestSetEmpty:`Replace with {{name}}.empty()`,suggestSetOf:`Replace with {{name}}.of(...)`,suggestSetFrom:`Replace with {{name}}(...)`,suggestUseLocalName:`Replace with {{name}}`,suggestAddImport:`Add {{symbol}} import from functype`}},create(l){let u=l.options[0]||{},d=u.allowInTests!==!1,f=u.allowMutable!==!1;function p(){let e=l.filename;return/\.(test|spec)\.(ts|js|tsx|jsx)$/.test(e)||e.includes(`__tests__`)||e.includes(`/test/`)||e.includes(`/tests/`)}let m=e(l);return{NewExpression(e){if(d&&p()||!e.callee||e.callee.type!==`Identifier`||e.callee.name!==`Set`||f&&s(e,a,l.sourceCode)||r(e.callee,`Set`,l.sourceCode)!==`native`||c(e,`ReadonlySet`)||t(e,m))return;let o=l.sourceCode,u=i(o,`Set`),h=u??`Set`,g=e.arguments||[],_=[];if(g.length===0)_.push({messageId:`suggestSetEmpty`,data:{name:h},fix(t){return t.replaceText(e,`${h}.empty()`)}});else if(g.length===1&&g[0].type===`ArrayExpression`){let t=(g[0].elements||[]).map(e=>o.getText(e)).join(`, `);_.push({messageId:`suggestSetOf`,data:{name:h},fix(n){return n.replaceText(e,`${h}.of(${t})`)}})}else if(g.length===1){let t=o.getText(g[0]);_.push({messageId:`suggestSetFrom`,data:{name:h},fix(n){return n.replaceText(e,`${h}(${t})`)}})}else _.push({messageId:`suggestSetEmpty`,data:{name:h},fix(t){return t.replaceText(e,`${h}.empty()`)}});u===null&&_.push({messageId:`suggestAddImport`,data:{symbol:`Set`},fix:n(o,`Set`)}),l.report({node:e,messageId:`preferFunctypeSetLiteral`,suggest:_})},TSTypeReference(e){if(d&&p()||!e.typeName)return;let t=l.sourceCode;if((e.typeName.type===`Identifier`?e.typeName.name:t.getText(e.typeName))!==`Set`||f&&o(e,a,l.sourceCode)||e.typeName.type===`Identifier`&&r(e.typeName,`Set`,l.sourceCode)!==`native`)return;let s=e.typeParameters?.params?.[0]??e.typeArguments?.params?.[0],c=s?t.getText(s):`T`,u=i(t,`Set`),m=u===null?[{messageId:`suggestAddImport`,data:{symbol:`Set`},fix:n(t,`Set`)}]:[{messageId:`suggestUseLocalName`,data:{name:u},fix:t=>t.replaceText(e.typeName,u)}];l.report({node:e,messageId:`preferFunctypeSet`,data:{type:c},suggest:m})}}}};export{l as default};
2
2
  //# sourceMappingURL=prefer-functype-set.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"prefer-functype-set.js","names":["rule","options","context","allowInTests","allowMutable","isInTestFile","filename","functypeImports","getFunctypeImportsLegacy","isSetImportedFromFunctype","hasFunctypeSymbol","node","isMutatedCollection","SET_MUTATORS","isAlreadyUsingFunctype","sourceCode","args","suggestions","fixer","argsText","el","argText","createImportFixer","annotatesMutatedBinding","typeParamNode","typeParam"],"sources":["../../src/rules/prefer-functype-set.ts"],"sourcesContent":["import type { Rule } from \"eslint\"\n\nimport type { ASTNode } from \"../types/ast\"\nimport { getFunctypeImportsLegacy, isAlreadyUsingFunctype } from \"../utils/functype-detection\"\nimport { createImportFixer, hasFunctypeSymbol } from \"../utils/import-fixer\"\nimport { annotatesMutatedBinding, isMutatedCollection, SET_MUTATORS } from \"../utils/mutable-collection\"\n\nconst rule: Rule.RuleModule = {\n meta: {\n type: \"suggestion\",\n hasSuggestions: true,\n docs: {\n description: \"Prefer functype Set<T> over native Set for immutable collections\",\n recommended: true,\n },\n schema: [\n {\n type: \"object\",\n properties: {\n allowInTests: {\n type: \"boolean\",\n default: true,\n },\n allowMutable: {\n type: \"boolean\",\n default: true,\n },\n },\n additionalProperties: false,\n },\n ],\n messages: {\n preferFunctypeSet: \"Prefer functype Set<{{type}}> over native Set\",\n preferFunctypeSetLiteral: \"Prefer Set.of(...) or Set.empty() over new Set()\",\n suggestSetEmpty: \"Replace with Set.empty()\",\n suggestSetOf: \"Replace with Set.of(...)\",\n suggestSetFrom: \"Replace with Set(...)\",\n suggestAddImport: \"Add {{symbol}} import from functype\",\n },\n },\n\n create(context) {\n const options = context.options[0] || {}\n const allowInTests = options.allowInTests !== false\n // A native Set the code mutates on purpose (cache, registry, accumulator) is a deliberate choice —\n // functype's Set is immutable and cannot express it (#324).\n const allowMutable = options.allowMutable !== false\n\n function isInTestFile() {\n const filename = context.filename\n return (\n /\\.(test|spec)\\.(ts|js|tsx|jsx)$/.test(filename) ||\n filename.includes(\"__tests__\") ||\n filename.includes(\"/test/\") ||\n filename.includes(\"/tests/\")\n )\n }\n\n const functypeImports = getFunctypeImportsLegacy(context)\n\n function isSetImportedFromFunctype() {\n return hasFunctypeSymbol(context.sourceCode, \"Set\")\n }\n\n return {\n NewExpression(node: ASTNode) {\n if (allowInTests && isInTestFile()) return\n\n // Only handle `new Set(...)` calls\n if (!node.callee || node.callee.type !== \"Identifier\" || node.callee.name !== \"Set\") return\n\n if (allowMutable && isMutatedCollection(node, SET_MUTATORS, context.sourceCode)) return\n\n // Skip if Set is already imported from functype\n if (isSetImportedFromFunctype()) return\n\n // Skip if already in a functype context\n if (isAlreadyUsingFunctype(node, functypeImports)) return\n\n const sourceCode = context.sourceCode\n const args = node.arguments || []\n const suggestions: Rule.SuggestionReportDescriptor[] = []\n\n if (args.length === 0) {\n // new Set() → Set.empty()\n suggestions.push({\n messageId: \"suggestSetEmpty\",\n fix(fixer) {\n return fixer.replaceText(node, \"Set.empty()\")\n },\n })\n } else if (args.length === 1 && args[0].type === \"ArrayExpression\") {\n // new Set([\"a\", \"b\", \"c\"]) → Set.of(\"a\", \"b\", \"c\")\n const arrayNode = args[0]\n const elements = arrayNode.elements || []\n const elementTexts = elements.map((el: ASTNode) => sourceCode.getText(el))\n const argsText = elementTexts.join(\", \")\n suggestions.push({\n messageId: \"suggestSetOf\",\n fix(fixer) {\n return fixer.replaceText(node, `Set.of(${argsText})`)\n },\n })\n } else if (args.length === 1) {\n // new Set(someVar) → Set(someVar)\n const argText = sourceCode.getText(args[0])\n suggestions.push({\n messageId: \"suggestSetFrom\",\n fix(fixer) {\n return fixer.replaceText(node, `Set(${argText})`)\n },\n })\n } else {\n // Fallback for unexpected cases\n suggestions.push({\n messageId: \"suggestSetEmpty\",\n fix(fixer) {\n return fixer.replaceText(node, \"Set.empty()\")\n },\n })\n }\n\n if (!isSetImportedFromFunctype()) {\n suggestions.push({\n messageId: \"suggestAddImport\",\n data: { symbol: \"Set\" },\n fix: createImportFixer(sourceCode, \"Set\"),\n })\n }\n\n context.report({\n node,\n messageId: \"preferFunctypeSetLiteral\",\n suggest: suggestions,\n })\n },\n\n TSTypeReference(node: ASTNode) {\n if (allowInTests && isInTestFile()) return\n\n if (!node.typeName) return\n\n const sourceCode = context.sourceCode\n const typeName = node.typeName.type === \"Identifier\" ? node.typeName.name : sourceCode.getText(node.typeName)\n\n if (typeName !== \"Set\") return\n\n if (allowMutable && annotatesMutatedBinding(node, SET_MUTATORS, context.sourceCode)) return\n\n // Skip if Set is already imported from functype\n if (isSetImportedFromFunctype()) return\n\n // Extract type parameter if present (typeArguments for newer TS-ESLint, typeParameters for older)\n const typeParamNode = node.typeParameters?.params?.[0] ?? node.typeArguments?.params?.[0]\n const typeParam = typeParamNode ? sourceCode.getText(typeParamNode) : \"T\"\n\n const suggestions: Rule.SuggestionReportDescriptor[] = [\n {\n messageId: \"suggestAddImport\",\n data: { symbol: \"Set\" },\n fix: createImportFixer(sourceCode, \"Set\"),\n },\n ]\n\n context.report({\n node,\n messageId: \"preferFunctypeSet\",\n data: { type: typeParam },\n suggest: suggestions,\n })\n },\n }\n },\n}\n\nexport default rule\n"],"mappings":"8SAOA,MAAMA,EAAwB,CAC5B,KAAM,CACJ,KAAM,aACN,eAAgB,GAChB,KAAM,CACJ,YAAa,mEACb,YAAa,EACf,EACA,OAAQ,CACN,CACE,KAAM,SACN,WAAY,CACV,aAAc,CACZ,KAAM,UACN,QAAS,EACX,EACA,aAAc,CACZ,KAAM,UACN,QAAS,EACX,CACF,EACA,qBAAsB,EACxB,CACF,EACA,SAAU,CACR,kBAAmB,gDACnB,yBAA0B,mDAC1B,gBAAiB,2BACjB,aAAc,2BACd,eAAgB,wBAChB,iBAAkB,qCACpB,CACF,EAEA,OAAO,EAAS,CACd,IAAMC,EAAUC,EAAQ,QAAQ,IAAM,CAAC,EACjCC,EAAeF,EAAQ,eAAiB,GAGxCG,EAAeH,EAAQ,eAAiB,GAE9C,SAASI,GAAe,CACtB,IAAMC,EAAWJ,EAAQ,SACzB,MACE,kCAAkC,KAAKI,CAAQ,GAC/CA,EAAS,SAAS,WA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EF,EAA0B,GAC7BQ,EAAY,KAAK,CACf,UAAW,mBACX,KAAM,CAAE,OAAQ,KAAM,EACtB,IAAKK,EAAkBP,EAAY,KAAK,CAC1C,CAAC,EAGHb,EAAQ,OAAO,CACb,OACA,UAAW,2BACX,QAASe,CACX,CAAC,CACH,EAEA,gBAAgB,EAAe,CAG7B,GAFId,GAAgBE,EAAa,GAE7B,CAACM,EAAK,SAAU,OAEpB,IAAMI,EAAab,EAAQ,WAQ3B,IAPiBS,EAAK,SAAS,OAAS,aAAeA,EAAK,SAAS,KAAOI,EAAW,QAAQJ,EAAK,QAAQ,KAE3F,OAEbP,GAAgBmB,EAAwBZ,EAAME,EAAcX,EAAQ,UAAU,GAG9EO,EAA0B,EAAG,OAGjC,IAAMe,EAAgBb,EAAK,gBAAgB,SAAS,IAAMA,EAAK,eAAe,SAAS,GACjFc,EAAYD,EAAgBT,EAAW,QAAQS,CAAa,EAAI,IAEhEP,EAAiD,CACrD,CACE,UAAW,mBACX,KAAM,CAAE,OAAQ,KAAM,EACtB,IAAKK,EAAkBP,EAAY,KAAK,CAC1C,CACF,EAEAb,EAAQ,OAAO,CACb,OACA,UAAW,oBACX,KAAM,CAAE,KAAMuB,CAAU,EACxB,QAASR,CACX,CAAC,CACH,CACF,CACF,CACF"}
1
+ {"version":3,"file":"prefer-functype-set.js","names":["rule","options","context","allowInTests","allowMutable","isInTestFile","filename","functypeImports","getFunctypeImportsLegacy","node","isMutatedCollection","SET_MUTATORS","bindingOf","flowsIntoReadonlyContract","isAlreadyUsingFunctype","sourceCode","imported","functypeLocalName","name","args","suggestions","fixer","argsText","el","argText","createImportFixer","annotatesMutatedBinding","typeParamNode","typeParam"],"sources":["../../src/rules/prefer-functype-set.ts"],"sourcesContent":["import type { Rule } from \"eslint\"\n\nimport type { ASTNode } from \"../types/ast\"\nimport { bindingOf, functypeLocalName } from \"../utils/collection-binding\"\nimport { getFunctypeImportsLegacy, isAlreadyUsingFunctype } from \"../utils/functype-detection\"\nimport { createImportFixer } from \"../utils/import-fixer\"\nimport { annotatesMutatedBinding, isMutatedCollection, SET_MUTATORS } from \"../utils/mutable-collection\"\nimport { flowsIntoReadonlyContract } from \"../utils/readonly-contract\"\n\nconst rule: Rule.RuleModule = {\n meta: {\n type: \"suggestion\",\n hasSuggestions: true,\n docs: {\n description: \"Prefer functype Set<T> over native Set for immutable collections\",\n recommended: true,\n },\n schema: [\n {\n type: \"object\",\n properties: {\n allowInTests: {\n type: \"boolean\",\n default: true,\n },\n allowMutable: {\n type: \"boolean\",\n default: true,\n },\n },\n additionalProperties: false,\n },\n ],\n messages: {\n preferFunctypeSet: \"Prefer functype Set<{{type}}> over native Set\",\n preferFunctypeSetLiteral: \"Prefer Set.of(...) or Set.empty() over new Set()\",\n suggestSetEmpty: \"Replace with {{name}}.empty()\",\n suggestSetOf: \"Replace with {{name}}.of(...)\",\n suggestSetFrom: \"Replace with {{name}}(...)\",\n suggestUseLocalName: \"Replace with {{name}}\",\n suggestAddImport: \"Add {{symbol}} import from functype\",\n },\n },\n\n create(context) {\n const options = context.options[0] || {}\n const allowInTests = options.allowInTests !== false\n // A native Set the code mutates on purpose (cache, registry, accumulator) is a deliberate choice —\n // functype's Set is immutable and cannot express it (#324).\n const allowMutable = options.allowMutable !== false\n\n function isInTestFile() {\n const filename = context.filename\n return (\n /\\.(test|spec)\\.(ts|js|tsx|jsx)$/.test(filename) ||\n filename.includes(\"__tests__\") ||\n filename.includes(\"/test/\") ||\n filename.includes(\"/tests/\")\n )\n }\n\n const functypeImports = getFunctypeImportsLegacy(context)\n\n return {\n NewExpression(node: ASTNode) {\n if (allowInTests && isInTestFile()) return\n\n // Only handle `new Set(...)` calls\n if (!node.callee || node.callee.type !== \"Identifier\" || node.callee.name !== \"Set\") return\n\n if (allowMutable && isMutatedCollection(node, SET_MUTATORS, context.sourceCode)) return\n\n // `Set` may be functype's import (under any name), a local shadow, or the native built-in (#350).\n if (bindingOf(node.callee, \"Set\", context.sourceCode) !== \"native\") return\n\n // A native Set flowing into a declared `ReadonlySet` contract is what that contract requires (#350).\n if (flowsIntoReadonlyContract(node, \"ReadonlySet\")) return\n\n // Skip if already in a functype context\n if (isAlreadyUsingFunctype(node, functypeImports)) return\n\n const sourceCode = context.sourceCode\n const imported = functypeLocalName(sourceCode, \"Set\")\n const name = imported ?? \"Set\"\n const args = node.arguments || []\n const suggestions: Rule.SuggestionReportDescriptor[] = []\n\n if (args.length === 0) {\n // new Set() → Set.empty()\n suggestions.push({\n messageId: \"suggestSetEmpty\",\n data: { name },\n fix(fixer) {\n return fixer.replaceText(node, `${name}.empty()`)\n },\n })\n } else if (args.length === 1 && args[0].type === \"ArrayExpression\") {\n // new Set([\"a\", \"b\", \"c\"]) → Set.of(\"a\", \"b\", \"c\")\n const arrayNode = args[0]\n const elements = arrayNode.elements || []\n const elementTexts = elements.map((el: ASTNode) => sourceCode.getText(el))\n const argsText = elementTexts.join(\", \")\n suggestions.push({\n messageId: \"suggestSetOf\",\n data: { name },\n fix(fixer) {\n return fixer.replaceText(node, `${name}.of(${argsText})`)\n },\n })\n } else if (args.length === 1) {\n // new Set(someVar) → Set(someVar)\n const argText = sourceCode.getText(args[0])\n suggestions.push({\n messageId: \"suggestSetFrom\",\n data: { name },\n fix(fixer) {\n return fixer.replaceText(node, `${name}(${argText})`)\n },\n })\n } else {\n // Fallback for unexpected cases\n suggestions.push({\n messageId: \"suggestSetEmpty\",\n data: { name },\n fix(fixer) {\n return fixer.replaceText(node, `${name}.empty()`)\n },\n })\n }\n\n if (imported === null) {\n suggestions.push({\n messageId: \"suggestAddImport\",\n data: { symbol: \"Set\" },\n fix: createImportFixer(sourceCode, \"Set\"),\n })\n }\n\n context.report({\n node,\n messageId: \"preferFunctypeSetLiteral\",\n suggest: suggestions,\n })\n },\n\n TSTypeReference(node: ASTNode) {\n if (allowInTests && isInTestFile()) return\n\n if (!node.typeName) return\n\n const sourceCode = context.sourceCode\n const typeName = node.typeName.type === \"Identifier\" ? node.typeName.name : sourceCode.getText(node.typeName)\n\n if (typeName !== \"Set\") return\n\n if (allowMutable && annotatesMutatedBinding(node, SET_MUTATORS, context.sourceCode)) return\n\n if (node.typeName.type === \"Identifier\" && bindingOf(node.typeName, \"Set\", context.sourceCode) !== \"native\")\n return\n\n // Extract type parameter if present (typeArguments for newer TS-ESLint, typeParameters for older)\n const typeParamNode = node.typeParameters?.params?.[0] ?? node.typeArguments?.params?.[0]\n const typeParam = typeParamNode ? sourceCode.getText(typeParamNode) : \"T\"\n\n // Already imported under another name (`Set as FSet`): point at that name instead of adding a\n // second import that would shadow the native `Set` the rest of the file uses.\n const imported = functypeLocalName(sourceCode, \"Set\")\n const suggestions: Rule.SuggestionReportDescriptor[] =\n imported === null\n ? [{ messageId: \"suggestAddImport\", data: { symbol: \"Set\" }, fix: createImportFixer(sourceCode, \"Set\") }]\n : [\n {\n messageId: \"suggestUseLocalName\",\n data: { name: imported },\n fix: (fixer) => fixer.replaceText(node.typeName, imported),\n },\n ]\n\n context.report({\n node,\n messageId: \"preferFunctypeSet\",\n data: { type: typeParam },\n suggest: suggestions,\n })\n },\n }\n },\n}\n\nexport default rule\n"],"mappings":"mbASA,MAAMA,EAAwB,CAC5B,KAAM,CACJ,KAAM,aACN,eAAgB,GAChB,KAAM,CACJ,YAAa,mEACb,YAAa,EACf,EACA,OAAQ,CACN,CACE,KAAM,SACN,WAAY,CACV,aAAc,CACZ,KAAM,UACN,QAAS,EACX,EACA,aAAc,CACZ,KAAM,UACN,QAAS,EACX,CACF,EACA,qBAAsB,EACxB,CACF,EACA,SAAU,CACR,kBAAmB,gDACnB,yBAA0B,mDAC1B,gBAAiB,gCACjB,aAAc,gCACd,eAAgB,6BAChB,oBAAqB,wBACrB,iBAAkB,qCACpB,CACF,EAEA,OAAO,EAAS,CACd,IAAMC,EAAUC,EAAQ,QAAQ,IAAM,CAAC,EACjCC,EAAeF,EAAQ,eAAiB,GAGxCG,EAAeH,EAAQ,eAAiB,GAE9C,SAASI,GAAe,CACtB,IAAMC,EAA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,KAAM,CAAE,KAAM0B,CAAU,EACxB,QAASR,CACX,CAAC,CACH,CACF,CACF,CACF"}
@@ -0,0 +1,21 @@
1
+ import { ASTNode } from "../types/ast.js";
2
+ import { SourceCode } from "eslint";
3
+ //#region src/utils/collection-binding.d.ts
4
+ /**
5
+ * What `Map` / `Set` means at a use site (#350). Deciding from the file's import list was wrong both ways:
6
+ * `import { Map as FMap } from "functype"` silenced the whole file even though the bare `Map` there is still
7
+ * the native one. Resolving the identifier through scope asks the question that matters.
8
+ *
9
+ * - `functype` — it is functype's import (under any local name), so there is nothing to report;
10
+ * - `shadowed` — a local declaration or a non-functype import, so it isn't the native collection;
11
+ * - `native` — unresolved, or a global / lib binding with no declaration: the built-in.
12
+ */
13
+ export type CollectionBinding = "native" | "functype" | "shadowed";
14
+ export declare const bindingOf: (node: ASTNode, name: string, sourceCode: SourceCode) => CollectionBinding;
15
+ /**
16
+ * The name this file already uses for functype's `name`: `"FMap"` for `import { Map as FMap }`,
17
+ * `"F.Map"` for `import * as F`, or null when it isn't imported (so the fix must add the import).
18
+ */
19
+ export declare const functypeLocalName: (sourceCode: SourceCode, name: string) => string | null;
20
+ //#endregion
21
+ //# sourceMappingURL=collection-binding.d.ts.map
@@ -0,0 +1,2 @@
1
+ const e=e=>typeof e==`string`&&(e===`functype`||e.startsWith(`functype/`)),t=(e,t,n)=>{let r=e=>e===null?null:e.set.get(t)??r(e.upper);return r(n.getScope(e))},n=(n,r,i)=>{let a=t(n,r,i);if(a===null||a.defs.length===0)return`native`;let o=a.defs[0];return o.type===`ImportBinding`&&e(o.parent?.source?.value)?`functype`:`shadowed`},r=(t,n)=>t.ast.body.filter(t=>t.type===`ImportDeclaration`&&e(t.source?.value)).flatMap(e=>e.specifiers??[]).flatMap(e=>e.type===`ImportSpecifier`&&e.imported?.type===`Identifier`&&e.imported.name===n?[e.local.name]:e.type===`ImportNamespaceSpecifier`?[`${e.local.name}.${n}`]:[])[0]??null;export{n as bindingOf,r as functypeLocalName};
2
+ //# sourceMappingURL=collection-binding.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"collection-binding.js","names":["isFunctypeSource","value","resolve","lookup","scope","name","sourceCode","node","bindingOf","variable","def","functypeLocalName","n","decl","spec"],"sources":["../../src/utils/collection-binding.ts"],"sourcesContent":["import type { Scope, SourceCode } from \"eslint\"\n\nimport type { ASTNode } from \"../types/ast\"\n\n/**\n * What `Map` / `Set` means at a use site (#350). Deciding from the file's import list was wrong both ways:\n * `import { Map as FMap } from \"functype\"` silenced the whole file even though the bare `Map` there is still\n * the native one. Resolving the identifier through scope asks the question that matters.\n *\n * - `functype` — it is functype's import (under any local name), so there is nothing to report;\n * - `shadowed` — a local declaration or a non-functype import, so it isn't the native collection;\n * - `native` — unresolved, or a global / lib binding with no declaration: the built-in.\n */\nexport type CollectionBinding = \"native\" | \"functype\" | \"shadowed\"\n\nconst isFunctypeSource = (value: unknown): boolean =>\n typeof value === \"string\" && (value === \"functype\" || value.startsWith(\"functype/\"))\n\nconst resolve = (node: ASTNode, name: string, sourceCode: SourceCode): Scope.Variable | null => {\n const lookup = (scope: Scope.Scope | null): Scope.Variable | null =>\n scope === null ? null : (scope.set.get(name) ?? lookup(scope.upper))\n return lookup(sourceCode.getScope(node))\n}\n\nexport const bindingOf = (node: ASTNode, name: string, sourceCode: SourceCode): CollectionBinding => {\n const variable = resolve(node, name, sourceCode)\n if (variable === null || variable.defs.length === 0) return \"native\"\n const def = variable.defs[0] as unknown as { readonly type: string; readonly parent?: ASTNode }\n if (def.type === \"ImportBinding\") return isFunctypeSource(def.parent?.source?.value) ? \"functype\" : \"shadowed\"\n return \"shadowed\"\n}\n\n/**\n * The name this file already uses for functype's `name`: `\"FMap\"` for `import { Map as FMap }`,\n * `\"F.Map\"` for `import * as F`, or null when it isn't imported (so the fix must add the import).\n */\nexport const functypeLocalName = (sourceCode: SourceCode, name: string): string | null => {\n const names = (sourceCode.ast.body as ReadonlyArray<ASTNode>)\n .filter((n) => n.type === \"ImportDeclaration\" && isFunctypeSource(n.source?.value))\n .flatMap((decl) => (decl.specifiers ?? []) as ReadonlyArray<ASTNode>)\n .flatMap((spec): ReadonlyArray<string> => {\n if (spec.type === \"ImportSpecifier\" && spec.imported?.type === \"Identifier\" && spec.imported.name === name) {\n return [spec.local.name]\n }\n if (spec.type === \"ImportNamespaceSpecifier\") return [`${spec.local.name}.${name}`]\n return []\n })\n return names[0] ?? null\n}\n"],"mappings":"AAeA,MAAMA,EAAoB,GACxB,OAAOC,GAAU,WAAaA,IAAU,YAAcA,EAAM,WAAW,WAAW,GAE9EC,GAAW,EAAe,EAAc,IAAkD,CAC9F,IAAMC,EAAU,GACdC,IAAU,KAAO,KAAQA,EAAM,IAAI,IAAIC,CAAI,GAAKF,EAAOC,EAAM,KAAK,EACpE,OAAOD,EAAOG,EAAW,SAASC,CAAI,CAAC,CACzC,EAEaC,GAAa,EAAe,EAAc,IAA8C,CACnG,IAAMC,EAAWP,EAAQK,EAAMF,EAAMC,CAAU,EAC/C,GAAIG,IAAa,MAAQA,EAAS,KAAK,SAAW,EAAG,MAAO,SAC5D,IAAMC,EAAMD,EAAS,KAAK,GAE1B,OADIC,EAAI,OAAS,iBAAwBV,EAAiBU,EAAI,QAAQ,QAAQ,KAAK,EAAI,WAAa,UAEtG,EAMaC,GAAqB,EAAwB,IACzCL,EAAW,IAAI,KAC3B,OAAQ,GAAMM,EAAE,OAAS,qBAAuBZ,EAAiBY,EAAE,QAAQ,KAAK,CAAC,CAAC,CAClF,QAAS,GAAUC,EAAK,YAAc,CAAC,CAA4B,CAAC,CACpE,QAAS,GACJC,EAAK,OAAS,mBAAqBA,EAAK,UAAU,OAAS,cAAgBA,EAAK,SAAS,OAAST,EAC7F,CAACS,EAAK,MAAM,IAAI,EAErBA,EAAK,OAAS,2BAAmC,CAAC,GAAGA,EAAK,MAAM,KAAK,GAAGT,GAAM,EAC3E,CAAC,CAED,CAAC,CAAC,IAAM"}
@@ -0,0 +1,5 @@
1
+ import { ASTNode } from "../types/ast.js";
2
+ //#region src/utils/readonly-contract.d.ts
3
+ export declare const flowsIntoReadonlyContract: (node: ASTNode, readonlyName: string) => boolean;
4
+ //#endregion
5
+ //# sourceMappingURL=readonly-contract.d.ts.map
@@ -0,0 +1,2 @@
1
+ const e=/* @__PURE__ */ new Set([`FunctionDeclaration`,`FunctionExpression`,`ArrowFunctionExpression`]),t=(e,t)=>e?.type===`TSTypeReference`&&e.typeName?.type===`Identifier`&&e.typeName.name===t,n=e=>e.typeArguments?.params??e.typeParameters?.params??[],r=(e,i)=>t(e,i)||t(e,`Promise`)&&r(n(e)[0],i),i=e=>e?.typeAnnotation?.typeAnnotation,a=t=>t==null?void 0:e.has(t.type)?t:a(t.parent),o=(e,t)=>{let n=e.parent;if(!n)return!1;switch(n.type){case`ConditionalExpression`:return n.test!==e&&o(n,t);case`LogicalExpression`:return o(n,t);case`TSAsExpression`:case`TSSatisfiesExpression`:return r(n.typeAnnotation,t)||o(n,t);case`ReturnStatement`:return r(a(n)?.returnType?.typeAnnotation,t);case`ArrowFunctionExpression`:return n.body===e&&r(n.returnType?.typeAnnotation,t);case`VariableDeclarator`:return n.init===e&&r(i(n.id),t);case`PropertyDefinition`:return n.value===e&&r(i(n),t);case`AssignmentPattern`:return n.right===e&&r(i(n.left),t);default:return!1}};export{o as flowsIntoReadonlyContract};
2
+ //# sourceMappingURL=readonly-contract.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"readonly-contract.js","names":["FUNCTIONS","isNamed","type","name","typeArgs","isReadonlyContract","readonlyName","annotationOf","node","nearestFunction","flowsIntoReadonlyContract","parent"],"sources":["../../src/utils/readonly-contract.ts"],"sourcesContent":["import type { ASTNode } from \"../types/ast\"\n\n/**\n * Does this expression flow straight into a declared native read-only contract (#350)?\n *\n * `prefer-functype-map` / `-set` never report a `ReadonlyMap` / `ReadonlySet` annotation, so they must not\n * report the constructor that annotation requires: when a function is declared to return\n * `ReadonlyMap<K, V>` — typically because a callee's interface takes one — the native `new Map(...)` it\n * returns is the contract, not an oversight. Recognized positions:\n *\n * - `return` in the nearest enclosing function whose declared return type is `R` or `Promise<R>`\n * (a callback nested inside such a function does not inherit its signature);\n * - the expression body of an arrow function with that return type;\n * - the initializer of a variable, class field, or parameter default annotated `R`;\n * - an `as R` / `satisfies R` assertion;\n * and through ternary branches and `??` / `||` / `&&` operands on the way there.\n */\n\nconst FUNCTIONS: ReadonlySet<string> = new Set([\"FunctionDeclaration\", \"FunctionExpression\", \"ArrowFunctionExpression\"])\n\nconst isNamed = (type: ASTNode | undefined, name: string): boolean =>\n type?.type === \"TSTypeReference\" && type.typeName?.type === \"Identifier\" && type.typeName.name === name\n\nconst typeArgs = (type: ASTNode): ReadonlyArray<ASTNode> =>\n (type.typeArguments?.params ?? type.typeParameters?.params ?? []) as ReadonlyArray<ASTNode>\n\n/** `R<…>` or `Promise<R<…>>`. */\nconst isReadonlyContract = (type: ASTNode | undefined, readonlyName: string): boolean =>\n isNamed(type, readonlyName) || (isNamed(type, \"Promise\") && isReadonlyContract(typeArgs(type!)[0], readonlyName))\n\nconst annotationOf = (node: ASTNode | undefined): ASTNode | undefined => node?.typeAnnotation?.typeAnnotation\n\nconst nearestFunction = (node: ASTNode | undefined): ASTNode | undefined =>\n node === undefined || node === null ? undefined : FUNCTIONS.has(node.type) ? node : nearestFunction(node.parent)\n\nexport const flowsIntoReadonlyContract = (node: ASTNode, readonlyName: string): boolean => {\n const parent = node.parent\n if (!parent) return false\n switch (parent.type) {\n case \"ConditionalExpression\":\n return parent.test !== node && flowsIntoReadonlyContract(parent, readonlyName)\n case \"LogicalExpression\":\n return flowsIntoReadonlyContract(parent, readonlyName)\n case \"TSAsExpression\":\n case \"TSSatisfiesExpression\":\n return isReadonlyContract(parent.typeAnnotation, readonlyName) || flowsIntoReadonlyContract(parent, readonlyName)\n case \"ReturnStatement\":\n return isReadonlyContract(nearestFunction(parent)?.returnType?.typeAnnotation, readonlyName)\n case \"ArrowFunctionExpression\":\n return parent.body === node && isReadonlyContract(parent.returnType?.typeAnnotation, readonlyName)\n case \"VariableDeclarator\":\n return parent.init === node && isReadonlyContract(annotationOf(parent.id), readonlyName)\n case \"PropertyDefinition\":\n return parent.value === node && isReadonlyContract(annotationOf(parent), readonlyName)\n case \"AssignmentPattern\":\n return parent.right === node && isReadonlyContract(annotationOf(parent.left), readonlyName)\n default:\n return false\n }\n}\n"],"mappings":"AAkBA,MAAMA,kBAAiC,IAAI,IAAI,CAAC,sBAAuB,qBAAsB,yBAAyB,CAAC,EAEjHC,GAAW,EAA2B,IAC1CC,GAAM,OAAS,mBAAqBA,EAAK,UAAU,OAAS,cAAgBA,EAAK,SAAS,OAASC,EAE/FC,EAAY,GACfF,EAAK,eAAe,QAAUA,EAAK,gBAAgB,QAAU,CAAC,EAG3DG,GAAsB,EAA2B,IACrDJ,EAAQC,EAAMI,CAAY,GAAML,EAAQC,EAAM,SAAS,GAAKG,EAAmBD,EAASF,CAAK,CAAC,CAAC,GAAII,CAAY,EAE3GC,EAAgB,GAAmDC,GAAM,gBAAgB,eAEzFC,EAAmB,GACvBD,GAA+B,KAAO,IAAA,GAAYR,EAAU,IAAIQ,EAAK,IAAI,EAAIA,EAAOC,EAAgBD,EAAK,MAAM,EAEpGE,GAA6B,EAAe,IAAkC,CACzF,IAAMC,EAASH,EAAK,OACpB,GAAI,CAACG,EAAQ,MAAO,GACpB,OAAQA,EAAO,KAAf,CACE,IAAK,wBACH,OAAOA,EAAO,OAASH,GAAQE,EAA0BC,EAAQL,CAAY,EAC/E,IAAK,oBACH,OAAOI,EAA0BC,EAAQL,CAAY,EACvD,IAAK,iBACL,IAAK,wBACH,OAAOD,EAAmBM,EAAO,eAAgBL,CAAY,GAAKI,EAA0BC,EAAQL,CAAY,EAClH,IAAK,kBACH,OAAOD,EAAmBI,EAAgBE,CAAM,CAAC,EAAE,YAAY,eAAgBL,CAAY,EAC7F,IAAK,0BACH,OAAOK,EAAO,OAASH,GAAQH,EAAmBM,EAAO,YAAY,eAAgBL,CAAY,EACnG,IAAK,qBACH,OAAOK,EAAO,OAASH,GAAQH,EAAmBE,EAAaI,EAAO,EAAE,EAAGL,CAAY,EACzF,IAAK,qBACH,OAAOK,EAAO,QAAUH,GAAQH,EAAmBE,EAAaI,CAAM,EAAGL,CAAY,EACvF,IAAK,oBACH,OAAOK,EAAO,QAAUH,GAAQH,EAAmBE,EAAaI,EAAO,IAAI,EAAGL,CAAY,EAC5F,QACE,MAAO,EACX,CACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-functype",
3
- "version": "2.111.0",
3
+ "version": "2.111.1",
4
4
  "description": "Custom ESLint rules for functional TypeScript programming with functype library patterns including Do notation (ESLint 10+)",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -42,7 +42,7 @@
42
42
  "@types/node": "^24.19.0",
43
43
  "@typescript-eslint/rule-tester": "^8.71.0",
44
44
  "eslint-config-prettier": "^10.1.8",
45
- "functype": "^1.11.0",
45
+ "functype": "^1.11.1",
46
46
  "ts-builds": "^3.4.3",
47
47
  "tsdown": "^0.23.0"
48
48
  },