Initial commit
This commit is contained in:
47
packages/core-eslint/rules/atomic-tier-import-direction.js
Normal file
47
packages/core-eslint/rules/atomic-tier-import-direction.js
Normal file
@@ -0,0 +1,47 @@
|
||||
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 },
|
||||
});
|
||||
}
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user