-
Notifications
You must be signed in to change notification settings - Fork 0
/
eslint.restrected-files.js
85 lines (78 loc) · 2.22 KB
/
eslint.restrected-files.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { readdirSync, statSync } from 'fs'
const getDirectories = (dir, _files = []) => {
const files = readdirSync(dir)
for (const file of files) {
const name = `${dir}/${file}`
_files.push(dir)
if (statSync(name).isDirectory()) {
getDirectories(name, _files)
}
}
return _files
}
export const isParent = (thisPath, isParentThisPath) => {
const isParentThisPaths = isParentThisPath.split('/')
isParentThisPaths.pop()
const stack = []
for (const path of isParentThisPaths) {
stack.push(path)
if (thisPath !== isParentThisPath && thisPath === stack.join('/')) {
return true
}
}
return false
}
export const isFirstChild = (thisPath, isFirstChildThisPath) => {
const thisPaths = thisPath.split('/')
thisPaths.pop()
return thisPath !== isFirstChildThisPath && thisPaths.join('/') === isFirstChildThisPath
}
export const isDeepChild = (thisPath, isDeepChildThisPath) => {
const thisPaths = thisPath.split('/')
const stack = []
for (const path of thisPaths) {
stack.push(path)
if (isDeepChildThisPath === stack.join('/')) {
return true
}
}
return false
}
export const getTargetPaths = (filePath, dirs) => {
const paths = []
for (const path of dirs) {
if (path === filePath || isParent(path, filePath)) {
continue
}
if (isDeepChild(path, filePath) && !isFirstChild(path, filePath)) {
continue
}
if (filePath.startsWith('./src/_internal/types')) {
continue
}
paths.push(path)
}
return getOnlyShallows(paths)
}
export const getOnlyShallows = dirs => {
const stack = []
for (const path of dirs) {
if (dirs.some(d => d !== path && isDeepChild(path, d))) {
continue
}
stack.push(path)
}
return stack
}
export const getRules = () => {
const dirs = [...new Set(getDirectories('./src/_internal'))]
const zones = []
for (const from of dirs) {
zones.push({
from: `${from}/*`,
target: ['./src/core', ...getTargetPaths(from, dirs)],
message: `${from}直下のファイルをインポートする際は同階層のファイルか親階層のファイルからしかインポートできません。例外としてsrc/modules内からは可能です。`,
})
}
return zones
}