Skip to content

Latest commit

 

History

History
144 lines (106 loc) · 3.89 KB

use-alias.md

File metadata and controls

144 lines (106 loc) · 3.89 KB

module-resolver/use-alias

Allows you to enforce that aliased modules are not using relative paths.

Rule Details

Given the babel configuration below

...
  "plugins": [
    [
      "module-resolver",
      {
        "root": ["."],
        "alias": {
          "action": "./actions"
        }
      }
    ]
  ]
...

The following patterns are considered warnings:

import fetchData from '../../actions/fetchData'
const fetchData = require('../../actions/fetchData')
const fetchData = await import('../../actions/fetchData')

The following patterns are not considered warnings:

import fetchData from 'actions/fetchData'
const fetchData = require('actions/fetchData')
const fetchData = await import('actions/fetchData')

Rule Options

...
"module-resolver/use-alias": [<enabled>, {
  "ignoreDepth": <number>,
  "projectRoot": <string>,
  "extensions": <array>,
  "chainedExtensions": <array>,
  "allowDepthMoreOrLessThanEquality": <boolean>,
  "alias": <object>
}]
...

ignoreDepth

Number representing a depth that can be ignored for aliased imports. Performs strict equality by default. If you want to allow depth of exact or lesser number, check allowDepthMoreOrLessThanEquality. By default this option is unused.

With the below ignoreDepth set, all of the above patterns causing warnings would no longer. The other cases would continue being valid as well.

"module-resolver/use-alias": ["error", {
  "ignoreDepth": 2
}]

allowDepthMoreOrLessThanEquality

Boolean allowDepthMoreOrLessThanEquality sets the equality rule for ignoreDepth. By default this option is unused (strict equality is performed).

With the below allowDepthMoreOrLessThanEquality set to true, all the pattern with depth equal or less than the ignoreDepth will be allowed.

"module-resolver/use-alias": ["error", {
  "allowDepthMoreOrLessThanEquality": false
}]

projectRoot

String representing a path from your workspace directory to your projectRoot directory where the .babelrc and .eslintrc files exist. By default this option is unused.

This is useful in a monorepo or projects that have these files nested in a subdirectory (e.g. <rootDir>/package/project/.eslintrc).

"module-resolver/use-alias": ["error", {
  "projectRoot": "/package/project"
}]

extensions

Array of additional extensions to look for when linting. By default, files without extensions are considered to be .js. This expands the resolution for extensionless imports. Possible values are .ts, .tsx, and .jsx.

With the below extensions array, TypeScript files will also be resolved.

"module-resolver/use-alias": ["error", {
  "extensions": [".ts"]
}]

chainedExtensions

Array of extensions to check for chaining. For example, React projects use .jss.js files to generate CSS styles for components. By default, files with extensions are considered to have only those extensions, while files without default to .js (and anything included in extensions as described above. This expands the resolution for imports with chained extensions. Any string value is supported.

With the below chainedExtensions array, JSS files will also be resolved.

"module-resolver/use-alias": ["error", {
  "chainedExtensions": [".jss"]
}]

alias

Object that represents projects alias mappings. This is used to pass the alias directly in and prevent searching for it within a .babelrc file. By default, this is unused and the alias is looked for within a .babelrc file. See #172 for motivation.

The alias object below will be used even if the project has a .babelrc file.

"module-resolver/use-alias": ["error", {
  "alias": {
    "root": ["."],
    "alias": {
      "#foo": "./src/foo",
      "#bar": "./bar",
    }
  }
}]