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.
78 lines • 3.49 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getImport = getImport;
const path_1 = require("path");
const package_1 = require("./package");
const config_1 = require("./config");
const import_types_1 = require("./import-types");
/**
* Params configurator
* @param value - path to file
* @param config - settings from tsconfig.json/jsconfig.json
* @returns Returns array of paths
*/
const getConfigPaths = (value, config) => config.map(({ path }) => (0, path_1.join)(path, value));
/**
* Params configurator
* @param filename - The name of the file
* @param node - The node in the AST
* @param source - The source object containing value and range
* @param packagePath - The path to package.json
* @param configSettings - Settings from tsconfig.json/jsconfig.json
* @returns The configured ImportHandlerParams
*/
function configureParams(filename, node, // Consider specifying a more specific type
source, packagePath, configSettings) {
var _a, _b;
const value = (_b = (_a = source.value) === null || _a === void 0 ? void 0 : _a.toString()) !== null && _b !== void 0 ? _b : "";
let path = (0, path_1.normalize)((0, path_1.join)((0, path_1.dirname)(filename), value));
if (!(0, import_types_1.isExistingPath)(path)) {
path = getConfigPaths(value, configSettings).find(import_types_1.isExistingPath) || "";
}
const [start, end] = source.range || [0, 0]; // Default to [0, 0] if range is undefined
return {
node,
value: value,
path,
start,
end,
packagePath,
configSettings,
filename,
};
}
/**
* ESLint rule handler
* @param context - The ESLint rule context
* @param callback - The callback function to call
*/
function getImport(context, callback) {
var _a, _b;
const filename = context.filename;
const settings = ((_b = (_a = context.settings) === null || _a === void 0 ? void 0 : _a.path) !== null && _b !== void 0 ? _b : {});
if (!filename) {
return {};
}
const packagePath = (0, package_1.getPackagePath)(filename);
const configSettings = (0, config_1.getConfigSettings)(packagePath, settings);
return {
ImportDeclaration: (node) => {
callback(configureParams(filename, node, node.source, packagePath, configSettings));
},
CallExpression: (node) => {
var _a, _b, _c, _d, _e;
if (node.arguments.length > 0 &&
((_b = (_a = node === null || node === void 0 ? void 0 : node.arguments) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.type) === "Literal" &&
(((_c = node === null || node === void 0 ? void 0 : node.callee) === null || _c === void 0 ? void 0 : _c.type) === "ImportExpression" ||
(((_d = node === null || node === void 0 ? void 0 : node.callee) === null || _d === void 0 ? void 0 : _d.type) === "Identifier" && ((_e = node === null || node === void 0 ? void 0 : node.callee) === null || _e === void 0 ? void 0 : _e.name) === "require"))) {
callback(configureParams(filename, node, node.arguments[0], packagePath, configSettings));
}
},
ImportExpression: (node) => {
if (node.source.type === "Literal") {
callback(configureParams(filename, node, node.source, packagePath, configSettings));
}
},
};
}
//# sourceMappingURL=index.js.map