Skip to content

LINT-45: Split layers-slices-boundaries #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 16 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ module.exports = {
"sourceType": "module",
},
extends: [
path.resolve(__dirname, "./rules/public-api-boundaries"),
path.resolve(__dirname, "./rules/layers-slices-boundaries"),
path.resolve(__dirname, "./rules/import-order")
],
"./rules/public-api-boundaries",
"./rules/slices-and-layers-boundaries",
"./rules/import-order",
].map(require.resolve),
};
1 change: 0 additions & 1 deletion rules/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ describe("Integration tests:", () => {
`, {
filePath: "src/widgets/mock/index.js",
});

assert.strictEqual(report[0].errorCount, 11);
});

Expand Down
34 changes: 34 additions & 0 deletions rules/layers-boundaries/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const { layersLib } = require("../../utils");

const getLayersRules = () =>
layersLib.FS_LAYERS.map((layer) => ({
from: layer,
disallow: layersLib.getUpperLayers(layer),
}));

const getLayersBoundariesElements = () =>
layersLib.FS_LAYERS.map((layer) => ({
type: layer,
pattern: `${layer}/**`,
mode: "folder",
capture: ["layers"],
}));

module.exports = {
plugins: ["boundaries"],
extends: ["plugin:boundaries/recommended"],
ignorePatterns: [".eslintrc.js"],
settings: {
"boundaries/elements": getLayersBoundariesElements(),
},
rules: {
"boundaries/element-types": [
2,
{
"default": "allow",
"message": "\"${file.type}\" is not allowed to import \"${dependency.type}\" | See rules: https://feature-sliced.design/docs/reference/layers/overview ",
"rules": getLayersRules(),
},
],
},
};
16 changes: 16 additions & 0 deletions rules/layers-boundaries/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# @feature-sliced/layers-slices-boundaries

Reference: [Cross-communication](https://feature-sliced.design/docs/concepts/cross-communication)

```js
// 👎 Fail
// 🛣 features/auth-form/index.ts
import { getRoute } from "pages/auth";
import { getStore } from "app/store";


// 👍 Pass
// 🛣 features/auth-form/index.ts
import { Form, Button } from "shared/ui";
import { getAuthCtx } from "entities/session";
```
67 changes: 0 additions & 67 deletions rules/layers-slices-boundaries/slices.test.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
const { layersLib } = require("../../utils");

const getLayersRules = () =>

layersLib.FS_LAYERS.map((layer) => ({
from: layer,
allow: layersLib.getLowerLayers(layer),
}));

const getLayersBoundariesElements = () =>
const getAllBoundariesElements = () =>
layersLib.FS_LAYERS.map((layer) => ({
type: layer,
pattern: `${layer}/*`,
Expand All @@ -20,7 +19,7 @@ module.exports = {
extends: ["plugin:boundaries/recommended"],
ignorePatterns: [".eslintrc.js"],
settings: {
"boundaries/elements": getLayersBoundariesElements(),
"boundaries/elements": getAllBoundariesElements(),
},
rules: {
"boundaries/element-types": [
Expand All @@ -32,4 +31,4 @@ module.exports = {
},
],
},
};
};
89 changes: 89 additions & 0 deletions rules/slices-and-layers-boundaries/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
const { ESLint } = require("eslint");
const assert = require("assert");
const { configLib } = require("../../utils");
const cfg = require("./");

const eslint = new ESLint({
useEslintrc: false,
baseConfig: configLib.setParser(
configLib.mockImports(cfg),
),
});

describe("Slices and Layers config:", () => {

describe("Layers:", () => {
it("should lint with cross-import errors.", async () => {
const wrongImports = [
`import { getRoute } from "pages/auth";`,
`import { getStore } from "app/store";`,
];

const report = await eslint.lintText(wrongImports.join("\n"), {
filePath: "src/shared/lib/index.js",
});
assert.strictEqual(report[0].errorCount, wrongImports.length);
});

it("should lint without errors.", async () => {
const validCodeSnippet = [
`import { sessionModel } from "entities/session";`,
`import { Form, Button } from "shared/ui";`,
].join("\n");

const report = await eslint.lintText(validCodeSnippet, {
filePath: "src/app/ui/app.js",
});
assert.strictEqual(report[0].errorCount, 0);
});
});

describe("Slices:", () => {
it("should lint with cross-import errors between pages.", async () => {
const report = await eslint.lintText(`
import { getAuthCtx } from "pages/logout";
import { UserAvatar } from "pages/auth";`,
{ filePath: "src/pages/map/index.js" });

assert.strictEqual(report[0].errorCount, 2);
});

it("should lint with cross-import errors between widgets.", async () => {
const report = await eslint.lintText(`
import { HeaderTitle } from "widgets/header";
import { Links } from "widgets/footer";`,
{ filePath: "src/widgets/mock/index.js" });

assert.strictEqual(report[0].errorCount, 2);
});

it("should lint with cross-import errors between features.", async () => {
const report = await eslint.lintText(`
import { getAuthCtx } from "features/logout";
import { UserAvatar } from "features/viewer-picker";`,
{ filePath: "src/features/auth-form/index.js" });

assert.strictEqual(report[0].errorCount, 2);
});

it("should lint with cross-import errors between entities.", async () => {
const report = await eslint.lintText(`
import { LoginForm } from "entities/login-form";
import { Avatar } from "entities/avatar";`,
{ filePath: "src/entities/mock/index.js" });

assert.strictEqual(report[0].errorCount, 2);
});

it("should lint without error", async () => {
const report = await eslint.lintText(`
import { model } from "../model";
import { styles } from "./style.js";
import { utils } from "../lib/utils.js";`,
{ filePath: "src/entities/mock/index.js" });

assert.strictEqual(report[0].errorCount, 0);
});

});
});
34 changes: 34 additions & 0 deletions rules/slices-boundaries/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const { layersLib } = require("../../utils");

const getSlicesRules = () =>
layersLib.FS_LAYERS.map((layer) => ({
from: layer,
disallow: layer,
}));

const getSlicesBoundariesElements = () =>
layersLib.FS_LAYERS.map((layer) => ({
type: layer,
pattern: `${layer}`,
mode: "folder",
capture: ["slices"],
}));

module.exports = {
plugins: ["boundaries"],
extends: ["plugin:boundaries/recommended"],
ignorePatterns: [".eslintrc.js"],
settings: {
"boundaries/elements": getSlicesBoundariesElements(),
},
rules: {
"boundaries/element-types": [
2,
{
"default": "allow",
"message": "\"${file.type}\" is not allowed to import \"${dependency.type}\" | See rules: https://feature-sliced.design/docs/concepts/app-splitting#group-slices ",
"rules": getSlicesRules(),
},
],
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@ Reference: [Cross-communication](https://feature-sliced.design/docs/concepts/cro
```js
// 👎 Fail
// 🛣 features/auth-form/index.ts
import { getRoute } from "pages/auth";
import { getStore } from "app/store";
import { getAuthCtx } from "features/logout";
import { UserAvatar } from "features/viewer-picker";

// 👍 Pass
// 🛣 features/auth-form/index.ts
import { sessionModel } from "entities/session";
import { Form, Button } from "shared/ui";
import { getAuthCtx } from "entities/session";
import { UserAvatar } from "entities/user";
```
61 changes: 61 additions & 0 deletions rules/slices-boundaries/slices.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const { ESLint } = require("eslint");
const assert = require("assert");
const { configLib } = require("../../utils");
const cfg = require("./");

const eslint = new ESLint({
useEslintrc: false,
baseConfig: configLib.setParser(
configLib.mockImports(cfg),
),
});

describe("Import boundaries between slices", () => {

it("should lint with cross-import errors between pages.", async () => {
const report = await eslint.lintText(`
import { getAuthCtx } from "pages/logout";
import { UserAvatar } from "pages/auth";`,
{ filePath: "src/pages/map/index.js" });

assert.strictEqual(report[0].errorCount, 2);
});

it("should lint with cross-import errors between widgets.", async () => {
const report = await eslint.lintText(`
import { HeaderTitle } from "widgets/header";
import { Links } from "widgets/footer";`,
{ filePath: "src/widgets/mock/index.js" });

assert.strictEqual(report[0].errorCount, 2);
});

it("should lint with cross-import errors between features.", async () => {
const report = await eslint.lintText(`
import { getAuthCtx } from "features/logout";
import { UserAvatar } from "features/viewer-picker";`,
{ filePath: "src/features/auth-form/index.js" });

assert.strictEqual(report[0].errorCount, 2);
});

it("should lint with cross-import errors between entities.", async () => {
const report = await eslint.lintText(`
import { LoginForm } from "entities/login-form";
import { Avatar } from "entities/avatar";`,
{ filePath: "src/entities/mock/index.js" });

assert.strictEqual(report[0].errorCount, 2);
});

it("should lint without error", async () => {
const report = await eslint.lintText(`
import { model } from "../model";
import { styles } from "./style.js";
import { utils } from "../lib/utils.js";`,
{ filePath: "src/entities/mock/index.js" });

assert.strictEqual(report[0].errorCount, 0);
});

});
12 changes: 8 additions & 4 deletions utils/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ const mockImports = (config, extension = "js") => {
extension,
},
},
}
}
}
},
};
};

function setParser (config, version = "2015") {
return {
Expand All @@ -31,4 +31,8 @@ function setTSParser (config) {
};
}

module.exports.configLib = { mockImports, setParser, setTSParser };
module.exports.configLib = {
mockImports,
setParser,
setTSParser,
};