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.
55 lines • 2.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.replaceBackSlashesWithForward = void 0;
const utils_1 = require("../utils");
const path_1 = require("path");
const replaceBackSlashesWithForward = (path) => !path ? "" : path.replace(/\\/g, "/");
exports.replaceBackSlashesWithForward = replaceBackSlashesWithForward;
function noAbsoluteImportCreate(context) {
return (0, utils_1.getImport)(context, ({ node, start, value: current, end, path, filename }) => {
if (current[0] != "/")
return;
let expected = (0, exports.replaceBackSlashesWithForward)((0, path_1.relative)((0, path_1.dirname)(filename), path));
if (!expected.startsWith("../")) {
expected = "./" + expected;
}
const data = {
current,
expected,
};
const fix = (fixer) => fixer.replaceTextRange([start + 1, end - 1], expected);
const descriptor = {
node,
messageId: "noAbsoluteImports",
data,
fix,
suggest: [
{
messageId: "replaceAbsoluteImport",
data,
fix,
},
],
};
context.report(descriptor);
});
}
const rule = {
meta: {
type: "problem",
docs: {
description: "disallow absolute imports of files where absolute is preferred",
url: "https://github.com/qDanik/eslint-plugin-path/blob/main/docs/rules/no-absolute-imports.md",
},
fixable: "code",
hasSuggestions: true,
schema: [{}],
messages: {
noAbsoluteImports: "Absolute import path '{{current}}' should be replaced with '{{expected}}'",
replaceAbsoluteImport: "Replace absolute import path '{{current}}' with absolute '{{expected}}'",
},
},
create: noAbsoluteImportCreate,
};
exports.default = rule;
//# sourceMappingURL=no-absolute-imports.js.map