-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathno-relative-parent-imports.js
34 lines (29 loc) · 1.08 KB
/
no-relative-parent-imports.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
import moduleVisitor, { makeOptionsSchema } from 'eslint-module-utils/moduleVisitor'
import docsUrl from '../docsUrl'
import { basename } from 'path'
import importType from '../core/importType'
module.exports = {
meta: {
docs: {
url: docsUrl('no-relative-parent-imports'),
},
schema: [makeOptionsSchema()],
},
create: function noRelativePackages(context) {
const myPath = context.getFilename()
if (myPath === '<text>') return {} // can't cycle-check a non-file
function checkSourceValue(sourceNode) {
const depPath = sourceNode.value
if (importType(depPath, context) === 'parent') {
context.report({
node: sourceNode,
message: 'Relative imports from parent directories are not allowed. ' +
`Please either pass what you're importing through at runtime ` +
`(dependency injection), move \`${basename(myPath)}\` to same ` +
`directory as \`${depPath}\` or consider making \`${depPath}\` a package.`,
})
}
}
return moduleVisitor(checkSourceValue, context.options[0])
},
}