eslint-plugin-ferramentas
Version:
A bundle of useful ESLint rules
49 lines (48 loc) • 2.08 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.rule = void 0;
const utils_1 = require("../../utils");
const Base_1 = require("../Base");
const Debug_1 = require("../Debug");
const Input_1 = require("./Input");
const Metadata_1 = require("./Metadata");
const rule = (0, Base_1.createRule)({
name: Metadata_1.name,
type: 'problem',
docs: { description: 'Prevents importing of files in specific folders from other specified locations of the codebase through regex.' },
schema: Metadata_1.schema,
messages: { fobiddenImport: "Importing of '{{rawPath}}' is forbidden on '{{relativeFileName}}'" },
}, (input) => ({ ...(0, Input_1.mapDirectories)(input), ...(0, Debug_1.mapDebugRuleOptionInput)(input) }), (context) => {
const relativeFileName = context.getRelativeFilename();
context.debug('relativeFileName', relativeFileName);
const forbidden = context
.getOption('directories')
.reduce((r, { filter, forbid }) => (new RegExp(filter).test(relativeFileName) ? [...r, ...forbid.map((f) => new RegExp(f))] : r), []);
context.debug('forbidden', ...forbidden);
if (forbidden.length === 0) {
/**
* There is nothing to forbid
* So don't return any declaration
*/
return {};
}
return {
ImportDeclaration: (node) => {
const declaration = context.getImportDeclaration(node);
context.debug('declaration', declaration);
const isRelative = (0, utils_1.isPathRelative)(declaration.rawPath);
context.debug('isRelative', isRelative);
if (!isRelative) {
return;
}
if (forbidden.some((forbid) => forbid.test(declaration.pathFromWorkingDirectory))) {
context.report({
loc: declaration.getLocation(false),
messageId: 'fobiddenImport',
data: { rawPath: declaration.rawPath, relativeFileName },
});
}
},
};
});
exports.rule = rule;