Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

eslint-plugin-strict-dependencies

Package Overview
Dependencies
Maintainers
0
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eslint-plugin-strict-dependencies

ESlint plugin to define custom module dependency rules.

  • 1.3.12
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
20K
increased by9.09%
Maintainers
0
Weekly downloads
 
Created
Source

eslint-plugin-strict-dependencies

ESLint plugin to define custom module dependency rules.

NOTE: eslint-plugin-strict-dependencies uses tsconfig, tsconfig.json must be present.

Installation

npm install eslint-plugin-strict-dependencies --save-dev

Supported Rules

  • strict-dependencies
    • module: string (Glob or Forward matching string)
      • Target module path
    • targetMembers: string[]
      • Target member name
      • e.x. ["Suspense"] in import { Suspense } from 'react'
    • allowReferenceFrom: string[] (Glob or Forward matching string)
      • Paths of files where target module imports are allowed.
    • allowSameModule: boolean
      • Whether it can be imported by other files in the same directory
    • excludeTypeImportChecks: boolean
      • Whether to exclude type import checks
      • e.x. import type { Suspense } from 'react'

Options

  • resolveRelativeImport: boolean[default = false]

    • Whether to resolve relative import as in the following example
    • src/components/aaa.ts
      import bbb from './bbb';
      
      • resolveRelativeImport = false: Resolve as ./bbb (excluded from lint target)
      • resolveRelativeImport = true: Resolve as src/components/bbb: (included from lint target)
  • pathIndexMap: object[default = null]

    • In eslint-plugin-strict-dependencies, path alias resolution is performed based on the paths specified in the tsconfig.
    • By default, the value with an index number of 0 is used, but you can specify an option to use a value with any index number.
    • Specify it as in the following example:
      • tsconfig.json
        {
          "compilerOptions": {
              "*": ["aaa/*", "bbb/*"]
            },
        }
        
      • pathIndexMap = { "*": 1 } : "bbb/*" is used.

Usage

.eslintrc:

"plugins": [
  "strict-dependencies",
],
"rules": {
  "strict-dependencies/strict-dependencies": [
    "error",
    [
      /**
       * Example:
       * Limit the dependencies in the following directions
       * pages -> components/page -> components/ui
       */
      {
        "module": "src/components/page",
        "allowReferenceFrom": ["src/pages"],
        // components/page can't import other components/page
        "allowSameModule": false
      },
      {
        "module": "src/components/ui",
        "allowReferenceFrom": ["src/components/page"],
        // components/ui can import other components/ui
        "allowSameModule": true,
        // components/ui exclude type import checks
        "excludeTypeImportChecks": true
      },

      /**
       * example:
       * Disallow to import `next/router` directly. it should always be imported using `libs/router.ts`.
       */
      {
        "module": "next/router",
        "allowReferenceFrom": ["src/libs/router.ts"],
        "allowSameModule": false
      },

      /**
       * example:
       * Disallow to import Suspense from react. it should always be imported using `libs/react.ts`.
       */
        {
            "module": "react",
            "targetMembers": ["Suspense"],
            "allowReferenceFrom": ["src/libs/react.ts"],
            "allowSameModule": false
        },
    ],
    // options
    // {
    //   "resolveRelativeImport": true
    //   "pathIndexMap": { "*": 1 }
    // }
  ]
}

License

MIT

Keywords

FAQs

Package last updated on 15 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc