eslint-plugin-stratified-design 0.8.0-beta.2 → 0.8.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.
@@ -3,9 +3,7 @@
3
3
  const { minimatch } = require("minimatch");
4
4
  const { resolvePath, reducedPaths, parsePath } = require("../common");
5
5
  const {
6
- toStructure,
7
6
  replaceAlias,
8
- readRawStructure,
9
7
  findLevel,
10
8
  findLayerWithReducedPath,
11
9
  readStructure,
@@ -30,6 +28,7 @@ const BARRIER = "barrier";
30
28
  * }} Options
31
29
  */
32
30
 
31
+ // @level 2
33
32
  /**
34
33
  * @param {Options} options
35
34
  * @param {string} contextFileSource
@@ -52,6 +51,7 @@ const parseFileSource = (options, contextFileSource) => {
52
51
  };
53
52
  };
54
53
 
54
+ // @level 2
55
55
  /**
56
56
  * @param {string} cwd
57
57
  * @param {string[]} excludeImports
@@ -79,6 +79,7 @@ const createModulePath = (cwd, excludeImports, fileDir, aliases) => {
79
79
  };
80
80
  };
81
81
 
82
+ // @level 2
82
83
  /**
83
84
  * @param {import('./2 layer').Structure} structure
84
85
  * @param {number} fileLevel
@@ -94,6 +95,7 @@ const findLevelInCurrent = (structure, fileLevel) => {
94
95
  };
95
96
  };
96
97
 
98
+ // @level 2
97
99
  /**
98
100
  * @param {import('./2 layer').Structure} structure
99
101
  */
@@ -105,13 +107,13 @@ const findLevelInChild = (structure) => {
105
107
  const layer = findLayerWithReducedPath(structure)(modulePath);
106
108
  if (!layer) return null;
107
109
  const fileDir = layer.name;
108
- const rawChildStructure = readRawStructure(fileDir);
109
- const childStructure = toStructure(rawChildStructure, fileDir);
110
+ const childStructure = readStructure(fileDir);
110
111
  const childLevel = findLevel(childStructure)(modulePath);
111
112
  return childLevel !== null ? findLevel(structure)(fileDir) : null;
112
113
  };
113
114
  };
114
115
 
116
+ // @level 1
115
117
  /**
116
118
  * @param {string} cwd
117
119
  * @param {string} fileDir
@@ -147,14 +149,17 @@ const findLevelInParent = (cwd, fileDir, fileLevel) => {
147
149
  }
148
150
 
149
151
  const fileLevel = findLevel(structure)(filePath);
150
- const moduleLevel = findLevel(structure)(modulePath);
152
+ if (fileLevel === null) return result(FAIL);
151
153
 
152
- if (hasBarrier(structure, fileLevel)(moduleLevel)) {
153
- return result(BARRIER);
154
- } else if (fileLevel === null) {
155
- return result(FAIL);
156
- } else if (moduleLevel === null) {
154
+ const moduleLevel = (() => {
155
+ const level = findLevel(structure)(modulePath);
156
+ return level !== null ? level : findLevelInChild(structure)(modulePath);
157
+ })();
158
+
159
+ if (moduleLevel === null) {
157
160
  return result(null);
161
+ } else if (hasBarrier(structure, fileLevel)(moduleLevel)) {
162
+ return result(BARRIER);
158
163
  } else {
159
164
  return result(originalFileLevel + moduleLevel - fileLevel);
160
165
  }
@@ -212,7 +212,19 @@ const isInParent = (fileDir) => {
212
212
  /**
213
213
  * @param {string} modulePath
214
214
  */
215
- return (modulePath) => toRelative(fileDir, modulePath).startsWith("..");
215
+ return (modulePath) =>
216
+ toSegments(toRelative(fileDir, modulePath))[0] === "..";
217
+ };
218
+
219
+ // @level 2
220
+ /**
221
+ * @param {string} fileDir
222
+ */
223
+ const isInChildren = (fileDir) => {
224
+ /**
225
+ * @param {string} modulePath
226
+ */
227
+ return (modulePath) => toSegments(toRelative(fileDir, modulePath))[0] === ".";
216
228
  };
217
229
 
218
230
  // @level 1
@@ -234,5 +246,6 @@ module.exports = {
234
246
  readStructure,
235
247
  hasBarrier,
236
248
  isInParent,
249
+ isInChildren,
237
250
  validateRawStructure,
238
251
  };
@@ -17,6 +17,7 @@ const {
17
17
  findLevel,
18
18
  isNotRegisteredNodeModule,
19
19
  isInParent,
20
+ isInChildren,
20
21
  validateRawStructure,
21
22
  readRawStructure,
22
23
  } = require("./2 layer");
@@ -34,6 +35,7 @@ module.exports = {
34
35
  findLevelInChild,
35
36
  findLevelInParent,
36
37
  isInParent,
38
+ isInChildren,
37
39
  validateRawStructure,
38
40
  FAIL,
39
41
  SUCCESS,
@@ -90,7 +90,7 @@ module.exports = {
90
90
  helper.createAliases(options.aliases)
91
91
  );
92
92
 
93
- const isInParent = helper.isInParent(fileDir);
93
+ const isInChildren = helper.isInChildren(fileDir);
94
94
 
95
95
  const findLevelInCurrent = helper.findLevelInCurrent(structure, fileLevel);
96
96
 
@@ -102,10 +102,6 @@ module.exports = {
102
102
  fileLevel
103
103
  );
104
104
 
105
- const isNotRegisteredNodeModule = helper.isNotRegisteredNodeModule(
106
- context.cwd
107
- );
108
-
109
105
  /**
110
106
  * @param {import('../helpers/type').Node} node
111
107
  * @param {'not-lower-level'|'barrier'|'not-registered'|'invalid-json'} messageId
@@ -145,10 +141,10 @@ module.exports = {
145
141
  const moduleLevel = (() => {
146
142
  const level = findLevelInCurrent(modulePath);
147
143
  if (level !== null) return level;
148
- if (isInParent(modulePath) || isNotRegisteredNodeModule(modulePath)) {
149
- return findLevelInParent(modulePath);
144
+ if (isInChildren(modulePath)) {
145
+ return findLevelInChild(modulePath);
150
146
  }
151
- return findLevelInChild(modulePath);
147
+ return findLevelInParent(modulePath);
152
148
  })();
153
149
 
154
150
  if (moduleLevel === helper.SUCCESS) return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-stratified-design",
3
- "version": "0.8.0-beta.2",
3
+ "version": "0.8.0",
4
4
  "description": "ESlint rules for stratified design",
5
5
  "keywords": [
6
6
  "eslint",
@@ -16,6 +16,8 @@ const {
16
16
  createAliases,
17
17
  replaceAlias,
18
18
  validateRawStructure,
19
+ isInParent,
20
+ isInChildren,
19
21
  } = require("../../../lib/helpers/stratifiedImports/2 layer");
20
22
  const {
21
23
  createModulePath,
@@ -289,4 +291,54 @@ describe("helpers/stratified-imports", () => {
289
291
  });
290
292
  });
291
293
  });
294
+
295
+ describe("isInParent()", () => {
296
+ const testCases = [
297
+ {
298
+ fileDir: "src/layerA",
299
+ modulePath: "src/layerB/layerBA",
300
+ expected: true,
301
+ },
302
+ {
303
+ fileDir: "src/layerA",
304
+ modulePath: "src/layerA/layerAA",
305
+ expected: false,
306
+ },
307
+ {
308
+ fileDir: "src/layerA",
309
+ modulePath: "nodeModule",
310
+ expected: true,
311
+ },
312
+ ];
313
+ testCases.forEach(({ fileDir, modulePath, expected }) => {
314
+ it(`${fileDir}, ${modulePath} -> ${expected}`, () => {
315
+ assert.equal(isInParent(fileDir)(modulePath), expected);
316
+ });
317
+ });
318
+ });
319
+
320
+ describe("isInChildren()", () => {
321
+ const testCases = [
322
+ {
323
+ fileDir: "src/layerA",
324
+ modulePath: "src/layerA/layerAA",
325
+ expected: true,
326
+ },
327
+ {
328
+ fileDir: "src/layerA",
329
+ modulePath: "src/layerB/layerBA",
330
+ expected: false,
331
+ },
332
+ {
333
+ fileDir: "src/layerA",
334
+ modulePath: "nodeModule",
335
+ expected: false,
336
+ },
337
+ ];
338
+ testCases.forEach(({ fileDir, modulePath, expected }) => {
339
+ it(`${fileDir}, ${modulePath} -> ${expected}`, () => {
340
+ assert.equal(isInChildren(fileDir)(modulePath), expected);
341
+ });
342
+ });
343
+ });
292
344
  });
@@ -106,6 +106,11 @@ ruleTester.run("stratified-imports", rule, {
106
106
  filename: "./mocked/stratified-imports/layerJ/entryJA.js",
107
107
  options: [],
108
108
  },
109
+ {
110
+ code: "import { func } from '../layerJ/entryJA'",
111
+ filename: "./mocked/stratified-imports/layerD/layerDA.js",
112
+ options: [],
113
+ },
109
114
  {
110
115
  code: "import { func } from './layerJC'",
111
116
  filename: "./mocked/stratified-imports/layerJ/entryJB.js",