An eslint plugin that converts any absolute import paths to relative ones if a file is imported from within the path/baseURL.
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";
// ## Import with same baseURL
// From within a file
// src/Foo/Foo.js
// we want to import
// src/Bar/Bar.js
// OK
import Bar from "../Bar/Bar"; // valid relative path
// NOT OK
import Something from "Bar/Bar";
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
.
baseUrl
and paths
defined in a single tsconfig.json
in the repository root.
- 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.
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',
'.d.ts',
'.ts',
'.jsx',
'.tsx',
'.scss',
'.css',
'.svg',
'.json',
]
Based on demand, this can be made configurable.
See the contributing guide for broad instructions on how to get started with this project.
To install, run
yarn add -D eslint-plugin-relative-imports
or respectively
npm install --save-dev eslint-plugin-relative-imports
Then update your eslint config by adding relative-imports
to the list of plugins,
and turn on the main rule require-relative-imports
of this plugin.
// .eslintrc.js
plugins: [
// other plugins ..
"relative-imports",
],
rules: {
// other rules ..
"relative-imports/require-relative-imports": "error",
}
Due to the use of embedding compiled TypeScript in a game engine. (see https://roblox-ts.com for more info), it is more efficient to use relative paths. It also is the only option when using some isolated containers. Therefore, a lint was required to force the imports to be relative where possible (under the baseURL).
- 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
Thanks to the https://github.com/spicattutti/eslint-plugin-relative-imports-when-same-folder - the original project this was forked from.