@yasainet/eslint 0.0.16 → 0.0.18
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/package.json +1 -1
- package/src/common/imports.mjs +26 -1
- package/src/common/index.mjs +5 -2
- package/src/common/local-plugins/import-path-style.mjs +99 -0
- package/src/common/local-plugins/index.mjs +10 -0
- package/src/common/naming.mjs +5 -58
- package/src/deno/index.mjs +3 -1
- package/src/next/imports.mjs +16 -0
- package/src/next/index.mjs +2 -0
- package/src/node/index.mjs +3 -1
package/package.json
CHANGED
package/src/common/imports.mjs
CHANGED
|
@@ -129,9 +129,34 @@ export const libBoundaryConfigs = [
|
|
|
129
129
|
];
|
|
130
130
|
|
|
131
131
|
/** @description Scope import restriction rules to the given feature root */
|
|
132
|
-
export function createImportsConfigs(
|
|
132
|
+
export function createImportsConfigs(
|
|
133
|
+
featureRoot,
|
|
134
|
+
prefixLibMapping,
|
|
135
|
+
{ banAliasImports = false } = {},
|
|
136
|
+
) {
|
|
133
137
|
const configs = [];
|
|
134
138
|
|
|
139
|
+
if (banAliasImports) {
|
|
140
|
+
configs.push({
|
|
141
|
+
name: "imports/ban-alias",
|
|
142
|
+
files: [`${featureRoot}/**/*.ts`],
|
|
143
|
+
rules: {
|
|
144
|
+
"no-restricted-imports": [
|
|
145
|
+
"error",
|
|
146
|
+
{
|
|
147
|
+
patterns: [
|
|
148
|
+
{
|
|
149
|
+
group: ["@/*", "@/**"],
|
|
150
|
+
message:
|
|
151
|
+
"Alias imports (@/) are not available in this environment. Use relative paths.",
|
|
152
|
+
},
|
|
153
|
+
],
|
|
154
|
+
},
|
|
155
|
+
],
|
|
156
|
+
},
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
|
|
135
160
|
configs.push(
|
|
136
161
|
makeConfig(
|
|
137
162
|
"repositories",
|
package/src/common/index.mjs
CHANGED
|
@@ -6,13 +6,16 @@ import { createNamingConfigs } from "./naming.mjs";
|
|
|
6
6
|
import { rulesConfigs } from "./rules.mjs";
|
|
7
7
|
|
|
8
8
|
/** @description Build common configs scoped to the given feature root */
|
|
9
|
-
export function createCommonConfigs(
|
|
9
|
+
export function createCommonConfigs(
|
|
10
|
+
featureRoot,
|
|
11
|
+
{ banAliasImports = false } = {},
|
|
12
|
+
) {
|
|
10
13
|
const prefixLibMapping = generatePrefixLibMapping(featureRoot);
|
|
11
14
|
return [
|
|
12
15
|
...rulesConfigs,
|
|
13
16
|
...createNamingConfigs(featureRoot, prefixLibMapping),
|
|
14
17
|
...createLayersConfigs(featureRoot),
|
|
15
|
-
...createImportsConfigs(featureRoot, prefixLibMapping),
|
|
18
|
+
...createImportsConfigs(featureRoot, prefixLibMapping, { banAliasImports }),
|
|
16
19
|
...createJsdocConfigs(featureRoot),
|
|
17
20
|
];
|
|
18
21
|
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @description Enforce import path style within features:
|
|
5
|
+
* - Same-feature imports must use relative paths
|
|
6
|
+
* - Cross-feature imports must use @/ alias
|
|
7
|
+
*/
|
|
8
|
+
export const importPathStyleRule = {
|
|
9
|
+
meta: {
|
|
10
|
+
type: "problem",
|
|
11
|
+
messages: {
|
|
12
|
+
useRelative:
|
|
13
|
+
"Same-feature import must use a relative path instead of '{{ importPath }}'.",
|
|
14
|
+
useAlias:
|
|
15
|
+
"Cross-feature import must use '@/' instead of relative path '{{ importPath }}'.",
|
|
16
|
+
},
|
|
17
|
+
schema: [
|
|
18
|
+
{
|
|
19
|
+
type: "object",
|
|
20
|
+
properties: {
|
|
21
|
+
featureRoot: { type: "string" },
|
|
22
|
+
},
|
|
23
|
+
required: ["featureRoot"],
|
|
24
|
+
additionalProperties: false,
|
|
25
|
+
},
|
|
26
|
+
],
|
|
27
|
+
},
|
|
28
|
+
create(context) {
|
|
29
|
+
const { featureRoot } = context.options[0];
|
|
30
|
+
const filename = context.filename;
|
|
31
|
+
|
|
32
|
+
const rootSep = featureRoot + "/";
|
|
33
|
+
const rootIdx = filename.indexOf(rootSep);
|
|
34
|
+
if (rootIdx === -1) return {};
|
|
35
|
+
|
|
36
|
+
const afterRoot = filename.slice(rootIdx + rootSep.length);
|
|
37
|
+
const featureName = afterRoot.split("/")[0];
|
|
38
|
+
if (!featureName) return {};
|
|
39
|
+
|
|
40
|
+
// Absolute path of the current feature directory
|
|
41
|
+
const featureDir =
|
|
42
|
+
filename.slice(0, rootIdx + rootSep.length) + featureName;
|
|
43
|
+
|
|
44
|
+
// Alias prefix for same-feature: @/features/{featureName}
|
|
45
|
+
const aliasBase = featureRoot.replace(/^src\//, "");
|
|
46
|
+
const sameFeaturePrefix = `@/${aliasBase}/${featureName}/`;
|
|
47
|
+
const sameFeatureExact = `@/${aliasBase}/${featureName}`;
|
|
48
|
+
|
|
49
|
+
function check(source) {
|
|
50
|
+
if (!source || typeof source.value !== "string") return;
|
|
51
|
+
const importPath = source.value;
|
|
52
|
+
|
|
53
|
+
// Case 1: @/features/{same-feature}/... → use relative path
|
|
54
|
+
if (
|
|
55
|
+
importPath.startsWith(sameFeaturePrefix) ||
|
|
56
|
+
importPath === sameFeatureExact
|
|
57
|
+
) {
|
|
58
|
+
context.report({
|
|
59
|
+
node: source,
|
|
60
|
+
messageId: "useRelative",
|
|
61
|
+
data: { importPath },
|
|
62
|
+
});
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Case 2: relative path that exits current feature → use @/
|
|
67
|
+
if (importPath.startsWith(".")) {
|
|
68
|
+
const resolved = path.resolve(path.dirname(filename), importPath);
|
|
69
|
+
if (
|
|
70
|
+
!resolved.startsWith(featureDir + "/") &&
|
|
71
|
+
resolved !== featureDir
|
|
72
|
+
) {
|
|
73
|
+
context.report({
|
|
74
|
+
node: source,
|
|
75
|
+
messageId: "useAlias",
|
|
76
|
+
data: { importPath },
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return {
|
|
83
|
+
ImportDeclaration(node) {
|
|
84
|
+
check(node.source);
|
|
85
|
+
},
|
|
86
|
+
ExportNamedDeclaration(node) {
|
|
87
|
+
if (node.source) check(node.source);
|
|
88
|
+
},
|
|
89
|
+
ExportAllDeclaration(node) {
|
|
90
|
+
check(node.source);
|
|
91
|
+
},
|
|
92
|
+
ImportExpression(node) {
|
|
93
|
+
if (node.source && node.source.type === "Literal") {
|
|
94
|
+
check(node.source);
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
};
|
|
98
|
+
},
|
|
99
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { actionHandleServiceRule } from "./action-handle-service.mjs";
|
|
2
|
+
import { importPathStyleRule } from "./import-path-style.mjs";
|
|
3
|
+
|
|
4
|
+
/** @description Shared local plugin object to avoid ESLint "Cannot redefine plugin" errors */
|
|
5
|
+
export const localPlugin = {
|
|
6
|
+
rules: {
|
|
7
|
+
"action-handle-service": actionHandleServiceRule,
|
|
8
|
+
"import-path-style": importPathStyleRule,
|
|
9
|
+
},
|
|
10
|
+
};
|
package/src/common/naming.mjs
CHANGED
|
@@ -1,22 +1,16 @@
|
|
|
1
1
|
import { featuresGlob } from "./constants.mjs";
|
|
2
|
+
import { localPlugin } from "./local-plugins/index.mjs";
|
|
2
3
|
import { checkFile } from "./plugins.mjs";
|
|
3
|
-
import { actionHandleServiceRule } from "./local-plugins/action-handle-service.mjs";
|
|
4
4
|
|
|
5
5
|
/** @description Scope naming rules to the given feature root */
|
|
6
6
|
export function createNamingConfigs(featureRoot, prefixLibMapping) {
|
|
7
7
|
const prefixPattern = `@(${Object.keys(prefixLibMapping).join("|")})`;
|
|
8
8
|
const sharedPrefixPattern = `@(shared|${Object.keys(prefixLibMapping).join("|")})`;
|
|
9
9
|
|
|
10
|
-
// DB prefix: value contains "/" = sub-directory origin = DB client
|
|
11
|
-
const dbPrefixKeys = Object.entries(prefixLibMapping)
|
|
12
|
-
.filter(([, value]) => value.includes("/"))
|
|
13
|
-
.map(([key]) => key);
|
|
14
|
-
|
|
15
10
|
const configs = [
|
|
16
11
|
{
|
|
17
12
|
name: "naming/services",
|
|
18
13
|
files: featuresGlob(featureRoot, "**/services/*.ts"),
|
|
19
|
-
ignores: featuresGlob(featureRoot, "shared/services/*.ts"),
|
|
20
14
|
plugins: { "check-file": checkFile },
|
|
21
15
|
rules: {
|
|
22
16
|
"check-file/filename-naming-convention": [
|
|
@@ -25,49 +19,18 @@ export function createNamingConfigs(featureRoot, prefixLibMapping) {
|
|
|
25
19
|
],
|
|
26
20
|
},
|
|
27
21
|
},
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
// Non-shared features: only DB prefixes allowed for repositories
|
|
31
|
-
if (dbPrefixKeys.length > 0) {
|
|
32
|
-
const dbPrefixPattern = `@(${dbPrefixKeys.join("|")})`;
|
|
33
|
-
configs.push({
|
|
22
|
+
{
|
|
34
23
|
name: "naming/repositories",
|
|
35
24
|
files: featuresGlob(featureRoot, "**/repositories/*.ts"),
|
|
36
|
-
ignores: featuresGlob(featureRoot, "shared/repositories/*.ts"),
|
|
37
25
|
plugins: { "check-file": checkFile },
|
|
38
26
|
rules: {
|
|
39
27
|
"check-file/filename-naming-convention": [
|
|
40
28
|
"error",
|
|
41
|
-
{ "**/*.ts": `${
|
|
29
|
+
{ "**/*.ts": `${prefixPattern}.repo` },
|
|
42
30
|
],
|
|
43
31
|
},
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
configs.push({
|
|
48
|
-
name: "naming/services-shared",
|
|
49
|
-
files: featuresGlob(featureRoot, "shared/services/*.ts"),
|
|
50
|
-
plugins: { "check-file": checkFile },
|
|
51
|
-
rules: {
|
|
52
|
-
"check-file/filename-naming-convention": [
|
|
53
|
-
"error",
|
|
54
|
-
{ "**/*.ts": "@(shared).service" },
|
|
55
|
-
],
|
|
56
32
|
},
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
// Shared feature: only "shared" prefix allowed
|
|
60
|
-
configs.push({
|
|
61
|
-
name: "naming/repositories-shared",
|
|
62
|
-
files: featuresGlob(featureRoot, "shared/repositories/*.ts"),
|
|
63
|
-
plugins: { "check-file": checkFile },
|
|
64
|
-
rules: {
|
|
65
|
-
"check-file/filename-naming-convention": [
|
|
66
|
-
"error",
|
|
67
|
-
{ "**/*.ts": "@(shared).repo" },
|
|
68
|
-
],
|
|
69
|
-
},
|
|
70
|
-
});
|
|
33
|
+
];
|
|
71
34
|
|
|
72
35
|
configs.push(
|
|
73
36
|
{
|
|
@@ -143,7 +106,6 @@ export function createNamingConfigs(featureRoot, prefixLibMapping) {
|
|
|
143
106
|
{
|
|
144
107
|
name: "naming/actions",
|
|
145
108
|
files: featuresGlob(featureRoot, "**/actions/*.ts"),
|
|
146
|
-
ignores: featuresGlob(featureRoot, "shared/actions/*.ts"),
|
|
147
109
|
plugins: { "check-file": checkFile },
|
|
148
110
|
rules: {
|
|
149
111
|
"check-file/filename-naming-convention": [
|
|
@@ -152,17 +114,6 @@ export function createNamingConfigs(featureRoot, prefixLibMapping) {
|
|
|
152
114
|
],
|
|
153
115
|
},
|
|
154
116
|
},
|
|
155
|
-
{
|
|
156
|
-
name: "naming/actions-shared",
|
|
157
|
-
files: featuresGlob(featureRoot, "shared/actions/*.ts"),
|
|
158
|
-
plugins: { "check-file": checkFile },
|
|
159
|
-
rules: {
|
|
160
|
-
"check-file/filename-naming-convention": [
|
|
161
|
-
"error",
|
|
162
|
-
{ "**/*.ts": "@(shared).action" },
|
|
163
|
-
],
|
|
164
|
-
},
|
|
165
|
-
},
|
|
166
117
|
{
|
|
167
118
|
name: "naming/actions-export",
|
|
168
119
|
files: featuresGlob(featureRoot, "**/actions/*.ts"),
|
|
@@ -181,11 +132,7 @@ export function createNamingConfigs(featureRoot, prefixLibMapping) {
|
|
|
181
132
|
{
|
|
182
133
|
name: "naming/actions-handle-service",
|
|
183
134
|
files: featuresGlob(featureRoot, "**/actions/*.ts"),
|
|
184
|
-
plugins: {
|
|
185
|
-
local: {
|
|
186
|
-
rules: { "action-handle-service": actionHandleServiceRule },
|
|
187
|
-
},
|
|
188
|
-
},
|
|
135
|
+
plugins: { local: localPlugin },
|
|
189
136
|
rules: {
|
|
190
137
|
"local/action-handle-service": "error",
|
|
191
138
|
},
|
package/src/deno/index.mjs
CHANGED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { localPlugin } from "../common/local-plugins/index.mjs";
|
|
2
|
+
|
|
3
|
+
/** @description Next.js: enforce relative paths within same feature, @/ for cross-feature */
|
|
4
|
+
export const importPathStyleConfigs = [
|
|
5
|
+
{
|
|
6
|
+
name: "imports/path-style",
|
|
7
|
+
files: ["src/features/**/*.ts"],
|
|
8
|
+
plugins: { local: localPlugin },
|
|
9
|
+
rules: {
|
|
10
|
+
"local/import-path-style": [
|
|
11
|
+
"error",
|
|
12
|
+
{ featureRoot: "src/features" },
|
|
13
|
+
],
|
|
14
|
+
},
|
|
15
|
+
},
|
|
16
|
+
];
|
package/src/next/index.mjs
CHANGED
|
@@ -2,6 +2,7 @@ import { createCommonConfigs } from "../common/index.mjs";
|
|
|
2
2
|
import { libBoundaryConfigs } from "../common/imports.mjs";
|
|
3
3
|
|
|
4
4
|
import { directivesConfigs } from "./directives.mjs";
|
|
5
|
+
import { importPathStyleConfigs } from "./imports.mjs";
|
|
5
6
|
import { namingConfigs } from "./naming.mjs";
|
|
6
7
|
|
|
7
8
|
export const eslintConfig = [
|
|
@@ -9,4 +10,5 @@ export const eslintConfig = [
|
|
|
9
10
|
...libBoundaryConfigs,
|
|
10
11
|
...namingConfigs,
|
|
11
12
|
...directivesConfigs,
|
|
13
|
+
...importPathStyleConfigs,
|
|
12
14
|
];
|
package/src/node/index.mjs
CHANGED