eslint-plugin-relative-imports-when-same-folder
v0.0.4
Published
An eslint plugin to convert any absolute import paths to relative ones if files are imported from within the same folder
Downloads
1,453
Maintainers
Readme
eslint-plugin-relative-imports-when-same-folder
An eslint plugin that converts any absolute import paths to relative ones if a file is imported from within the same directory.
Resolves absolute paths to paths on disk by parsing a tsconfig.json
expected to be found in the root of the repository
using this plugin.
// ## Import sibling
// From within a file
// src/Foo/Foo.js
// we want to import
// src/Foo/Baz.js
// OK
import Bar from "./Baz"; // valid relative path
// NOT OK
import Something from "src/Foo/Baz";
// ## Import descendant
// From within a file
// src/Foo/Foo.js
// we want to import
// src/Foo/Bar/Bar.js
// OK
import Bar from "./Bar/Bar"; // valid relative path
// NOT OK
import Something from "src/Foo/Bar/Bar";
Prerequisites / Limitations
Module resolution must be defined in tsconfig.json
A modern JS/TS-based project can not only define some sort of baseUrl like src
, it can also define import aliases using webpack`s resolve option or path mappings specified tsconfig.
As an example, ~
can be an alias for all code under <repository-root>/src
.
⚠️ This plugin only resolves module resolution configs baseUrl
and paths
defined in a single tsconfig.json
in the repository root. ⚠️
It only supports the common Node Module resolution strategy. There is no
support for
- rootDirs
- multiple tsconfigs that extend each other.
Contributions to make this more flexible or to retrieve module resolution mappings out of a webpack config are welcome.
As a workaround to this limitation, a project can re-use or re-declare import aliases that are defined in a webpack config in the project`s tsconfig.
Default set of file extensions used when resolving paths
When an alias is mapped to a single path, we assume that another mechanism (e.g. TypeScript or no-unresolved) already ensures that the de-aliased path is valid.
When an alias is mapped to more than one path, we check what de-aliased path can be resolved. Here we assume a default set of file extensions.
[
'.js',
'.ts',
'.jsx',
'.tsx',
'.scss',
'.css',
'.svg',
'.json',
]
Based on demand, this can be made configurable.
How to contribute
See the contributing guide for broad instructions on how to get started with this project.
Setup Instructions
To install, run
yarn add -D eslint-plugin-relative-imports-when-same-folder
respectively
npm install --save-dev eslint-plugin-relative-imports-when-same-folder
Then update your eslint config by adding relative-imports-when-same-folder
to the list of plugins,
and turn on the main rule no-relative-imports-when-same-folder
of this plugin.
// .eslintrc.js
plugins: [
// other plugins ..
"relative-imports-when-same-folder",
],
rules: {
// other rules ..
"relative-imports-when-same-folder/no-relative-imports-when-same-folder": "error",
}
Example Repo
Check out https://github.com/s-pic/fixmy.frontend/tree/use_eslint-plugin-relative-imports-when-same-folder. It is a not too small, real-world project.
How this was born
In a codebase, we wanted to have three kinds of import blocks:
- Third party modules (e.g.
lodash
) - Imports from outside the folder the current file is in (e.g.
src/utils/some_util
) - Imports from within the same file, as relative imports (
./some_sub_component
)
We utilized import/order for the ordering as well as eslint-plugin-no-relative-import-paths with allowSameFolder
) to only allow imports from within the same file. Using that setup we could enforce allmost all of the mentioned constraints we wanted to apply:
- imports where ordered in those mentioned blocks
- relative imports where only allowed from within the same folder
What was missing was automation to refactor all absolute imports that can be relative to actually be relative, so they are moved to the last block.
TODOs
- [ ] check eslint settings to tell clients that this is a plugin for ts-parser
- [ ] Support windows -> e.g. read and use platform specific segment separator
- [ ] Performance testing, see https://www.darraghoriordan.com/2021/11/06/how-to-write-an-eslint-plugin-typescript/
- [ ] migrate to typescript !
- [ ] Add proper unit test using
RuleTester
from typescript-eslint - [ ] Check for a lib that helps with dealing with globs instead of verbosely hand-rolling the string manipulation logic
- [ ] Solidify reverse mapping of path aliases with more tests, preferably using real world configs
- [ ] Try to find a lib to reverse-map tsconfig module resolution configs. This must have been solved somewhere else already. Eventually we can feed aliases to
enhanced-resolve
Acknowledgements
Thanks SMG for letting me work on company time.