eslint-plugin-clean-architecture
Version:
Enforce layering your code following Jason Taylor's Clean Architecture.
69 lines (68 loc) • 2.38 kB
JavaScript
class o {
withLayer(t) {
const a = {};
return a[t] = [], new c(a);
}
}
class c {
/**
* @param {LayerMap} layerMap
*/
constructor(t) {
this.layerMap = t;
}
/**
* @param {string} layer
* @param {string[]} dependsOn
*/
withLayer(t, a) {
if (this.layerMap.hasOwnProperty(t))
throw new Error(`Cannot add layer "${t}" as it has already been added`);
const r = a ?? [];
for (let e of r)
if (!this.layerMap.hasOwnProperty(e))
throw new Error(`Layer "${t}" cannot depend on layer "${e}" as layer "${e}" has not been added to the builder`);
const l = this.layerMap;
return l[t] = r, this;
}
/**
* @param {string} pluginName
*/
build(t) {
const a = [];
for (let r in this.layerMap) {
const l = this.layerMap[r], e = [];
for (let n in this.layerMap)
r !== n && (l.includes(n) || e.push({
group: [n],
message: `Do not import ${n} code into the ${r} layer`
}));
a.push({
files: [`**/${r}/**`],
rules: {
"no-restricted-imports": ["error", {
patterns: e
}]
}
});
}
return {
plugins: [t],
rules: {
"no-restricted-imports": "off"
},
overrides: a
};
}
}
const s = (i, t) => i && t ? new o().withLayer("3-domain").withLayer("2-application", ["3-domain"]).withLayer("1-infrastructure", ["3-domain", "2-application"]).withLayer("0-presentation", ["3-domain", "2-application", "1-infrastructure"]).build("clean-architecture") : i ? new o().withLayer("3-domain").withLayer("2-application", ["3-domain"]).withLayer("1-infrastructure", ["3-domain", "2-application"]).withLayer("1-presentation", ["3-domain", "2-application"]).build("clean-architecture") : t ? new o().withLayer("domain").withLayer("application", ["domain"]).withLayer("infrastructure", ["domain", "application"]).withLayer("presentation", ["domain", "application", "infrastructure"]).build("clean-architecture") : new o().withLayer("domain").withLayer("application", ["domain"]).withLayer("infrastructure", ["domain", "application"]).withLayer("presentation", ["domain", "application"]).build("clean-architecture"), p = {
configs: {
strict: s(!1, !1),
strictWithNumbers: s(!0, !1),
loose: s(!1, !0),
looseWithNumbers: s(!0, !0)
}
};
export {
p as default
};