Check the import paths of modules to ensure project guidelines.
You'll first need to install ESLint:
$ npm i eslint --save-dev
Next, install eslint-plugin-test-import-paths
:
$ npm install eslint-plugin-test-import-paths --save-dev
Note: If you installed ESLint globally (using the -g
flag) then you must also install eslint-plugin-test-import-paths
globally.
Add test-import-paths
to the plugins section of your .eslintrc
configuration file and enable the test-import-paths
rule. You can omit the eslint-plugin-
prefix:
{
"plugins": [
"test-import-paths"
],
"rules": {
"test-import-paths/test-import-paths": "error"
}
}
By default, the rule is configured to only allow the following kinds of imports:
- installed node-modules (from version 1.0.1 onwards these are not tested for validity anymore! Use a plugin like
eslint-plugin-import
for that.) - sibling files with the the same name as the importing file in lower-camelCase, with the extensions
css
andscss
(configurable) - files from a subdirectory with the same name as the importing file in lower-camelCase, but only one level.
- files from a shared root folder called
~
(configurable) (since version 1.0.1 there won't be an error if these are not configured properly, as they will be handled like a node-module. This rule will simply test that only imports from the top level of these directories are made).
Four import syntaxes are supported, including dynamic imports:
import sth from "somewhere"
import "something"
require("something")
import("something")
Examples for default configuration:
// MyFile.js
// valid
import "./myFile.css"
import "./myFile.scss"
// invalid
import "./myOtherFile"
import "./myOtherFile.css"
import "./myFile.txt"
import "./myFile"
The allowed extensions can be configured for the rule via the validSiblingExtensions
key:
{
"rules": {
"test-import-paths/test-import-paths": ["error", {
"validSiblingExtensions": ["scss", "css"]
}]
}
}
// MyFile.js
// valid
import "./myFile/MyOtherFile.js"
import "./myFile/mySecondFile.txt"
// invalid
import "./myDir/SomeFile.js"
import "./myFile/dir/AFile.js"
Examples for default configuration:
// valid
import "~/util.js"
// invalid
import "/util.js"
import "~/handlers/click_event.js"
The root folder resolvers can be configured with the sharedFilesRootPrefixes
key:
{
"rules": {
"test-import-paths": ["error", {
"sharedFilesRootPrefixes": ["~"]
}]
}
}