eslint-plugin-stratified-design 0.5.1 → 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
@@ -99,16 +113,20 @@ const reportHasProperLevelNumber = (context, options, rootDir, filePath) => {
99
113
  return toPath(parent);
100
114
  })();
101
115
 
102
- const { moduleLevel, isInterfaceError } = (() => {
116
+ const { moduleLevel, isBarrierError } = (() => {
103
117
  const segments = toSegments(toRelative(parentPath, modulePath));
104
- const level = extractLevel(segments[1]);
118
+ const moduleLevel = extractLevel(segments[1]);
119
+ const isBarrierError =
120
+ segments.length === 2
121
+ ? false
122
+ : segments.some((seg) => extractLevel(seg) !== null);
105
123
  return {
106
- moduleLevel: level,
107
- isInterfaceError: segments.length > 2,
124
+ moduleLevel,
125
+ isBarrierError,
108
126
  };
109
127
  })();
110
128
 
111
- if (isInterfaceError) {
129
+ if (isBarrierError) {
112
130
  report("barrier");
113
131
  return FINISHED;
114
132
  }
@@ -218,6 +236,7 @@ module.exports = {
218
236
  createRootDir,
219
237
  parseFileSource,
220
238
  createModulePath,
239
+ isFileIndexOfModule,
221
240
  reportHasProperLevelNumber,
222
241
  reportInSubDirOfFileDir,
223
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.1",
3
+ "version": "0.6.0-beta",
4
4
  "description": "ESlint rules for stratified design",
5
5
  "keywords": [
6
6
  "eslint",
@@ -187,6 +187,30 @@ ruleTester.run("lower-level-imports", rule, {
187
187
  },
188
188
  ],
189
189
  },
190
+ {
191
+ code: "import { func } from '@/layer1/subLayer2/sub'",
192
+ filename: "./src/layer1/subLayer1/1 layer.js",
193
+ options: [
194
+ {
195
+ structure,
196
+ root: "./src",
197
+ aliases: { "@/": "./src/" },
198
+ useLevelNumber: true,
199
+ },
200
+ ],
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
+ },
190
214
  ],
191
215
  invalid: [
192
216
  {
@@ -200,6 +224,28 @@ ruleTester.run("lower-level-imports", rule, {
200
224
  },
201
225
  ],
202
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
+ },
203
249
  {
204
250
  code: "import { func } from 'node-module'",
205
251
  filename: "./src/layer3/subLayer1.js",
@@ -338,5 +384,22 @@ ruleTester.run("lower-level-imports", rule, {
338
384
  },
339
385
  ],
340
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
+ },
341
404
  ],
342
405
  });