2022-11-10 10:43:16 +00:00
|
|
|
'use strict';
|
|
|
|
exports.__esModule = true;
|
2020-08-25 23:57:08 +00:00
|
|
|
|
2022-11-10 10:43:16 +00:00
|
|
|
const fs = require('fs');
|
|
|
|
const Module = require('module');
|
|
|
|
const path = require('path');
|
2020-08-25 23:57:08 +00:00
|
|
|
|
2022-11-10 10:43:16 +00:00
|
|
|
const hashObject = require('./hash').hashObject;
|
|
|
|
const ModuleCache = require('./ModuleCache').default;
|
|
|
|
const pkgDir = require('./pkgDir').default;
|
2020-08-25 23:57:08 +00:00
|
|
|
|
2022-11-10 10:43:16 +00:00
|
|
|
const CASE_SENSITIVE_FS = !fs.existsSync(path.join(__dirname.toUpperCase(), 'reSOLVE.js'));
|
|
|
|
exports.CASE_SENSITIVE_FS = CASE_SENSITIVE_FS;
|
2020-08-25 23:57:08 +00:00
|
|
|
|
2022-11-10 10:43:16 +00:00
|
|
|
const ERROR_NAME = 'EslintPluginImportResolveError';
|
2020-08-25 23:57:08 +00:00
|
|
|
|
2022-11-10 10:43:16 +00:00
|
|
|
const fileExistsCache = new ModuleCache();
|
2020-08-25 23:57:08 +00:00
|
|
|
|
|
|
|
// Polyfill Node's `Module.createRequireFromPath` if not present (added in Node v10.12.0)
|
|
|
|
// Use `Module.createRequire` if available (added in Node v12.2.0)
|
|
|
|
const createRequire = Module.createRequire || Module.createRequireFromPath || function (filename) {
|
2022-11-10 10:43:16 +00:00
|
|
|
const mod = new Module(filename, null);
|
|
|
|
mod.filename = filename;
|
|
|
|
mod.paths = Module._nodeModulePaths(path.dirname(filename));
|
2020-08-25 23:57:08 +00:00
|
|
|
|
2022-11-10 10:43:16 +00:00
|
|
|
mod._compile(`module.exports = require;`, filename);
|
2020-08-25 23:57:08 +00:00
|
|
|
|
2022-11-10 10:43:16 +00:00
|
|
|
return mod.exports;
|
|
|
|
};
|
2020-08-25 23:57:08 +00:00
|
|
|
|
|
|
|
function tryRequire(target, sourceFile) {
|
2022-11-10 10:43:16 +00:00
|
|
|
let resolved;
|
2020-08-25 23:57:08 +00:00
|
|
|
try {
|
|
|
|
// Check if the target exists
|
|
|
|
if (sourceFile != null) {
|
|
|
|
try {
|
2022-11-10 10:43:16 +00:00
|
|
|
resolved = createRequire(path.resolve(sourceFile)).resolve(target);
|
2020-08-25 23:57:08 +00:00
|
|
|
} catch (e) {
|
2022-11-10 10:43:16 +00:00
|
|
|
resolved = require.resolve(target);
|
2020-08-25 23:57:08 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-11-10 10:43:16 +00:00
|
|
|
resolved = require.resolve(target);
|
2020-08-25 23:57:08 +00:00
|
|
|
}
|
2022-11-10 10:43:16 +00:00
|
|
|
} catch (e) {
|
2020-08-25 23:57:08 +00:00
|
|
|
// If the target does not exist then just return undefined
|
2022-11-10 10:43:16 +00:00
|
|
|
return undefined;
|
2020-08-25 23:57:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the target exists then return the loaded module
|
2022-11-10 10:43:16 +00:00
|
|
|
return require(resolved);
|
2020-08-25 23:57:08 +00:00
|
|
|
}
|
|
|
|
|
2022-11-10 10:43:16 +00:00
|
|
|
// https://stackoverflow.com/a/27382838
|
|
|
|
exports.fileExistsWithCaseSync = function fileExistsWithCaseSync(filepath, cacheSettings, strict) {
|
2020-08-25 23:57:08 +00:00
|
|
|
// don't care if the FS is case-sensitive
|
2022-11-10 10:43:16 +00:00
|
|
|
if (CASE_SENSITIVE_FS) return true;
|
2020-08-25 23:57:08 +00:00
|
|
|
|
|
|
|
// null means it resolved to a builtin
|
2022-11-10 10:43:16 +00:00
|
|
|
if (filepath === null) return true;
|
|
|
|
if (filepath.toLowerCase() === process.cwd().toLowerCase() && !strict) return true;
|
|
|
|
const parsedPath = path.parse(filepath);
|
|
|
|
const dir = parsedPath.dir;
|
2020-08-25 23:57:08 +00:00
|
|
|
|
2022-11-10 10:43:16 +00:00
|
|
|
let result = fileExistsCache.get(filepath, cacheSettings);
|
|
|
|
if (result != null) return result;
|
2020-08-25 23:57:08 +00:00
|
|
|
|
|
|
|
// base case
|
|
|
|
if (dir === '' || parsedPath.root === filepath) {
|
2022-11-10 10:43:16 +00:00
|
|
|
result = true;
|
2020-08-25 23:57:08 +00:00
|
|
|
} else {
|
2022-11-10 10:43:16 +00:00
|
|
|
const filenames = fs.readdirSync(dir);
|
2020-08-25 23:57:08 +00:00
|
|
|
if (filenames.indexOf(parsedPath.base) === -1) {
|
2022-11-10 10:43:16 +00:00
|
|
|
result = false;
|
2020-08-25 23:57:08 +00:00
|
|
|
} else {
|
2022-11-10 10:43:16 +00:00
|
|
|
result = fileExistsWithCaseSync(dir, cacheSettings, strict);
|
2020-08-25 23:57:08 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-10 10:43:16 +00:00
|
|
|
fileExistsCache.set(filepath, result);
|
|
|
|
return result;
|
|
|
|
};
|
2020-08-25 23:57:08 +00:00
|
|
|
|
|
|
|
function relative(modulePath, sourceFile, settings) {
|
2022-11-10 10:43:16 +00:00
|
|
|
return fullResolve(modulePath, sourceFile, settings).path;
|
2020-08-25 23:57:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function fullResolve(modulePath, sourceFile, settings) {
|
|
|
|
// check if this is a bonus core module
|
2022-11-10 10:43:16 +00:00
|
|
|
const coreSet = new Set(settings['import/core-modules']);
|
|
|
|
if (coreSet.has(modulePath)) return { found: true, path: null };
|
2020-08-25 23:57:08 +00:00
|
|
|
|
2022-11-10 10:43:16 +00:00
|
|
|
const sourceDir = path.dirname(sourceFile);
|
|
|
|
const cacheKey = sourceDir + hashObject(settings).digest('hex') + modulePath;
|
2020-08-25 23:57:08 +00:00
|
|
|
|
2022-11-10 10:43:16 +00:00
|
|
|
const cacheSettings = ModuleCache.getSettings(settings);
|
2020-08-25 23:57:08 +00:00
|
|
|
|
2022-11-10 10:43:16 +00:00
|
|
|
const cachedPath = fileExistsCache.get(cacheKey, cacheSettings);
|
|
|
|
if (cachedPath !== undefined) return { found: true, path: cachedPath };
|
2020-08-25 23:57:08 +00:00
|
|
|
|
|
|
|
function cache(resolvedPath) {
|
2022-11-10 10:43:16 +00:00
|
|
|
fileExistsCache.set(cacheKey, resolvedPath);
|
2020-08-25 23:57:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function withResolver(resolver, config) {
|
|
|
|
|
|
|
|
function v1() {
|
|
|
|
try {
|
2022-11-10 10:43:16 +00:00
|
|
|
const resolved = resolver.resolveImport(modulePath, sourceFile, config);
|
|
|
|
if (resolved === undefined) return { found: false };
|
|
|
|
return { found: true, path: resolved };
|
2020-08-25 23:57:08 +00:00
|
|
|
} catch (err) {
|
2022-11-10 10:43:16 +00:00
|
|
|
return { found: false };
|
2020-08-25 23:57:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function v2() {
|
2022-11-10 10:43:16 +00:00
|
|
|
return resolver.resolve(modulePath, sourceFile, config);
|
2020-08-25 23:57:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (resolver.interfaceVersion) {
|
2022-11-10 10:43:16 +00:00
|
|
|
case 2:
|
|
|
|
return v2();
|
2020-08-25 23:57:08 +00:00
|
|
|
|
2022-11-10 10:43:16 +00:00
|
|
|
default:
|
|
|
|
case 1:
|
|
|
|
return v1();
|
2020-08-25 23:57:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const configResolvers = (settings['import/resolver']
|
2022-11-10 10:43:16 +00:00
|
|
|
|| { 'node': settings['import/resolve'] }); // backward compatibility
|
2020-08-25 23:57:08 +00:00
|
|
|
|
2022-11-10 10:43:16 +00:00
|
|
|
const resolvers = resolverReducer(configResolvers, new Map());
|
2020-08-25 23:57:08 +00:00
|
|
|
|
2022-11-10 10:43:16 +00:00
|
|
|
for (const pair of resolvers) {
|
|
|
|
const name = pair[0];
|
|
|
|
const config = pair[1];
|
|
|
|
const resolver = requireResolver(name, sourceFile);
|
|
|
|
const resolved = withResolver(resolver, config);
|
2020-08-25 23:57:08 +00:00
|
|
|
|
2022-11-10 10:43:16 +00:00
|
|
|
if (!resolved.found) continue;
|
2020-08-25 23:57:08 +00:00
|
|
|
|
|
|
|
// else, counts
|
2022-11-10 10:43:16 +00:00
|
|
|
cache(resolved.path);
|
|
|
|
return resolved;
|
2020-08-25 23:57:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// failed
|
|
|
|
// cache(undefined)
|
2022-11-10 10:43:16 +00:00
|
|
|
return { found: false };
|
2020-08-25 23:57:08 +00:00
|
|
|
}
|
2022-11-10 10:43:16 +00:00
|
|
|
exports.relative = relative;
|
2020-08-25 23:57:08 +00:00
|
|
|
|
|
|
|
function resolverReducer(resolvers, map) {
|
2022-11-10 10:43:16 +00:00
|
|
|
if (Array.isArray(resolvers)) {
|
|
|
|
resolvers.forEach(r => resolverReducer(r, map));
|
|
|
|
return map;
|
2020-08-25 23:57:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof resolvers === 'string') {
|
2022-11-10 10:43:16 +00:00
|
|
|
map.set(resolvers, null);
|
|
|
|
return map;
|
2020-08-25 23:57:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof resolvers === 'object') {
|
2022-11-10 10:43:16 +00:00
|
|
|
for (const key in resolvers) {
|
|
|
|
map.set(key, resolvers[key]);
|
2020-08-25 23:57:08 +00:00
|
|
|
}
|
2022-11-10 10:43:16 +00:00
|
|
|
return map;
|
2020-08-25 23:57:08 +00:00
|
|
|
}
|
|
|
|
|
2022-11-10 10:43:16 +00:00
|
|
|
const err = new Error('invalid resolver config');
|
|
|
|
err.name = ERROR_NAME;
|
|
|
|
throw err;
|
2020-08-25 23:57:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getBaseDir(sourceFile) {
|
2022-11-10 10:43:16 +00:00
|
|
|
return pkgDir(sourceFile) || process.cwd();
|
2020-08-25 23:57:08 +00:00
|
|
|
}
|
|
|
|
function requireResolver(name, sourceFile) {
|
|
|
|
// Try to resolve package with conventional name
|
2022-11-10 10:43:16 +00:00
|
|
|
const resolver = tryRequire(`eslint-import-resolver-${name}`, sourceFile) ||
|
2020-08-25 23:57:08 +00:00
|
|
|
tryRequire(name, sourceFile) ||
|
2022-11-10 10:43:16 +00:00
|
|
|
tryRequire(path.resolve(getBaseDir(sourceFile), name));
|
2020-08-25 23:57:08 +00:00
|
|
|
|
|
|
|
if (!resolver) {
|
2022-11-10 10:43:16 +00:00
|
|
|
const err = new Error(`unable to load resolver "${name}".`);
|
|
|
|
err.name = ERROR_NAME;
|
|
|
|
throw err;
|
2020-08-25 23:57:08 +00:00
|
|
|
}
|
|
|
|
if (!isResolverValid(resolver)) {
|
2022-11-10 10:43:16 +00:00
|
|
|
const err = new Error(`${name} with invalid interface loaded as resolver`);
|
|
|
|
err.name = ERROR_NAME;
|
|
|
|
throw err;
|
2020-08-25 23:57:08 +00:00
|
|
|
}
|
|
|
|
|
2022-11-10 10:43:16 +00:00
|
|
|
return resolver;
|
2020-08-25 23:57:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function isResolverValid(resolver) {
|
|
|
|
if (resolver.interfaceVersion === 2) {
|
2022-11-10 10:43:16 +00:00
|
|
|
return resolver.resolve && typeof resolver.resolve === 'function';
|
2020-08-25 23:57:08 +00:00
|
|
|
} else {
|
2022-11-10 10:43:16 +00:00
|
|
|
return resolver.resolveImport && typeof resolver.resolveImport === 'function';
|
2020-08-25 23:57:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-10 10:43:16 +00:00
|
|
|
const erroredContexts = new Set();
|
2020-08-25 23:57:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Given
|
|
|
|
* @param {string} p - module path
|
|
|
|
* @param {object} context - ESLint context
|
|
|
|
* @return {string} - the full module filesystem path;
|
|
|
|
* null if package is core;
|
|
|
|
* undefined if not found
|
|
|
|
*/
|
|
|
|
function resolve(p, context) {
|
|
|
|
try {
|
2022-11-10 10:43:16 +00:00
|
|
|
return relative(p, context.getPhysicalFilename ? context.getPhysicalFilename() : context.getFilename(), context.settings);
|
2020-08-25 23:57:08 +00:00
|
|
|
} catch (err) {
|
|
|
|
if (!erroredContexts.has(context)) {
|
|
|
|
// The `err.stack` string starts with `err.name` followed by colon and `err.message`.
|
|
|
|
// We're filtering out the default `err.name` because it adds little value to the message.
|
2022-11-10 10:43:16 +00:00
|
|
|
let errMessage = err.message;
|
2020-08-25 23:57:08 +00:00
|
|
|
if (err.name !== ERROR_NAME && err.stack) {
|
2022-11-10 10:43:16 +00:00
|
|
|
errMessage = err.stack.replace(/^Error: /, '');
|
2020-08-25 23:57:08 +00:00
|
|
|
}
|
|
|
|
context.report({
|
|
|
|
message: `Resolve error: ${errMessage}`,
|
|
|
|
loc: { line: 1, column: 0 },
|
2022-11-10 10:43:16 +00:00
|
|
|
});
|
|
|
|
erroredContexts.add(context);
|
2020-08-25 23:57:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-10 10:43:16 +00:00
|
|
|
resolve.relative = relative;
|
|
|
|
exports.default = resolve;
|