@kubit-ui-web/eslint-plugin-no-index-import 1.0.0 → 1.0.2
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/README.md +10 -6
- package/lib/rules/no-index-import.js +28 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -31,9 +31,9 @@ Add `no-index-import` to the `plugins` array and configure the rule in the `rule
|
|
|
31
31
|
```js
|
|
32
32
|
module.exports = {
|
|
33
33
|
// Other ESLint configurations...
|
|
34
|
-
plugins: ['no-index-import'],
|
|
34
|
+
plugins: ['@kubit-ui-web/no-index-import'],
|
|
35
35
|
rules: {
|
|
36
|
-
'no-index-import/no-index-import': 'error',
|
|
36
|
+
'@kubit-ui-web/no-index-import/no-index-import': 'error',
|
|
37
37
|
},
|
|
38
38
|
};
|
|
39
39
|
```
|
|
@@ -45,9 +45,9 @@ You can also configure aliases and ignore specific imports:
|
|
|
45
45
|
```js
|
|
46
46
|
module.exports = {
|
|
47
47
|
// Other ESLint configurations...
|
|
48
|
-
plugins: ['no-index-import'],
|
|
48
|
+
plugins: ['@kubit-ui-web/no-index-import'],
|
|
49
49
|
rules: {
|
|
50
|
-
'no-index-import/no-index-import': [
|
|
50
|
+
'@kubit-ui-web/no-index-import/no-index-import': [
|
|
51
51
|
'error',
|
|
52
52
|
{
|
|
53
53
|
aliases: {
|
|
@@ -84,9 +84,13 @@ module.exports = {
|
|
|
84
84
|
ecmaVersion: 12,
|
|
85
85
|
sourceType: 'module',
|
|
86
86
|
},
|
|
87
|
-
plugins: [
|
|
87
|
+
plugins: [
|
|
88
|
+
'react',
|
|
89
|
+
'@typescript-eslint',
|
|
90
|
+
'@kubit-ui-web/no-index-import/no-index-import',
|
|
91
|
+
],
|
|
88
92
|
rules: {
|
|
89
|
-
'no-index-import/no-index-import': [
|
|
93
|
+
'@kubit-ui-web/no-index-import/no-index-import': [
|
|
90
94
|
'error',
|
|
91
95
|
{
|
|
92
96
|
aliases: {
|
|
@@ -12,7 +12,9 @@ function isIndexOrDirectoryWithIndex(fullPath) {
|
|
|
12
12
|
try {
|
|
13
13
|
const stat = fs.statSync(fullPath);
|
|
14
14
|
if (stat.isDirectory()) {
|
|
15
|
-
return indexFiles.some(indexFile =>
|
|
15
|
+
return indexFiles.some((indexFile) =>
|
|
16
|
+
fs.existsSync(path.join(fullPath, indexFile))
|
|
17
|
+
);
|
|
16
18
|
} else if (stat.isFile()) {
|
|
17
19
|
return indexFiles.includes(path.basename(fullPath));
|
|
18
20
|
}
|
|
@@ -40,6 +42,21 @@ function resolveFullPath(importPath, aliases, contextFilename) {
|
|
|
40
42
|
return path.resolve(path.dirname(contextFilename), importPath);
|
|
41
43
|
}
|
|
42
44
|
|
|
45
|
+
/**
|
|
46
|
+
* Checks if the import path ends with '/index'.
|
|
47
|
+
* @param {string} importPath - The import path.
|
|
48
|
+
* @returns {boolean} - Returns true if the import path ends with '/index', otherwise false.
|
|
49
|
+
*/
|
|
50
|
+
function endsWithIndex(importPath) {
|
|
51
|
+
return (
|
|
52
|
+
importPath.endsWith('/index') ||
|
|
53
|
+
importPath.endsWith('/index.js') ||
|
|
54
|
+
importPath.endsWith('/index.jsx') ||
|
|
55
|
+
importPath.endsWith('/index.ts') ||
|
|
56
|
+
importPath.endsWith('/index.tsx')
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
|
|
43
60
|
module.exports = {
|
|
44
61
|
meta: {
|
|
45
62
|
type: 'problem',
|
|
@@ -88,10 +105,17 @@ module.exports = {
|
|
|
88
105
|
return;
|
|
89
106
|
}
|
|
90
107
|
|
|
91
|
-
const fullPath = resolveFullPath(
|
|
108
|
+
const fullPath = resolveFullPath(
|
|
109
|
+
importPath,
|
|
110
|
+
aliases,
|
|
111
|
+
context.getFilename()
|
|
112
|
+
);
|
|
92
113
|
|
|
93
|
-
// Check if the import path points to an index file
|
|
94
|
-
if (
|
|
114
|
+
// Check if the import path points to an index file, a directory containing an 'index' file, or ends with '/index'
|
|
115
|
+
if (
|
|
116
|
+
isIndexOrDirectoryWithIndex(fullPath) ||
|
|
117
|
+
endsWithIndex(importPath)
|
|
118
|
+
) {
|
|
95
119
|
context.report({
|
|
96
120
|
node,
|
|
97
121
|
message: 'Importing from index files is not allowed.',
|