-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from feature-sliced/feature-public-api
LINT-14: Public API boundaries (private imports)
- Loading branch information
Showing
9 changed files
with
171 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const FS_LAYERS = [ | ||
"app", | ||
"processes", | ||
"pages", | ||
"widgets", | ||
"features", | ||
"entities", | ||
"shared", | ||
]; | ||
|
||
const FS_SEGMENTS = [ | ||
"ui", | ||
"model", | ||
"lib", | ||
"styles", | ||
"api", | ||
"config", | ||
]; | ||
|
||
const getLowerLayers = (layer) => FS_LAYERS.slice(FS_LAYERS.indexOf(layer) + 1); | ||
const getUpperLayers = (layer) => FS_LAYERS.slice(0, FS_LAYERS.indexOf(layer)); | ||
|
||
module.exports = { FS_LAYERS, FS_SEGMENTS, getLowerLayers, getUpperLayers }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,12 @@ | ||
const { getLayersBoundariesElements, getLayersRules} = require("./layers") | ||
const path = require("path"); | ||
|
||
module.exports = { | ||
parserOptions: { | ||
"ecmaVersion": "2015", | ||
"sourceType": "module", | ||
}, | ||
plugins: ["boundaries"], | ||
extends: ["plugin:boundaries/recommended"], | ||
ignorePatterns: [".eslintrc.js"], | ||
settings: { | ||
"boundaries/elements": getLayersBoundariesElements(), | ||
}, | ||
rules: { | ||
"boundaries/element-types": [ | ||
2, | ||
{ | ||
"default": "disallow", | ||
"message": "\"${file.type}\" is not allowed to import \"${dependency.type}\" | See rules: https://feature-sliced.design/docs/reference/layers/overview ", | ||
"rules": getLayersRules(), | ||
} | ||
], | ||
}, | ||
extends: [ | ||
path.resolve(__dirname, "./public-api-boundaries.js"), | ||
path.resolve(__dirname, "./layers-slices-boundaries.js") | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const { getLowerLayers, FS_LAYERS } = require("./helpers"); | ||
|
||
const getLayersRules = () => | ||
FS_LAYERS.map((layer) => ({ | ||
from: layer, | ||
allow: getLowerLayers(layer), | ||
})); | ||
|
||
const getLayersBoundariesElements = () => | ||
FS_LAYERS.map((layer) => ({ | ||
type: layer, | ||
pattern: `${layer}/*`, | ||
mode: "folder", | ||
capture: ["slices"], | ||
})); | ||
|
||
module.exports = { | ||
parserOptions: { | ||
"ecmaVersion": "2015", | ||
"sourceType": "module", | ||
}, | ||
plugins: ["boundaries"], | ||
extends: ["plugin:boundaries/recommended"], | ||
ignorePatterns: [".eslintrc.js"], | ||
settings: { | ||
"boundaries/elements": getLayersBoundariesElements(), | ||
}, | ||
rules: { | ||
"boundaries/element-types": [ | ||
2, | ||
{ | ||
"default": "disallow", | ||
"message": "\"${file.type}\" is not allowed to import \"${dependency.type}\" | See rules: https://feature-sliced.design/docs/reference/layers/overview ", | ||
"rules": getLayersRules(), | ||
}, | ||
], | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
const { getUpperLayers, FS_SEGMENTS, FS_LAYERS } = require("./helpers"); | ||
|
||
const FS_SEGMENTS_REG = FS_SEGMENTS.join("|"); | ||
const FS_LAYERS_REG = FS_LAYERS.join("|"); | ||
|
||
module.exports = { | ||
parserOptions: { | ||
"ecmaVersion": "2015", | ||
"sourceType": "module", | ||
}, | ||
plugins: ["import"], | ||
rules: { | ||
"import/no-internal-modules": [ | ||
"error", { | ||
"allow": [ | ||
/** | ||
* Allow not segments import from slices | ||
* @example | ||
* 'entities/form/ui' // Fail | ||
* 'entities/form' // Pass | ||
*/ | ||
`**/*(${getUpperLayers("shared").join("|")})/!(${FS_SEGMENTS_REG})`, | ||
|
||
/** | ||
* Allow slices with structure grouping | ||
* @example | ||
* 'features/auth/form' // Pass | ||
*/ | ||
`**/*(${FS_LAYERS_REG})/!(${FS_SEGMENTS_REG})/!(${FS_SEGMENTS_REG})`, | ||
|
||
/** | ||
* Allow not segments import in shared segments | ||
* @example | ||
* 'shared/ui/button' // Pass | ||
*/ | ||
`**/shared/*(${FS_SEGMENTS_REG})/!(${FS_SEGMENTS_REG})`, | ||
|
||
/** | ||
* Allow import from segments in shared | ||
* @example | ||
* 'shared/ui' // Pass | ||
*/ | ||
`**/shared/*(${FS_SEGMENTS_REG})`, | ||
], | ||
}], | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
const { ESLint } = require("eslint"); | ||
const assert = require("assert"); | ||
const { mockImports } = require("./utils/mock-import"); | ||
const cfg = require("../public-api-boundaries"); | ||
|
||
const eslint = new ESLint({ | ||
useEslintrc: false, | ||
baseConfig: mockImports(cfg), | ||
}); | ||
|
||
describe("PublicAPI import boundaries:", () => { | ||
it("Should lint PublicAPI boundaries with errors.", async () => { | ||
const wrongImports = [ | ||
`import { Issues } from "pages/issues/ui";`, | ||
`import { IssueDetails } from "widgets/issue-details/ui/details"`, | ||
`import { AuthForm } from "features/auth-form/ui/form"`, | ||
`import { Button } from "shared/ui/button/button";`, | ||
`import { saveOrder } from "entities/order/model/actions";`, | ||
`import { orderModel } from "entities/order/model";`, | ||
`import { TicketCard } from "@app/entities/ticket/ui";`, | ||
]; | ||
|
||
const report = await eslint.lintText(wrongImports.join("\n"), | ||
{ filePath: "src/app/ui/index.js" }); | ||
assert.strictEqual(report[0].errorCount, wrongImports.length); | ||
}); | ||
|
||
it("Should lint PublicAPI boundaries without errors.", async () => { | ||
const validCodeSnippet = [ | ||
`import { Issues } from "pages/issues";`, | ||
`import { GoodIssues } from "@app/pages/issues";`, | ||
`import { IssueDetails } from "widgets/issue-details"`, | ||
`import { AuthForm } from "features/auth-form"`, | ||
`import { Button } from "shared/ui/button";`, | ||
`import { orderModel } from "entities/order";`, | ||
`import { TicketCard } from "@/entities/ticket";`, | ||
`import { FixButton } from "@app/shared/ui/fix-button";`, | ||
].join("\n"); | ||
|
||
const report = await eslint.lintText(validCodeSnippet, { filePath: "src/app/ui/index.js" }); | ||
assert.strictEqual(report[0].errorCount, 0); | ||
}); | ||
|
||
it("Should lint extra PublicAPI boundaries cases without errors.", async () => { | ||
const validCodeSnippet = [ | ||
`import { AuthForm } from "features/auth/form"`, | ||
`import { Button } from "shared/ui";`, | ||
].join("\n"); | ||
|
||
const report = await eslint.lintText(validCodeSnippet, { filePath: "src/app/ui/index.js" }); | ||
assert.strictEqual(report[0].errorCount, 0); | ||
}); | ||
|
||
}); |
File renamed without changes.
1 change: 1 addition & 0 deletions
1
test/utils/mock-resolver.js → test/utils/mock-import/mock-resolver.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters