/
importType.js
131 lines (108 loc) · 3.85 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
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
import { isAbsolute as nodeIsAbsolute, relative, resolve as nodeResolve } from 'path';
import isCoreModule from 'is-core-module';
import resolve from 'eslint-module-utils/resolve';
import { getContextPackagePath } from './packagePath';
function baseModule(name) {
if (isScoped(name)) {
const [scope, pkg] = name.split('/');
return `${scope}/${pkg}`;
}
const [pkg] = name.split('/');
return pkg;
}
function isInternalRegexMatch(name, settings) {
const internalScope = (settings && settings['import/internal-regex']);
return internalScope && new RegExp(internalScope).test(name);
}
export function isAbsolute(name) {
return typeof name === 'string' && nodeIsAbsolute(name);
}
// path is defined only when a resolver resolves to a non-standard path
export function isBuiltIn(name, settings, path) {
if (path || !name) return false;
const base = baseModule(name);
const extras = (settings && settings['import/core-modules']) || [];
return isCoreModule(base) || extras.indexOf(base) > -1;
}
export function isExternalModule(name, path, context) {
if (arguments.length < 3) {
throw new TypeError('isExternalModule: name, path, and context are all required');
}
return (isModule(name) || isScoped(name)) && typeTest(name, context, () => path) === 'external';
}
export function isExternalModuleMain(name, path, context) {
if (arguments.length < 3) {
throw new TypeError('isExternalModule: name, path, and context are all required');
}
return isModuleMain(name) && typeTest(name, context, () => path) === 'external';
}
const moduleRegExp = /^\w/;
function isModule(name) {
return name && moduleRegExp.test(name);
}
const moduleMainRegExp = /^[\w]((?!\/).)*$/;
function isModuleMain(name) {
return name && moduleMainRegExp.test(name);
}
const scopedRegExp = /^@[^/]+\/?[^/]+/;
export function isScoped(name) {
return name && scopedRegExp.test(name);
}
const scopedMainRegExp = /^@[^/]+\/?[^/]+$/;
export function isScopedMain(name) {
return name && scopedMainRegExp.test(name);
}
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);
}
function isExternalPath(path, context) {
if (!path) {
return false;
}
const { settings } = context;
const packagePath = getContextPackagePath(context);
if (relative(packagePath, path).startsWith('..')) {
return true;
}
const folders = (settings && settings['import/external-module-folders']) || ['node_modules'];
return folders.some((folder) => {
const folderPath = nodeResolve(packagePath, folder);
const relativePath = relative(folderPath, path);
return !relativePath.startsWith('..');
});
}
function isInternalPath(path, context) {
if (!path) {
return false;
}
const packagePath = getContextPackagePath(context);
return !relative(packagePath, path).startsWith('../');
}
function isExternalLookingName(name) {
return isModule(name) || isScoped(name);
}
function typeTest(name, context, getPath) {
const { settings } = context;
if (isInternalRegexMatch(name, settings)) { return 'internal'; }
if (isAbsolute(name)) { return 'absolute'; }
if (isRelativeToParent(name)) { return 'parent'; }
if (isIndex(name)) { return 'index'; }
if (isRelativeToSibling(name)) { return 'sibling'; }
// `resolve` is expensive, do it only when necessary
const path = getPath();
if (isBuiltIn(name, settings, path)) { return 'builtin'; }
if (isExternalPath(path, context)) { return 'external'; }
if (isInternalPath(path, context)) { return 'internal'; }
if (isExternalLookingName(name)) { return 'external'; }
return 'unknown';
}
export default function resolveImportType(name, context) {
return typeTest(name, context, () => resolve(name, context));
}