eslint-plugin-no-barrel-files
Version:
ESLint plugin for reducing barrel-file usage in two ways:
92 lines • 4.19 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const node_path_1 = __importDefault(require("node:path"));
const utils_1 = require("@typescript-eslint/utils");
const minimatch_1 = require("minimatch");
function isAllowedBarrel(filename, options) {
if (!(options === null || options === void 0 ? void 0 : options.allow)) {
return false;
}
const normalizedFilename = filename.split(node_path_1.default.sep).join('/');
const relativeFilename = node_path_1.default.relative(process.cwd(), filename).split(node_path_1.default.sep).join('/');
return options.allow.some(pattern => (0, minimatch_1.minimatch)(relativeFilename, pattern, { dot: true }) || (0, minimatch_1.minimatch)(normalizedFilename, pattern, { dot: true }));
}
const noBarrelFiles = {
defaultOptions: [{}],
meta: {
type: 'suggestion',
docs: {
url: 'https://github.com/art0rz/eslint-plugin-no-barrel-files',
description: 'disallow barrel files',
},
schema: [
{
type: 'object',
properties: { allow: { type: 'array', items: { type: 'string' } } },
additionalProperties: false,
},
],
messages: {
noReExport: 'Do not re-export imported variable (`{{name}}`)',
noExportAll: 'Do not use export all (`export * from ...`)',
},
},
create(context) {
const [options] = context.options;
return {
'Program:exit'(program) {
if (isAllowedBarrel(context.filename, options))
return;
const declaredImports = new Set(program.body.flatMap(statement => statement.type === utils_1.AST_NODE_TYPES.ImportDeclaration
? statement.specifiers.map(specifier => specifier.local.name)
: []));
program.body.forEach(node => {
var _a;
if (node.type === utils_1.AST_NODE_TYPES.ExportDefaultDeclaration) {
if (node.declaration.type === utils_1.AST_NODE_TYPES.Identifier && declaredImports.has(node.declaration.name)) {
context.report({
node,
messageId: 'noReExport',
data: { name: node.declaration.name },
});
}
return;
}
if (node.type === utils_1.AST_NODE_TYPES.ExportAllDeclaration) {
context.report({
node,
messageId: 'noExportAll',
});
return;
}
if (node.type !== utils_1.AST_NODE_TYPES.ExportNamedDeclaration) {
return;
}
if (((_a = node.source) === null || _a === void 0 ? void 0 : _a.type) === 'Literal') {
context.report({
node,
messageId: 'noReExport',
data: { name: node.source.value },
});
}
node.specifiers.forEach(specifier => {
if (specifier.local.type === utils_1.AST_NODE_TYPES.Identifier &&
specifier.exported.type === utils_1.AST_NODE_TYPES.Identifier &&
declaredImports.has(specifier.local.name)) {
context.report({
node: specifier,
messageId: 'noReExport',
data: { name: specifier.exported.name },
});
}
});
});
},
};
},
};
exports.default = noBarrelFiles;
//# sourceMappingURL=no-barrel-files.js.map