eslint-plugin-barrel-rules 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.ko.md +123 -0
- package/README.md +126 -0
- package/dist/index.cjs +131 -0
- package/dist/index.d.cts +10 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +94 -0
- package/package.json +48 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 RakHyeon-Sung
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.ko.md
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
# ๐ก๏ธ eslint-plugin-barrel-rules
|
|
2
|
+
|
|
3
|
+
# **Advanced Barrel Pattern Enforcement for JavaScript/TypeScript Projects**
|
|
4
|
+
|
|
5
|
+
<div align="center">
|
|
6
|
+
<img src="https://img.shields.io/badge/version-1.0.0-blue.svg" alt="Version"/>
|
|
7
|
+
<img src="https://img.shields.io/badge/License-MIT-yellow.svg" alt="License"/>
|
|
8
|
+
<img src="https://img.shields.io/badge/PRs-welcome-brightgreen.svg" alt="PRs Welcome"/>
|
|
9
|
+
</div>
|
|
10
|
+
|
|
11
|
+
<div align="center">
|
|
12
|
+
<img width="320" alt="Scry" src="https://github.com/user-attachments/assets/dc11d2d4-3896-4def-bf5f-e778086a3de8" />
|
|
13
|
+
</div>
|
|
14
|
+
|
|
15
|
+
Github: [https://github.com/racgoo/eslint-plugin-barrel-module](https://github.com/racgoo/eslint-plugin-barrel-module)
|
|
16
|
+
|
|
17
|
+
NPM: [https://github.com/racgoo/eslint-plugin-barrel-module](https://github.com/racgoo/eslint-plugin-barrel-module)
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
## ์๊ฐ
|
|
22
|
+
|
|
23
|
+
**eslint-plugin-barrel-rules**๋
|
|
24
|
+
JavaScript/TypeScript ํ๋ก์ ํธ์์ Barrel Pattern(๋ฐฐ๋ด ํจํด)์ ๊ฐ์ ํ๊ณ , ๋ชจ๋ ๊ฒฝ๊ณ์ ์บก์ํ๋ฅผ ๋ณด์ฅํ๋ ๊ณ ๊ธ ESLint ํ๋ฌ๊ทธ์ธ์
๋๋ค.
|
|
25
|
+
|
|
26
|
+
์ง์ ํ ๋๋ ํ ๋ฆฌ(์: `src/domains/*`)์ ๋ด๋ถ ๊ตฌํ์
|
|
27
|
+
์ค์ง ํด๋น ๋๋ ํ ๋ฆฌ์ **index(๋ฐฐ๋ด) ํ์ผ**์ ํตํด์๋ง ์ ๊ทผํ ์ ์๋๋ก ๊ฐ์ ํฉ๋๋ค.
|
|
28
|
+
๋ด๋ถ ํ์ผ์ ์ง์ importํ๋ ๊ฒ์ ์ฐจ๋จํ์ฌ
|
|
29
|
+
**๋ชจ๋ํ, ์ถ์ํ, ์ ์ง๋ณด์์ฑ, ํ์ฅ์ฑ**์ ๊ทน๋ํํฉ๋๋ค.
|
|
30
|
+
|
|
31
|
+
---
|
|
32
|
+
|
|
33
|
+
## ์ง์ ํ๊ฒฝ
|
|
34
|
+
|
|
35
|
+
- Node.js (ES2015+)
|
|
36
|
+
- ES Modules(ESM)
|
|
37
|
+
|
|
38
|
+
---
|
|
39
|
+
|
|
40
|
+
## ์ฃผ์ ๊ธฐ๋ฅ
|
|
41
|
+
|
|
42
|
+
- **Barrel Pattern ๊ฐ์ **
|
|
43
|
+
์ง์ ํ ๋๋ ํ ๋ฆฌ์ index(๋ฐฐ๋ด) ํ์ผ์ ํตํด์๋ง import๋ฅผ ํ์ฉ
|
|
44
|
+
(์: `import ... from "../domains/foo"`๋ ํ์ฉ,
|
|
45
|
+
`import ... from "../domains/foo/components/Bar"`๋ ์ฐจ๋จ)
|
|
46
|
+
|
|
47
|
+
- **๊ณ ์ฑ๋ฅ glob ๋งค์นญ**
|
|
48
|
+
`src/domains/*`์ฒ๋ผ glob ํจํด์ผ๋ก ์ฌ๋ฌ ๋๋ ํ ๋ฆฌ ์ง์ ๊ฐ๋ฅ
|
|
49
|
+
|
|
50
|
+
---
|
|
51
|
+
|
|
52
|
+
## ์ค์น
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
npm i eslint-plugin-barrel-rules --save-dev
|
|
56
|
+
# or
|
|
57
|
+
yarn add -D eslint-plugin-barrel-rules
|
|
58
|
+
# or
|
|
59
|
+
pnpm add -D eslint-plugin-barrel-rules
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
---
|
|
63
|
+
|
|
64
|
+
## ์ฌ์ฉ๋ฒ
|
|
65
|
+
|
|
66
|
+
```js
|
|
67
|
+
import { fileURLToPath } from "url";
|
|
68
|
+
import path from "path";
|
|
69
|
+
import { enforceBarrelPattern } from "eslint-plugin-barrel-rules";
|
|
70
|
+
|
|
71
|
+
//ESM์ __dirname์ ์ง์ํ์ง ์๊ธฐ์ ์ง์ ๊ตฌํํฉ๋๋ค.
|
|
72
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
73
|
+
const __dirname = path.dirname(__filename);
|
|
74
|
+
|
|
75
|
+
export default [
|
|
76
|
+
{
|
|
77
|
+
plugins: {
|
|
78
|
+
"barrel-rules": enforceBarrelPattern,
|
|
79
|
+
},
|
|
80
|
+
rules: {
|
|
81
|
+
"barrel-rules/enforce-barrel-pattern": [
|
|
82
|
+
"error",
|
|
83
|
+
{
|
|
84
|
+
//Barrel Pattern์ ๊ฐ์ ํ ๋๋ ํ ๋ฆฌ๋ฅผ ์ ์ํฉ๋๋ค(baseDir ๊ธฐ์ค์ผ๋ก ์๋๊ฒฝ๋ก)
|
|
85
|
+
paths: ["src/domains/*"],
|
|
86
|
+
//paths๋ค์ root๊ฒฝ๋ก๋ฅผ ์ง์ ํฉ๋๋ค.
|
|
87
|
+
baseDir: __dirname,
|
|
88
|
+
},
|
|
89
|
+
],
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
];
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
---
|
|
96
|
+
|
|
97
|
+
## ์์
|
|
98
|
+
|
|
99
|
+
```ts
|
|
100
|
+
// โ ๋ด๋ถ ํ์ผ์ ์ง์ importํ๋ฉด ์ฐจ๋จ๋ฉ๋๋ค.
|
|
101
|
+
import { Test } from "../domains/foo/components/Test";
|
|
102
|
+
|
|
103
|
+
// โ
๋ฐ๋์ ๋ฐฐ๋ด(index) ํ์ผ์ ํตํด importํด์ผ ํฉ๋๋ค.
|
|
104
|
+
import { Test } from "../domains/foo";
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
---
|
|
108
|
+
|
|
109
|
+
## ์์ผ๋ก์ ๊ณํ
|
|
110
|
+
|
|
111
|
+
- ๋ ๋ค์ํ ๋ชจ๋ ๊ฒฝ๊ณ/์ถ์ํ ๊ด๋ จ ๋ฃฐ ์ถ๊ฐ ์์
|
|
112
|
+
|
|
113
|
+
- **Alias/tsconfig ์ง์**
|
|
114
|
+
TypeScript `paths`, Vite `resolve.alias` ๋ฑ ๋ค์ํ ๊ฒฝ๋ก ๋งคํ ์๋ฒฝ ์ง์ ์์
|
|
115
|
+
|
|
116
|
+
- **Commonjs ์ง์**
|
|
117
|
+
|
|
118
|
+
---
|
|
119
|
+
|
|
120
|
+
## ๋ฌธ์
|
|
121
|
+
|
|
122
|
+
์ง๋ฌธ, ์ ์, ๋ฒ๊ทธ ๋ฆฌํฌํธ, ๊ธฐ์ฌ ๋ชจ๋ ํ์ํฉ๋๋ค!
|
|
123
|
+
[[๐ฌ send mail]](mailto:lhsung98@naver.com)
|
package/README.md
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
# ๐ก๏ธ eslint-plugin-barrel-rules
|
|
2
|
+
|
|
3
|
+
# **Advanced Barrel Pattern Enforcement for JavaScript/TypeScript Projects**
|
|
4
|
+
|
|
5
|
+
<div align="center">
|
|
6
|
+
<img src="https://img.shields.io/badge/version-1.0.0-blue.svg" alt="Version"/>
|
|
7
|
+
<img src="https://img.shields.io/badge/License-MIT-yellow.svg" alt="License"/>
|
|
8
|
+
<img src="https://img.shields.io/badge/PRs-welcome-brightgreen.svg" alt="PRs Welcome"/>
|
|
9
|
+
</div>
|
|
10
|
+
|
|
11
|
+
<div align="center">
|
|
12
|
+
<img width="320" alt="Scry" src="https://github.com/user-attachments/assets/dc11d2d4-3896-4def-bf5f-e778086a3de8" />
|
|
13
|
+
</div>
|
|
14
|
+
|
|
15
|
+
Github: [https://github.com/racgoo/eslint-plugin-barrel-module](https://github.com/racgoo/eslint-plugin-barrel-module)
|
|
16
|
+
|
|
17
|
+
NPM: [https://github.com/racgoo/eslint-plugin-barrel-module](https://github.com/racgoo/eslint-plugin-barrel-module)
|
|
18
|
+
|
|
19
|
+
๐ฐ๐ท [README (Korean)](./README.ko.md)
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
## Introduction
|
|
24
|
+
|
|
25
|
+
**eslint-plugin-barrel-rules** is an advanced ESLint plugin
|
|
26
|
+
that enforces the Barrel Pattern in JavaScript/TypeScript projects,
|
|
27
|
+
ensuring strict module boundaries and encapsulation.
|
|
28
|
+
|
|
29
|
+
You can specify directories (e.g., `src/domains/*`) where
|
|
30
|
+
internal implementation details must only be accessed via the directoryโs **index (barrel) file**.
|
|
31
|
+
Direct imports from internal files are blocked, maximizing
|
|
32
|
+
**modularity, abstraction, maintainability, and scalability**.
|
|
33
|
+
|
|
34
|
+
---
|
|
35
|
+
|
|
36
|
+
## Supports
|
|
37
|
+
|
|
38
|
+
- Node.js (ES2015+)
|
|
39
|
+
- Supports ES Modules (ESM)
|
|
40
|
+
|
|
41
|
+
---
|
|
42
|
+
|
|
43
|
+
## Features
|
|
44
|
+
|
|
45
|
+
- **Barrel Pattern Enforcement**
|
|
46
|
+
Only allows imports from the index (barrel) file of specified directories
|
|
47
|
+
(e.g., `import ... from "../domains/foo"` is allowed,
|
|
48
|
+
but `import ... from "../domains/foo/components/Bar"` is blocked)
|
|
49
|
+
|
|
50
|
+
- **High-performance glob matching**
|
|
51
|
+
Specify multiple directories using glob patterns like `src/domains/*`
|
|
52
|
+
|
|
53
|
+
---
|
|
54
|
+
|
|
55
|
+
## Install
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
npm i eslint-plugin-barrel-rules --save-dev
|
|
59
|
+
# or
|
|
60
|
+
yarn add -D eslint-plugin-barrel-rules
|
|
61
|
+
# or
|
|
62
|
+
pnpm add -D eslint-plugin-barrel-rules
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
---
|
|
66
|
+
|
|
67
|
+
## Usage
|
|
68
|
+
|
|
69
|
+
```js
|
|
70
|
+
import { fileURLToPath } from "url";
|
|
71
|
+
import path from "path";
|
|
72
|
+
import { enforceBarrelPattern } from "eslint-plugin-barrel-rules";
|
|
73
|
+
|
|
74
|
+
//ESM not support __dirname(custom __dirname)
|
|
75
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
76
|
+
const __dirname = path.dirname(__filename);
|
|
77
|
+
|
|
78
|
+
export default [
|
|
79
|
+
{
|
|
80
|
+
plugins: {
|
|
81
|
+
"barrel-rules": enforceBarrelPattern,
|
|
82
|
+
},
|
|
83
|
+
rules: {
|
|
84
|
+
"barrel-rules/enforce-barrel-pattern": [
|
|
85
|
+
"error",
|
|
86
|
+
{
|
|
87
|
+
//Enforced directories
|
|
88
|
+
paths: ["src/domains/*"],
|
|
89
|
+
//BaseDir(root path, mutable)
|
|
90
|
+
baseDir: __dirname,
|
|
91
|
+
},
|
|
92
|
+
],
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
];
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
---
|
|
99
|
+
|
|
100
|
+
## Example
|
|
101
|
+
|
|
102
|
+
```ts
|
|
103
|
+
// โ Direct import from internal file is blocked
|
|
104
|
+
import { Test } from "../domains/foo/components/Test";
|
|
105
|
+
|
|
106
|
+
// โ
Must import via the barrel (index) file
|
|
107
|
+
import { Test } from "../domains/foo";
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
---
|
|
111
|
+
|
|
112
|
+
## Future Work
|
|
113
|
+
|
|
114
|
+
- More rules for module boundaries and abstraction
|
|
115
|
+
|
|
116
|
+
- **Alias/tsconfig Support**
|
|
117
|
+
Fully supports TypeScript `paths`, Vite `resolve.alias`, and other custom path mappings
|
|
118
|
+
|
|
119
|
+
- **CJS Support**
|
|
120
|
+
|
|
121
|
+
---
|
|
122
|
+
|
|
123
|
+
## Contact
|
|
124
|
+
|
|
125
|
+
Questions, suggestions, bug reports, and contributions are welcome!
|
|
126
|
+
[[๐ฌ send mail]](mailto:lhsung98@naver.com)
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
enforceBarrelPattern: () => enforceBarrelPattern
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(index_exports);
|
|
36
|
+
|
|
37
|
+
// src/rules/enforce-barrel-pattern.ts
|
|
38
|
+
var import_utils = require("@typescript-eslint/utils");
|
|
39
|
+
var import_path = __toESM(require("path"), 1);
|
|
40
|
+
var import_resolve = __toESM(require("resolve"), 1);
|
|
41
|
+
var import_fast_glob = __toESM(require("fast-glob"), 1);
|
|
42
|
+
var enforceBarrelPattern = {
|
|
43
|
+
meta: {
|
|
44
|
+
type: "problem",
|
|
45
|
+
docs: {
|
|
46
|
+
description: "Only barrel pattern imports are allowed in this module."
|
|
47
|
+
},
|
|
48
|
+
schema: [
|
|
49
|
+
{
|
|
50
|
+
type: "object",
|
|
51
|
+
properties: {
|
|
52
|
+
paths: { type: "array", items: { type: "string" } },
|
|
53
|
+
baseDir: { type: "string" }
|
|
54
|
+
},
|
|
55
|
+
additionalProperties: false
|
|
56
|
+
}
|
|
57
|
+
],
|
|
58
|
+
messages: {
|
|
59
|
+
DirectImportDisallowed: "Please import from '{{matchedTargetPath}}'. Direct access to '{{rawImportPath}}' is not allowed. You must use the barrel pattern and only consume APIs exposed externally. This is to ensure encapsulation of internal logic and maintain module boundaries."
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
//default options(baseDir is current working directory. almost user execute eslint in project root)
|
|
63
|
+
defaultOptions: [{ paths: [], baseDir: process.cwd() }],
|
|
64
|
+
create(context) {
|
|
65
|
+
const options = context.options[0];
|
|
66
|
+
const baseDir = options.baseDir;
|
|
67
|
+
const targetPaths = options.paths.flatMap(
|
|
68
|
+
(_path) => import_fast_glob.default.sync(_path, {
|
|
69
|
+
cwd: baseDir,
|
|
70
|
+
onlyDirectories: true,
|
|
71
|
+
absolute: true
|
|
72
|
+
})
|
|
73
|
+
);
|
|
74
|
+
return {
|
|
75
|
+
//check only import declaration(ESM)
|
|
76
|
+
ImportDeclaration(node) {
|
|
77
|
+
const rawImportPath = node.source.value;
|
|
78
|
+
const absoluteCurrentFilePath = context.getFilename();
|
|
79
|
+
let absoluteImportPath = null;
|
|
80
|
+
try {
|
|
81
|
+
absoluteImportPath = import_resolve.default.sync(rawImportPath, {
|
|
82
|
+
basedir: import_path.default.dirname(absoluteCurrentFilePath),
|
|
83
|
+
extensions: [
|
|
84
|
+
".ts",
|
|
85
|
+
".tsx",
|
|
86
|
+
".js",
|
|
87
|
+
".jsx",
|
|
88
|
+
".json",
|
|
89
|
+
".d.ts",
|
|
90
|
+
".mjs",
|
|
91
|
+
".cjs"
|
|
92
|
+
]
|
|
93
|
+
});
|
|
94
|
+
} catch (e) {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
let matchedLatestTargetPath = null;
|
|
98
|
+
const invalidDirectedImport = targetPaths.some((targetPath) => {
|
|
99
|
+
const targetPathEntryPoints = [
|
|
100
|
+
"index.ts",
|
|
101
|
+
"index.tsx",
|
|
102
|
+
"index.js",
|
|
103
|
+
"index.jsx",
|
|
104
|
+
"index.cjs",
|
|
105
|
+
"index.mjs",
|
|
106
|
+
"index.d.ts"
|
|
107
|
+
].map((entry) => import_path.default.resolve(targetPath, entry));
|
|
108
|
+
const closedTargetPath = targetPath + "/";
|
|
109
|
+
const targetPathEntryPointed = targetPathEntryPoints.includes(absoluteImportPath);
|
|
110
|
+
const importedOutsideOfTargetPath = !absoluteCurrentFilePath.startsWith(closedTargetPath) && absoluteImportPath.startsWith(closedTargetPath);
|
|
111
|
+
const invalidImported = !targetPathEntryPointed && importedOutsideOfTargetPath;
|
|
112
|
+
if (invalidImported) {
|
|
113
|
+
matchedLatestTargetPath = targetPath;
|
|
114
|
+
}
|
|
115
|
+
return invalidImported;
|
|
116
|
+
});
|
|
117
|
+
if (invalidDirectedImport) {
|
|
118
|
+
context.report({
|
|
119
|
+
node,
|
|
120
|
+
messageId: "DirectImportDisallowed",
|
|
121
|
+
data: { rawImportPath, matchedTargetPath: matchedLatestTargetPath }
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
129
|
+
0 && (module.exports = {
|
|
130
|
+
enforceBarrelPattern
|
|
131
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { RuleModule } from '@typescript-eslint/utils/ts-eslint';
|
|
2
|
+
|
|
3
|
+
type Options = [{
|
|
4
|
+
paths: string[];
|
|
5
|
+
baseDir: string;
|
|
6
|
+
}];
|
|
7
|
+
type MessageIds = "DirectImportDisallowed";
|
|
8
|
+
declare const enforceBarrelPattern: RuleModule<MessageIds, Options>;
|
|
9
|
+
|
|
10
|
+
export { enforceBarrelPattern };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { RuleModule } from '@typescript-eslint/utils/ts-eslint';
|
|
2
|
+
|
|
3
|
+
type Options = [{
|
|
4
|
+
paths: string[];
|
|
5
|
+
baseDir: string;
|
|
6
|
+
}];
|
|
7
|
+
type MessageIds = "DirectImportDisallowed";
|
|
8
|
+
declare const enforceBarrelPattern: RuleModule<MessageIds, Options>;
|
|
9
|
+
|
|
10
|
+
export { enforceBarrelPattern };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
// src/rules/enforce-barrel-pattern.ts
|
|
2
|
+
import "@typescript-eslint/utils";
|
|
3
|
+
import path from "path";
|
|
4
|
+
import resolve from "resolve";
|
|
5
|
+
import fastGlob from "fast-glob";
|
|
6
|
+
var enforceBarrelPattern = {
|
|
7
|
+
meta: {
|
|
8
|
+
type: "problem",
|
|
9
|
+
docs: {
|
|
10
|
+
description: "Only barrel pattern imports are allowed in this module."
|
|
11
|
+
},
|
|
12
|
+
schema: [
|
|
13
|
+
{
|
|
14
|
+
type: "object",
|
|
15
|
+
properties: {
|
|
16
|
+
paths: { type: "array", items: { type: "string" } },
|
|
17
|
+
baseDir: { type: "string" }
|
|
18
|
+
},
|
|
19
|
+
additionalProperties: false
|
|
20
|
+
}
|
|
21
|
+
],
|
|
22
|
+
messages: {
|
|
23
|
+
DirectImportDisallowed: "Please import from '{{matchedTargetPath}}'. Direct access to '{{rawImportPath}}' is not allowed. You must use the barrel pattern and only consume APIs exposed externally. This is to ensure encapsulation of internal logic and maintain module boundaries."
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
//default options(baseDir is current working directory. almost user execute eslint in project root)
|
|
27
|
+
defaultOptions: [{ paths: [], baseDir: process.cwd() }],
|
|
28
|
+
create(context) {
|
|
29
|
+
const options = context.options[0];
|
|
30
|
+
const baseDir = options.baseDir;
|
|
31
|
+
const targetPaths = options.paths.flatMap(
|
|
32
|
+
(_path) => fastGlob.sync(_path, {
|
|
33
|
+
cwd: baseDir,
|
|
34
|
+
onlyDirectories: true,
|
|
35
|
+
absolute: true
|
|
36
|
+
})
|
|
37
|
+
);
|
|
38
|
+
return {
|
|
39
|
+
//check only import declaration(ESM)
|
|
40
|
+
ImportDeclaration(node) {
|
|
41
|
+
const rawImportPath = node.source.value;
|
|
42
|
+
const absoluteCurrentFilePath = context.getFilename();
|
|
43
|
+
let absoluteImportPath = null;
|
|
44
|
+
try {
|
|
45
|
+
absoluteImportPath = resolve.sync(rawImportPath, {
|
|
46
|
+
basedir: path.dirname(absoluteCurrentFilePath),
|
|
47
|
+
extensions: [
|
|
48
|
+
".ts",
|
|
49
|
+
".tsx",
|
|
50
|
+
".js",
|
|
51
|
+
".jsx",
|
|
52
|
+
".json",
|
|
53
|
+
".d.ts",
|
|
54
|
+
".mjs",
|
|
55
|
+
".cjs"
|
|
56
|
+
]
|
|
57
|
+
});
|
|
58
|
+
} catch (e) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
let matchedLatestTargetPath = null;
|
|
62
|
+
const invalidDirectedImport = targetPaths.some((targetPath) => {
|
|
63
|
+
const targetPathEntryPoints = [
|
|
64
|
+
"index.ts",
|
|
65
|
+
"index.tsx",
|
|
66
|
+
"index.js",
|
|
67
|
+
"index.jsx",
|
|
68
|
+
"index.cjs",
|
|
69
|
+
"index.mjs",
|
|
70
|
+
"index.d.ts"
|
|
71
|
+
].map((entry) => path.resolve(targetPath, entry));
|
|
72
|
+
const closedTargetPath = targetPath + "/";
|
|
73
|
+
const targetPathEntryPointed = targetPathEntryPoints.includes(absoluteImportPath);
|
|
74
|
+
const importedOutsideOfTargetPath = !absoluteCurrentFilePath.startsWith(closedTargetPath) && absoluteImportPath.startsWith(closedTargetPath);
|
|
75
|
+
const invalidImported = !targetPathEntryPointed && importedOutsideOfTargetPath;
|
|
76
|
+
if (invalidImported) {
|
|
77
|
+
matchedLatestTargetPath = targetPath;
|
|
78
|
+
}
|
|
79
|
+
return invalidImported;
|
|
80
|
+
});
|
|
81
|
+
if (invalidDirectedImport) {
|
|
82
|
+
context.report({
|
|
83
|
+
node,
|
|
84
|
+
messageId: "DirectImportDisallowed",
|
|
85
|
+
data: { rawImportPath, matchedTargetPath: matchedLatestTargetPath }
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
export {
|
|
93
|
+
enforceBarrelPattern
|
|
94
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "eslint-plugin-barrel-rules",
|
|
3
|
+
"description": "Enforce barrel module pattern",
|
|
4
|
+
"repository": {
|
|
5
|
+
"type": "git",
|
|
6
|
+
"url": "https://github.com/racgoo/eslint-plugin-barrel-module"
|
|
7
|
+
},
|
|
8
|
+
"bugs": {
|
|
9
|
+
"url": "https://github.com/racgoo/eslint-plugin-barrel-module/issues"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"dist",
|
|
13
|
+
"README.md",
|
|
14
|
+
"LICENSE"
|
|
15
|
+
],
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"keywords": [
|
|
18
|
+
"eslint",
|
|
19
|
+
"eslint-plugin",
|
|
20
|
+
"eslint-plugin-barrel-rules",
|
|
21
|
+
"barrel-module",
|
|
22
|
+
"barrel pattern",
|
|
23
|
+
"encapsulation directory",
|
|
24
|
+
"enforce barrel pattern"
|
|
25
|
+
],
|
|
26
|
+
"version": "1.0.0",
|
|
27
|
+
"type": "module",
|
|
28
|
+
"main": "dist/index.cjs",
|
|
29
|
+
"module": "dist/index.js",
|
|
30
|
+
"types": "dist/index.d.ts",
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@typescript-eslint/utils": "^8.36.0",
|
|
33
|
+
"fast-glob": "^3.3.3",
|
|
34
|
+
"resolve": "^1.22.10",
|
|
35
|
+
"tsconfig-paths": "^4.2.0"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@types/node": "^24.0.13",
|
|
39
|
+
"@types/resolve": "^1.20.6",
|
|
40
|
+
"tsup": "^8.5.0",
|
|
41
|
+
"typescript": "~5.8.3"
|
|
42
|
+
},
|
|
43
|
+
"scripts": {
|
|
44
|
+
"build": "tsup src/index.ts --dts --format cjs,esm",
|
|
45
|
+
"type-check": "tsc --noEmit",
|
|
46
|
+
"release": "pnpm run build && pnpm publish --access=public"
|
|
47
|
+
}
|
|
48
|
+
}
|