eslint-plugin-stratified-design 0.5.2 → 0.6.0-beta

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.
@@ -65,6 +65,20 @@ const createModulePath = (cwd, fileDir, aliases) => {
65
65
  };
66
66
  };
67
67
 
68
+ /**
69
+ * @param {*} options
70
+ * @param {string} fileDor
71
+ * @param {string} modulePath
72
+ */
73
+ const isFileIndexOfModule = (options, fileDir, filePath) => (modulePath) => {
74
+ if (!options.isIndexHighest || parsePath(filePath).name !== "index") {
75
+ return false;
76
+ }
77
+ const segments = toSegments(toRelative(fileDir, modulePath));
78
+ if (segments.length === 2 && segments[0] === ".") return true;
79
+ return false;
80
+ };
81
+
68
82
  /**
69
83
  * Report error about using `options.useLevelNumber`
70
84
  * @param {import('eslint').Rule.RuleContext} context
@@ -222,6 +236,7 @@ module.exports = {
222
236
  createRootDir,
223
237
  parseFileSource,
224
238
  createModulePath,
239
+ isFileIndexOfModule,
225
240
  reportHasProperLevelNumber,
226
241
  reportInSubDirOfFileDir,
227
242
  reportHasProperLevel,
@@ -21,6 +21,7 @@ const {
21
21
  parseFileSource,
22
22
  createRootDir,
23
23
  } = require("../helpers/lowerLevelImports/");
24
+ const { isFileIndexOfModule } = require("../helpers/lowerLevelImports/1 layer");
24
25
 
25
26
  //------------------------------------------------------------------------------
26
27
  // Rule Definition
@@ -89,6 +90,9 @@ module.exports = {
89
90
  useLevelNumber: {
90
91
  type: "boolean",
91
92
  },
93
+ isIndexHighest: {
94
+ type: "boolean",
95
+ },
92
96
  },
93
97
  additionalProperties: false,
94
98
  },
@@ -151,17 +155,31 @@ module.exports = {
151
155
  filePath
152
156
  );
153
157
 
154
- return {
155
- ImportDeclaration(node) {
156
- if (isExcludedFile) return;
158
+ const isFileIndex = isFileIndexOfModule(options, fileDir, filePath);
159
+
160
+ const report = (node) => {
161
+ if (isExcludedFile) return;
162
+
163
+ const modulePath = createModulePath(node.source.value);
157
164
 
158
- const modulePath = createModulePath(node.source.value);
165
+ if (isFileIndex(modulePath)) return;
159
166
 
160
- if (reportHasProperLevelNumber(node, modulePath) === FINISHED) return;
167
+ if (reportHasProperLevelNumber(node, modulePath) === FINISHED) return;
161
168
 
162
- if (reportInSubDirOfFileDir(node, modulePath) === FINISHED) return;
169
+ if (reportInSubDirOfFileDir(node, modulePath) === FINISHED) return;
163
170
 
164
- reportHasProperLevel(node, modulePath);
171
+ reportHasProperLevel(node, modulePath);
172
+ };
173
+
174
+ return {
175
+ ImportDeclaration(node) {
176
+ report(node);
177
+ },
178
+ ExportNamedDeclaration(node) {
179
+ report(node);
180
+ },
181
+ ExportAllDeclaration(node) {
182
+ report(node);
165
183
  },
166
184
  };
167
185
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-stratified-design",
3
- "version": "0.5.2",
3
+ "version": "0.6.0-beta",
4
4
  "description": "ESlint rules for stratified design",
5
5
  "keywords": [
6
6
  "eslint",
@@ -199,6 +199,18 @@ ruleTester.run("lower-level-imports", rule, {
199
199
  },
200
200
  ],
201
201
  },
202
+ {
203
+ code: "import { func } from '@/other/file.js'",
204
+ filename: "./src/other/index.js",
205
+ options: [
206
+ {
207
+ structure,
208
+ root: "./src",
209
+ aliases: { "@/": "./src/" },
210
+ isIndexHighest: true,
211
+ },
212
+ ],
213
+ },
202
214
  ],
203
215
  invalid: [
204
216
  {
@@ -212,6 +224,28 @@ ruleTester.run("lower-level-imports", rule, {
212
224
  },
213
225
  ],
214
226
  },
227
+ {
228
+ code: "export { func } from './otherLayerB'",
229
+ filename: "./src/otherLayerA.js",
230
+ options: [{ structure, root: "./src" }],
231
+ errors: [
232
+ {
233
+ messageId: "not-registered:file",
234
+ data: { module: "./otherLayerB", file: "./otherLayerA" },
235
+ },
236
+ ],
237
+ },
238
+ {
239
+ code: "export * from './otherLayerB'",
240
+ filename: "./src/otherLayerA.js",
241
+ options: [{ structure, root: "./src" }],
242
+ errors: [
243
+ {
244
+ messageId: "not-registered:file",
245
+ data: { module: "./otherLayerB", file: "./otherLayerA" },
246
+ },
247
+ ],
248
+ },
215
249
  {
216
250
  code: "import { func } from 'node-module'",
217
251
  filename: "./src/layer3/subLayer1.js",
@@ -350,5 +384,22 @@ ruleTester.run("lower-level-imports", rule, {
350
384
  },
351
385
  ],
352
386
  },
387
+ {
388
+ code: "import { func } from '@/other/file.js'",
389
+ filename: "./src/other/index.js",
390
+ options: [
391
+ {
392
+ structure,
393
+ root: "./src",
394
+ aliases: { "@/": "./src/" },
395
+ },
396
+ ],
397
+ errors: [
398
+ {
399
+ messageId: "not-registered:file",
400
+ data: { module: "./other/file.js", file: "./other/index" },
401
+ },
402
+ ],
403
+ },
353
404
  ],
354
405
  });