eslint-plugin-path
Version:
An ESLint plugin for enforcing consistent imports across project. In other words, it helps to replace all relatives import with absolutes dependinng on settings.
40 lines • 1.37 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isRelativeToParent = isRelativeToParent;
exports.isExistingPath = isExistingPath;
exports.isExternalPath = isExternalPath;
const path_1 = require("path");
const fs_1 = require("fs");
const extensions = [".js", ".ts", ".jsx", ".tsx"];
const modulesFolder = "node_modules";
/**
* Check if the path is relative to the parent directory.
* @param path - The path to check.
* @returns True if the path is relative to the parent directory.
*/
function isRelativeToParent(path) {
return /^\.\.$|^\.\.[\\/]/.test(path);
}
/**
* Check if the given path exists.
* @param path - The path to check.
* @returns True if the path exists.
*/
function isExistingPath(path) {
const paths = [path, ...extensions.map((ext) => `${path}${ext}`), ...extensions.map((ext) => `${path}/index${ext}`)];
return paths.some((target) => (0, fs_1.existsSync)(target));
}
/**
* Check if the path is external.
* @param path - The path to check.
* @param packagePath - The package path to resolve against.
* @returns True if the path is external.
*/
function isExternalPath(path, packagePath) {
if (!path) {
return false;
}
const modulePath = (0, path_1.resolve)(packagePath, modulesFolder, path);
return isExistingPath(modulePath);
}
//# sourceMappingURL=import-types.js.map