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

eslint-plugin-import-alias

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eslint-plugin-import-alias

ESLint rule for restricting imports to path aliases

  • 1.2.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
14K
decreased by-5.79%
Maintainers
1
Weekly downloads
 
Created
Source

eslint-plugin-import-alias

An ESLint rule for forcing import path aliases.

Install

npm install --save-dev eslint-plugin-import-alias

Usage

import { test } from '@src/test'; // valid
import { test } from './test'; // invalid
import { test } from '../test'; // invalid

// Optional relative depth can be specified.
import { test } from './test'; // valid, { relativeDepth: 0 }
import { test } from '../test'; // valid, { relativeDepth: 1 }
import { test } from '../../test'; // invalid, { relativeDepth: 1 }

Configure

{
  "rules": {
    "import-alias/import-alias": [
      "error",
      {
        "relativeDepth": 0,
        "aliases": [
          { "alias": "@src", "matcher": "^src" }, // src/modules/app/test -> @src/modules/app/test
          { "alias": "@test", "matcher": "^test\/unit" }, // test/unit/modules/app -> @test/modules/app
          { "alias": "@testRoot", "matcher": "^(test)\/e2e" } // test/e2e/modules/app -> @testRoot/e2e/modules/app
        ]
      }
    ]
  }
}

Aliases can be configured to fix the path and rewrite to an aliased path. Each alias has the alias text and a regex matcher that will match against the resolved path from the root directory of the eslint process (usually the project root). For example, if the resolved file path is in the 'src' folder (src/modules/app/test) then 'src' will be replaced with '@src'. Optionally, you can define a capture group to replace only the part within the capture group, but still match against the whole regex.

A 'rootDir' can be defined to resolve the file paths from. This defaults to process.cwd(). In a lot of cases, this is already the project root in most cases.

module.exports = {
  "rules": {
    "import-alias/import-alias": [
      "error",
      {
        "relativeDepth": 0,
        "rootDir": __dirname,
        "aliases": [
          { "alias": "@src", "matcher": "^src" } // src/modules/app/test -> @src/modules/app/test
        ]
      }
    ]
  }
};

FAQs

Package last updated on 18 Nov 2018

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