1 | 'use strict';
|
2 |
|
3 | exports.__esModule = true;
|
4 |
|
5 | const fs = require('fs');
|
6 | const Module = require('module');
|
7 | const path = require('path');
|
8 | const { getPhysicalFilename } = require('./contextCompat');
|
9 |
|
10 | const hashObject = require('./hash').hashObject;
|
11 | const ModuleCache = require('./ModuleCache').default;
|
12 | const pkgDir = require('./pkgDir').default;
|
13 |
|
14 | const CASE_SENSITIVE_FS = !fs.existsSync(path.join(__dirname.toUpperCase(), 'reSOLVE.js'));
|
15 | exports.CASE_SENSITIVE_FS = CASE_SENSITIVE_FS;
|
16 |
|
17 | const ERROR_NAME = 'EslintPluginImportResolveError';
|
18 |
|
19 | const fileExistsCache = new ModuleCache();
|
20 |
|
21 |
|
22 |
|
23 | const createRequire = Module.createRequire
|
24 |
|
25 | || Module.createRequireFromPath
|
26 | || function (filename) {
|
27 | const mod = new Module(filename, void null);
|
28 | mod.filename = filename;
|
29 |
|
30 | mod.paths = Module._nodeModulePaths(path.dirname(filename));
|
31 |
|
32 |
|
33 | mod._compile(`module.exports = require;`, filename);
|
34 |
|
35 | return mod.exports;
|
36 | };
|
37 |
|
38 |
|
39 | function isResolverValid(resolver) {
|
40 | if ('interfaceVersion' in resolver && resolver.interfaceVersion === 2) {
|
41 | return 'resolve' in resolver && !!resolver.resolve && typeof resolver.resolve === 'function';
|
42 | }
|
43 | return 'resolveImport' in resolver && !!resolver.resolveImport && typeof resolver.resolveImport === 'function';
|
44 | }
|
45 |
|
46 |
|
47 | function tryRequire(target, sourceFile) {
|
48 | let resolved;
|
49 | try {
|
50 |
|
51 | if (sourceFile != null) {
|
52 | try {
|
53 | resolved = createRequire(path.resolve(sourceFile)).resolve(target);
|
54 | } catch (e) {
|
55 | resolved = require.resolve(target);
|
56 | }
|
57 | } else {
|
58 | resolved = require.resolve(target);
|
59 | }
|
60 | } catch (e) {
|
61 |
|
62 | return undefined;
|
63 | }
|
64 |
|
65 |
|
66 | return require(resolved);
|
67 | }
|
68 |
|
69 |
|
70 | function resolverReducer(resolvers, map) {
|
71 | if (Array.isArray(resolvers)) {
|
72 | resolvers.forEach((r) => resolverReducer(r, map));
|
73 | return map;
|
74 | }
|
75 |
|
76 | if (typeof resolvers === 'string') {
|
77 | map.set(resolvers, null);
|
78 | return map;
|
79 | }
|
80 |
|
81 | if (typeof resolvers === 'object') {
|
82 | for (const key in resolvers) {
|
83 | map.set(key, resolvers[key]);
|
84 | }
|
85 | return map;
|
86 | }
|
87 |
|
88 | const err = new Error('invalid resolver config');
|
89 | err.name = ERROR_NAME;
|
90 | throw err;
|
91 | }
|
92 |
|
93 |
|
94 | function getBaseDir(sourceFile) {
|
95 | return pkgDir(sourceFile) || process.cwd();
|
96 | }
|
97 |
|
98 |
|
99 | function requireResolver(name, sourceFile) {
|
100 |
|
101 | const resolver = tryRequire(`eslint-import-resolver-${name}`, sourceFile)
|
102 | || tryRequire(name, sourceFile)
|
103 | || tryRequire(path.resolve(getBaseDir(sourceFile), name));
|
104 |
|
105 | if (!resolver) {
|
106 | const err = new Error(`unable to load resolver "${name}".`);
|
107 | err.name = ERROR_NAME;
|
108 | throw err;
|
109 | }
|
110 | if (!isResolverValid(resolver)) {
|
111 | const err = new Error(`${name} with invalid interface loaded as resolver`);
|
112 | err.name = ERROR_NAME;
|
113 | throw err;
|
114 | }
|
115 |
|
116 | return resolver;
|
117 | }
|
118 |
|
119 |
|
120 |
|
121 | exports.fileExistsWithCaseSync = function fileExistsWithCaseSync(filepath, cacheSettings, strict) {
|
122 |
|
123 | if (CASE_SENSITIVE_FS) { return true; }
|
124 |
|
125 |
|
126 | if (filepath === null) { return true; }
|
127 | if (filepath.toLowerCase() === process.cwd().toLowerCase() && !strict) { return true; }
|
128 | const parsedPath = path.parse(filepath);
|
129 | const dir = parsedPath.dir;
|
130 |
|
131 | let result = fileExistsCache.get(filepath, cacheSettings);
|
132 | if (result != null) { return result; }
|
133 |
|
134 |
|
135 | if (dir === '' || parsedPath.root === filepath) {
|
136 | result = true;
|
137 | } else {
|
138 | const filenames = fs.readdirSync(dir);
|
139 | if (filenames.indexOf(parsedPath.base) === -1) {
|
140 | result = false;
|
141 | } else {
|
142 | result = fileExistsWithCaseSync(dir, cacheSettings, strict);
|
143 | }
|
144 | }
|
145 | fileExistsCache.set(filepath, result);
|
146 | return result;
|
147 | };
|
148 |
|
149 |
|
150 | let prevSettings = null;
|
151 | let memoizedHash = '';
|
152 |
|
153 | function fullResolve(modulePath, sourceFile, settings) {
|
154 |
|
155 | const coreSet = new Set(settings['import/core-modules']);
|
156 | if (coreSet.has(modulePath)) { return { found: true, path: null }; }
|
157 |
|
158 | const sourceDir = path.dirname(sourceFile);
|
159 |
|
160 | if (prevSettings !== settings) {
|
161 | memoizedHash = hashObject(settings).digest('hex');
|
162 | prevSettings = settings;
|
163 | }
|
164 |
|
165 | const cacheKey = sourceDir + memoizedHash + modulePath;
|
166 |
|
167 | const cacheSettings = ModuleCache.getSettings(settings);
|
168 |
|
169 | const cachedPath = fileExistsCache.get(cacheKey, cacheSettings);
|
170 | if (cachedPath !== undefined) { return { found: true, path: cachedPath }; }
|
171 |
|
172 |
|
173 | function cache(resolvedPath) {
|
174 | fileExistsCache.set(cacheKey, resolvedPath);
|
175 | }
|
176 |
|
177 |
|
178 | function withResolver(resolver, config) {
|
179 | if (resolver.interfaceVersion === 2) {
|
180 | return resolver.resolve(modulePath, sourceFile, config);
|
181 | }
|
182 |
|
183 | try {
|
184 | const resolved = resolver.resolveImport(modulePath, sourceFile, config);
|
185 | if (resolved === undefined) { return { found: false }; }
|
186 | return { found: true, path: resolved };
|
187 | } catch (err) {
|
188 | return { found: false };
|
189 | }
|
190 | }
|
191 |
|
192 | const configResolvers = settings['import/resolver']
|
193 | || { node: settings['import/resolve'] };
|
194 |
|
195 | const resolvers = resolverReducer(configResolvers, new Map());
|
196 |
|
197 | for (const pair of resolvers) {
|
198 | const name = pair[0];
|
199 | const config = pair[1];
|
200 | const resolver = requireResolver(name, sourceFile);
|
201 | const resolved = withResolver(resolver, config);
|
202 |
|
203 | if (!resolved.found) { continue; }
|
204 |
|
205 |
|
206 | cache(resolved.path);
|
207 | return resolved;
|
208 | }
|
209 |
|
210 |
|
211 |
|
212 | return { found: false };
|
213 | }
|
214 |
|
215 |
|
216 | function relative(modulePath, sourceFile, settings) {
|
217 | return fullResolve(modulePath, sourceFile, settings).path;
|
218 | }
|
219 | exports.relative = relative;
|
220 |
|
221 |
|
222 | const erroredContexts = new Set();
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 | function resolve(p, context) {
|
232 | try {
|
233 | return relative(p, getPhysicalFilename(context), context.settings);
|
234 | } catch (err) {
|
235 | if (!erroredContexts.has(context)) {
|
236 |
|
237 |
|
238 |
|
239 | let errMessage = err.message;
|
240 |
|
241 | if (err.name !== ERROR_NAME && err.stack) {
|
242 |
|
243 | errMessage = err.stack.replace(/^Error: /, '');
|
244 | }
|
245 | context.report({
|
246 | message: `Resolve error: ${errMessage}`,
|
247 | loc: { line: 1, column: 0 },
|
248 | });
|
249 | erroredContexts.add(context);
|
250 | }
|
251 | }
|
252 | }
|
253 | resolve.relative = relative;
|
254 | exports.default = resolve;
|