-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
importType.js
66 lines (53 loc) · 1.84 KB
/
importType.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
import cond from 'lodash.cond'
import builtinModules from 'builtin-modules'
import { join } from 'path'
import resolve from 'eslint-module-utils/resolve'
function constant(value) {
return () => value
}
export function isAbsolute(name) {
return name.indexOf('/') === 0
}
export function isBuiltIn(name, settings) {
const extras = (settings && settings['import/core-modules']) || []
return builtinModules.indexOf(name) !== -1 || extras.indexOf(name) > -1
}
function isExternalPath(path, name, settings) {
const folders = (settings && settings['import/external-module-folders']) || ['node_modules']
return !path || folders.some(folder => -1 < path.indexOf(join(folder, name)))
}
const externalModuleRegExp = /^\w/
function isExternalModule(name, settings, path) {
return externalModuleRegExp.test(name) && isExternalPath(path, name, settings)
}
const scopedRegExp = /^@\w+\/\w+/
function isScoped(name) {
return scopedRegExp.test(name)
}
function isInternalModule(name, settings, path) {
return externalModuleRegExp.test(name) && !isExternalPath(path, name, settings)
}
function isRelativeToParent(name) {
return name.indexOf('../') === 0
}
const indexFiles = ['.', './', './index', './index.js']
function isIndex(name) {
return indexFiles.indexOf(name) !== -1
}
function isRelativeToSibling(name) {
return name.indexOf('./') === 0
}
const typeTest = cond([
[isAbsolute, constant('absolute')],
[isBuiltIn, constant('builtin')],
[isExternalModule, constant('external')],
[isScoped, constant('external')],
[isInternalModule, constant('internal')],
[isRelativeToParent, constant('parent')],
[isIndex, constant('index')],
[isRelativeToSibling, constant('sibling')],
[constant(true), constant('unknown')],
])
export default function resolveImportType(name, context) {
return typeTest(name, context.settings, resolve(name, context))
}