UNPKG

eslint-plugin-allowed-dependencies

Version:
1 lines 2.15 kB
import{ESLintUtils as e}from"@typescript-eslint/utils";import*as t from"ramda";import{readFileSync as n}from"node:fs";import{join as r}from"node:path";const i=t.startsWith(`@`),a=e=>t.flow(e,[t.split(`/`),t.take(i(e)?2:1),t.join(`/`)]),o=e=>JSON.parse(n(r(e,`package.json`),`utf8`)),s=e=>{let[n,r]=t.partition(t=>e.peerDependenciesMeta?.[t]?.optional===!0,Object.keys(e.peerDependencies||{}));return{requiredPeers:r,optionalPeers:n}},c=(e,n)=>t.map(e,n),l={type:`object`,properties:{packageDir:{type:`string`},typeOnly:{type:`array`,items:{type:`string`}},ignore:{type:`array`,items:{type:`string`,format:`regex`}},...t.fromPairs(t.xprod([`production`,`optionalPeers`,`requiredPeers`,`development`],[{oneOf:[{type:`boolean`},{type:`string`,enum:[`typeOnly`]}]}]))},additionalProperties:!1},u={prohibited:`Importing {{name}} is not allowed.`,typeOnly:`Only 'import type' syntax is allowed for {{name}}.`},d={production:!0,requiredPeers:!0,development:`typeOnly`,optionalPeers:`typeOnly`},f=[!0,!1,`typeOnly`],p=e=>({packageDir:n=e.cwd,typeOnly:r=[],ignore:i=[`^\\.`,`^node:`],production:a=d.production,development:l=d.development,requiredPeers:u=d.requiredPeers,optionalPeers:p=d.optionalPeers})=>{let m=o(n),h={production:Object.keys(m.dependencies||{}),development:Object.keys(m.devDependencies||{}),...s(m)},g={production:a,development:l,requiredPeers:u,optionalPeers:p},[_,v,y]=c(e=>t.flatten(t.values(t.mapObjIndexed((t,n)=>t===e?h[n]:[],g))),f);return y.push(...r),{allowed:_,prohibited:v,limited:y,ignore:i}},m=[`allowed`,`prohibited`,`limited`,`ignore`],h=e=>{let n=t.map(t.constructN(1,RegExp),e),r=t.invoker(1,`test`);return e=>t.any(r(e),n)};var g={rules:{dependencies:e.RuleCreator.withoutDocs({meta:{messages:u,type:`problem`,schema:{type:`array`,items:l}},defaultOptions:[...[d]],create:e=>{let n=p(e),r=t.map(n,e.options.length?e.options:[{}]),[i,o,s,l]=c(e=>t.flatten(t.pluck(e,r)),m),u=h(l);return{ImportDeclaration:({source:t,importKind:n})=>{if(u(t.value))return;let r=a(t.value);i.includes(r)||n===`type`&&!o.includes(r)||e.report({node:t,data:{name:r},messageId:s.includes(r)?`typeOnly`:`prohibited`})}}}})}};export{g as default};