Skip to content

Commit 38db3da

Browse files
authored
Merge pull request #22 from qdanik/release/2.1.0
Release/2.1.0
2 parents 6f4ac7e + 5285a5d commit 38db3da

33 files changed

Lines changed: 1893 additions & 660 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
.idea
33

44
node_modules
5+
coverage
56

67
lib
78
lib/*

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
# eslint-plugin-path [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
1+
# eslint-plugin-path
2+
[![Biome](https://img.shields.io/badge/linting-biome-60a5fa?style=flat-square)](https://biomejs.dev)
3+
[![code style: biome](https://img.shields.io/badge/code_style-biome-60a5fa?style=flat-square)](https://biomejs.dev)
24

35
An ESLint plugin for enforcing consistent imports across project. In other words, it helps to replace all relatives import with absolutes dependinng on settings.
46

@@ -87,6 +89,19 @@ If you are using custom paths in your `tsconfig.json` or `jsconfig.json` file, y
8789
}
8890
```
8991

92+
`path/no-absolute-imports` can also flag `compilerOptions.paths` aliases as absolute imports. This is opt-in via `useAliases`:
93+
94+
```json
95+
{
96+
"rules": {
97+
"path/no-absolute-imports": ["error", {
98+
"useAliases": true,
99+
"maxDepth": 1
100+
}]
101+
}
102+
}
103+
```
104+
90105
## Configuration
91106

92107
Enable the rules in your ESLint configuration file:

biome.json

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
{
2+
"$schema": "https://biomejs.dev/schemas/2.3.10/schema.json",
3+
"vcs": {
4+
"useIgnoreFile": true,
5+
"defaultBranch": "main",
6+
"enabled": true,
7+
"clientKind": "git"
8+
},
9+
"formatter": {
10+
"enabled": true,
11+
"formatWithErrors": false,
12+
"attributePosition": "auto",
13+
"indentStyle": "space",
14+
"indentWidth": 2,
15+
"lineWidth": 100,
16+
"lineEnding": "lf"
17+
},
18+
"javascript": {
19+
"formatter": {
20+
"jsxQuoteStyle": "single",
21+
"quoteStyle": "single"
22+
}
23+
},
24+
"linter": {
25+
"enabled": true,
26+
"rules": {
27+
"performance": {
28+
"noDelete": "off",
29+
"useTopLevelRegex": "error"
30+
},
31+
"nursery": {
32+
"noImportCycles": "error"
33+
},
34+
"complexity": {
35+
"useLiteralKeys": "off"
36+
},
37+
"correctness": {
38+
"noUnusedImports": "error",
39+
"noInvalidPositionAtImportRule": "off"
40+
},
41+
"suspicious": {
42+
"noImplicitAnyLet": "off",
43+
"noExplicitAny": "off",
44+
"noUnknownAtRules": "off"
45+
},
46+
"security": {
47+
"noBlankTarget": "error"
48+
},
49+
"recommended": true
50+
}
51+
},
52+
"assist": {
53+
"actions": {
54+
"source": {
55+
"organizeImports": {
56+
"level": "on",
57+
"options": {
58+
"identifierOrder": "natural"
59+
}
60+
}
61+
}
62+
}
63+
}
64+
}

docs/rules/no-absolute-imports.md

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
# path/no-absolute-imports
22

3-
Disallows the use of absolute file imports. This rule only affects paths included in tsconfig or jsconfig.
3+
Disallows absolute file imports and replaces them with relative paths.
4+
5+
Any import that resolves to a local file but is not a relative path (`./` or `../`) will be flagged. This includes:
6+
7+
- Rooted imports (`/components/button`)
8+
- tsconfig/jsconfig `paths` aliases (`@components/button`)
9+
- `baseUrl`-resolved imports (`presentation/presenters/auth`)
10+
11+
External packages (e.g. `react`, `lodash`) are not affected since they don't resolve to local files.
412

513
**Fixable:** This rule is automatically fixable using the `--fix` command line option.
614

7-
**Doesn't work with eslint-plugin-import/no-absolute-path:**
15+
**Conflicts with eslint-plugin-import/no-absolute-path:**
816
```
9-
"import/no-absolute-path": "off" // disable this rule
17+
"import/no-absolute-path": "off"
1018
```
1119

1220
## Example
@@ -16,33 +24,55 @@ These examples have the following project structure:
1624
```
1725
project
1826
└─── package.json
27+
└─── tsconfig.json
1928
└─── src
20-
└─── components
21-
└─── pages
29+
└─── components/
30+
└─── presentation/
31+
└─── presenters/
2232
```
2333

24-
`project/jsconfig.json` or `project/tsconfig.json`
34+
`project/tsconfig.json`:
2535

26-
```
36+
```json
2737
{
2838
"compilerOptions": {
29-
"baseUrl": "./src"
39+
"baseUrl": ".",
40+
"paths": {
41+
"@components/*": ["src/components/*"]
42+
}
3043
}
3144
}
3245
```
3346

34-
if `compilerOptions.baseUrl` is unset, it will use `project/` = the dirname of `package.json`
35-
3647
## Fail
3748

3849
```js
39-
// inside "project/src/pages/users/details/index.js"
50+
// Rooted import
4051
import foo from "/components/button";
52+
53+
// Alias import
54+
import foo from "@components/button";
55+
56+
// baseUrl import
57+
import foo from "presentation/presenters/auth";
4158
```
4259

4360
## Pass
4461

4562
```js
46-
// inside "project/src/components/common/input/index.js"
63+
// Relative import
4764
import foo from "../../components/button";
65+
66+
// External package — not a local file
67+
import React from "react";
68+
```
69+
70+
## Configuration
71+
72+
```json
73+
{
74+
"rules": {
75+
"path/no-absolute-imports": "error"
76+
}
77+
}
4878
```

jest.config.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/** @type {import('jest').Config} */
2+
module.exports = {
3+
testMatch: ['<rootDir>/tests/**/*.test.js'],
4+
collectCoverageFrom: ['lib/**/*.js', '!lib/**/*.d.ts'],
5+
coverageDirectory: 'coverage',
6+
coverageReporters: ['text', 'lcov'],
7+
coverageThreshold: {
8+
global: {
9+
branches: 85,
10+
functions: 100,
11+
lines: 100,
12+
statements: 100,
13+
},
14+
},
15+
};

0 commit comments

Comments
 (0)