48 lines
1.3 KiB
JavaScript
48 lines
1.3 KiB
JavaScript
const TIERS = ["atoms", "molecules", "organisms", "templates", "pages"];
|
|
|
|
function tierOf(filepath) {
|
|
for (let i = 0; i < TIERS.length; i++) {
|
|
const tier = TIERS[i];
|
|
if (filepath.includes(`/${tier}/`) || filepath.includes(`\\${tier}\\`)) {
|
|
return { name: tier, index: i };
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/** @type {import("eslint").Rule.RuleModule} */
|
|
export default {
|
|
meta: {
|
|
type: "problem",
|
|
docs: {
|
|
description:
|
|
"Atomic-design tier imports respect direction: atoms ← molecules ← organisms ← templates ← pages.",
|
|
},
|
|
schema: [],
|
|
messages: {
|
|
wrongDirection:
|
|
"{{fromTier}} cannot import from {{toTier}} ({{importPath}}). Tier direction: atoms ← molecules ← organisms ← templates ← pages.",
|
|
},
|
|
},
|
|
create(context) {
|
|
const filename = context.filename;
|
|
const from = tierOf(filename);
|
|
if (!from) return {};
|
|
return {
|
|
ImportDeclaration(node) {
|
|
const source = node.source.value;
|
|
if (typeof source !== "string") return;
|
|
const to = tierOf(source);
|
|
if (!to) return;
|
|
if (to.index > from.index) {
|
|
context.report({
|
|
node,
|
|
messageId: "wrongDirection",
|
|
data: { fromTier: from.name, toTier: to.name, importPath: source },
|
|
});
|
|
}
|
|
},
|
|
};
|
|
},
|
|
};
|