-
Notifications
You must be signed in to change notification settings - Fork 17
/
import-type.js
104 lines (85 loc) · 2.76 KB
/
import-type.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
const cond = require('lodash/cond');
const coreModules = require('builtin-modules/static');
const { join } = require('path');
const resolve = require('eslint-module-utils/resolve').default;
function constant(value) {
return () => value;
}
function baseModule(name) {
if (isScoped(name)) {
const [scope, pkg] = name.split('/');
return `${scope}/${pkg}`;
}
const [pkg] = name.split('/');
return pkg;
}
function isAbsolute(name) {
return name.indexOf('/') === 0;
}
function isBuiltIn(name, settings) {
const base = baseModule(name);
const extras = (settings && settings['import/core-modules']) || [];
return coreModules.indexOf(base) > -1 || extras.indexOf(base) > -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 externalModuleMainRegExp = /^[\w]((?!\/).)*$/;
function isExternalModuleMain(name, settings, path) {
return externalModuleMainRegExp.test(name) && isExternalPath(path, name, settings);
}
const scopedRegExp = /^@[^/]+\/[^/]+/;
function isScoped(name) {
return scopedRegExp.test(name);
}
const scopedMainRegExp = /^@[^/]+\/?[^/]+$/;
function isScopedMain(name) {
return scopedMainRegExp.test(name);
}
function isInternalModule(name, settings, path) {
return externalModuleRegExp.test(name) && !isExternalPath(path, name, settings);
}
function isRelativeToParent(name) {
return /^\.\.[\\/]/.test(name);
}
const indexFiles = ['.', './', './index', './index.js'];
function isIndex(name) {
return indexFiles.indexOf(name) !== -1;
}
function isRelativeToSibling(name) {
return /^\.[\\/]/.test(name);
}
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')]
]);
function isRegularExpressionGroup(group) {
return group && group[0] === '/' && group[group.length - 1] === '/' && group.length > 1;
}
function resolveImportType(name, context, regExpGroups) {
let matchingRegExpGroup = regExpGroups.find(([_groupName, regExp]) => regExp.test(name));
if (matchingRegExpGroup) {
return matchingRegExpGroup[0];
}
return typeTest(name, context.settings, resolve(name, context));
}
module.exports = {
isAbsolute,
isBuiltIn,
isExternalModuleMain,
isRegularExpressionGroup,
isScopedMain,
resolveImportType
};