-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
214 lines (206 loc) · 7.04 KB
/
index.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
"use strict"
const {readFileSync} = require("fs")
const {join, relative} = require("path")
const readJSON = path => {
try {
return JSON.parse(readFileSync(path, "utf8"))
} catch {
return null
}
}
const getTSConfigPaths = context => {
const config = readJSON(join(context.cwd, "tsconfig.json"))
const paths = {}
for (const key of Object.keys(config?.compilerOptions?.paths ?? {})) {
paths[key] = config.compilerOptions.paths[key]?.[0]
?? config.compilerOptions.paths[key]
}
return paths
}
const getImportDetails = node => {
if (node.type === "ImportDeclaration") {
// Pure ESM
return {
"isType": node.importKind === "type", "path": node.source?.value
}
}
if (node.type === "ExpressionStatement") {
// Import function
if (node.expression.type === "CallExpression"
&& node.expression.callee.type === "Identifier"
&& node.expression.callee.name === "import") {
return {
"isType": node.importKind === "type", "path": node.source?.value
}
}
// Require function
if (node.expression.type === "CallExpression"
&& node.expression.callee.type === "Identifier"
&& node.expression.callee.name === "require") {
return {"path": node.source?.value}
}
}
if (node.type === "VariableDeclaration") {
// Require function to variable
if (node.declarations.some(d => d.init
&& d.init.type === "CallExpression"
&& d.init.callee.type === "Identifier"
&& d.init.callee.name === "require")) {
return {"path": node.source?.value}
}
// Import function to variable
if (node.declarations.some(d => d.init
&& d.init.type === "CallExpression"
&& d.init.callee.type === "Identifier"
&& d.init.callee.name === "import")) {
return {"path": node.source?.value}
}
// Lazy loaders
const lazyNames = ["lazy", "lazyWithRetry", "lazily", "lazilyWithRetry"]
const dec = node.declarations.find(d => d.init
&& d.init.type === "CallExpression"
&& d.init.callee.type === "Identifier"
&& lazyNames.includes(d.init.callee.name)
&& d.init.arguments.length > 0
&& d.init.arguments[0].body.type === "ImportExpression")
return {"path": dec?.init.arguments[0].body.source.value}
}
return false
}
const getOwnDomain = (context, paths) => {
const ownPath = relative(context.cwd, context.getFilename())
return Object.keys(paths).find(k => {
const path = relative(context.cwd, paths[k].replace("/*", ""))
return ownPath.startsWith(path)
})?.replace("/*", "")
}
const getImportDomain = (importPath, paths) => {
const pathKeys = Object.keys(paths).map(k => k.replace("/*", ""))
.filter(k => k !== "*" && k.startsWith("@"))
return pathKeys.find(k => importPath.startsWith(`${k}/`))
?? Object.keys(paths).find(k => {
let loc = paths[k].replace("/*", "")
if (importPath.startsWith(loc)) {
return true
}
if (loc.startsWith("./src/")) {
loc = loc.replace("./src/", "./")
return importPath.startsWith(loc)
}
return false
})?.replace("/*", "")
}
const importRule = {
"create": context => ({":statement": node => {
const opts = context.options?.[0] ?? {}
let paths = {}
if (opts.useTSConfig) {
paths = {...paths, ...getTSConfigPaths(context)}
}
if (opts.domains) {
paths = {...paths, ...opts.domains}
}
if (!Object.keys(paths).length) {
return
}
const {"path": importPath, isType} = getImportDetails(node) ?? {}
if (!importPath) {
return
}
const ownDomain = getOwnDomain(context, paths)
const importDomain = getImportDomain(importPath, paths)
if (opts.shared?.includes(importDomain)) {
// Ignore modules in the shared list from being reported
return
}
if (!opts.reportOutside && !ownDomain) {
// Ignore if no domain is found for the file
return
}
if (!opts.reportTypes && isType) {
// Ignore if the import is a type
return
}
if (opts.allowedCrossings?.[ownDomain]?.includes(importDomain)) {
// Ignore if the crossing is allowed
return
}
if (importDomain && importDomain !== ownDomain) {
// Report if the import of a domain and not part of this domain
context.report({
"data": {
"imdomain": importDomain,
"owndomain": ownDomain ?? "<none>",
"path": importPath
},
"messageId": "import",
node
})
}
}}),
"meta": {
"docs": {
"description": "Find/prevent cross-module imports in your codebase",
"recommended": true,
"url": "https://github.com/Jelmerro/eslint-plugin-cross-module-imports"
},
"messages": {
"import": "{{ path }} of {{ imdomain }} is outside {{ owndomain }}"
},
"schema": [{
"properties": {
"allowedCrossings": {
"patternProperties": {
".*": {
"additionalItems": false,
"items": {
"type": "string"
},
"type": "array",
"uniqueItems": true
}
},
"type": "object"
},
"domains": {
"patternProperties": {
".*": {
"items": {
"type": "string"
},
"type": "string"
}
},
"type": "object"
},
"reportOutside": {
"default": true,
"type": "boolean"
},
"reportTypes": {
"default": true,
"type": "boolean"
},
"shared": {
"additionalItems": false,
"items": {
"type": "string"
},
"type": "array",
"uniqueItems": true
},
"useTSConfig": {
"default": true,
"type": "boolean"
}
},
"type": "object"
}],
"type": "problem"
}
}
module.exports = {
"rules": {
"no-cross-imports": importRule
}
}