Skip to content

Commit

Permalink
feat(core): update CLI with moduleType information
Browse files Browse the repository at this point in the history
- add `moduleType` to CLI export
- rename "Deep Imports" to "Encapsulation Violations" in CLI verify
  • Loading branch information
rainerhahnekamp authored Oct 19, 2024
1 parent 3a4f132 commit 16bbf6e
Show file tree
Hide file tree
Showing 12 changed files with 2,788 additions and 17 deletions.
40 changes: 40 additions & 0 deletions .detective/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"scopes": [
"packages/core/src/lib/api",
"packages/core/src/lib/checks",
"packages/core/src/lib/cli",
"packages/core/src/lib/config",
"packages/core/src/lib/error",
"packages/core/src/lib/eslint",
"packages/core/src/lib/file-info",
"packages/core/src/lib/fs",
"packages/core/src/lib/log",
"packages/core/src/lib/main",
"packages/core/src/lib/modules",
"packages/core/src/lib/tags",
"packages/core/src/lib/util"
],
"groups": [
"packages/core/src/lib",
"packages/core/src"
],
"entries": [
"packages/core/src/index.ts",
"packages/core/src/bin/main.ts"
],
"filter": {
"files": [],
"logs": []
},
"aliases": {},
"teams": {
"example-team-a": [
"John Doe",
"Jane Doe"
],
"example-team-b": [
"Max Muster",
"Susi Sorglos"
]
}
}
1 change: 1 addition & 0 deletions .detective/hash
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
687b14f18979daf3cb871b8c11921168672abe82, v1.2.1
2,517 changes: 2,517 additions & 0 deletions .detective/log

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions docs/docs/release-notes/0.18.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,8 @@ The `tagging` property still works, but we recommend renaming it to `modules`. N
## Renaming of "Deep Import" to "Encapsulation Violation"

Since in barrel-less modules, every access is a deep import but can still be valid, the ESLint rule "Deep Import" has been renamed to "Encapsulation Violation". The "Deep Import" rule still works but is deprecated.

## CLI

- `verify` uses now the term "Encapsulation Violations" instead of "Deep Imports".
- `export` includes the module type. Even if barrel-less modules are disabled, root is marked as barrel-less.
3 changes: 3 additions & 0 deletions packages/core/src/lib/api/get-project-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import getFs from '../fs/getFs';

export type ProjectDataEntry = {
module: string;
moduleType: 'barrel' | 'barrel-less',
tags: string[];
imports: string[];
externalLibraries?: string[];
Expand Down Expand Up @@ -141,6 +142,7 @@ export function getProjectData(
for (const { fileInfo } of traverseFileInfo(projectInfo.fileInfo)) {
const entry: ProjectDataEntry = {
module: fileInfo.moduleInfo.path || '.',
moduleType: fileInfo.moduleInfo.hasBarrel ? 'barrel' : 'barrel-less',
tags: calcOrGetTags(
fileInfo.moduleInfo.path,
projectInfo,
Expand Down Expand Up @@ -177,6 +179,7 @@ function relativizeIfRequired(
for (const [modulePath, moduleData] of Object.entries(data)) {
const entry: ProjectDataEntry = {
module: relative(toFsPath(moduleData.module)),
moduleType: moduleData.moduleType,
tags: moduleData.tags,
imports: moduleData.imports.map((importPath) =>
relative(toFsPath(importPath)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ exports[`export data > should also work with a sheriff.config.ts > sheriff-confi
"{
"src/main.ts": {
"module": ".",
"moduleType": "barrel-less",
"tags": [
"root"
],
Expand All @@ -15,6 +16,7 @@ exports[`export data > should also work with a sheriff.config.ts > sheriff-confi
},
"src/holidays/index.ts": {
"module": "src/holidays",
"moduleType": "barrel",
"tags": [
"scope:holidays"
],
Expand All @@ -23,6 +25,7 @@ exports[`export data > should also work with a sheriff.config.ts > sheriff-confi
},
"src/customers/index.ts": {
"module": "src/customers",
"moduleType": "barrel",
"tags": [
"scope:customers"
],
Expand All @@ -36,6 +39,7 @@ exports[`export data > should avoid circular dependencies > circular-dependencie
"{
"src/main.ts": {
"module": ".",
"moduleType": "barrel-less",
"tags": [
"root"
],
Expand All @@ -47,6 +51,7 @@ exports[`export data > should avoid circular dependencies > circular-dependencie
},
"src/app1.service.ts": {
"module": ".",
"moduleType": "barrel-less",
"tags": [
"root"
],
Expand All @@ -57,6 +62,7 @@ exports[`export data > should avoid circular dependencies > circular-dependencie
},
"src/app2.service.ts": {
"module": ".",
"moduleType": "barrel-less",
"tags": [
"root"
],
Expand All @@ -72,6 +78,7 @@ exports[`export data > should skip not reachable files > not-reachable-files 1`]
"{
"src/main.ts": {
"module": ".",
"moduleType": "barrel-less",
"tags": [
"root"
],
Expand All @@ -82,6 +89,7 @@ exports[`export data > should skip not reachable files > not-reachable-files 1`]
},
"src/app1.service.ts": {
"module": ".",
"moduleType": "barrel-less",
"tags": [
"root"
],
Expand All @@ -95,6 +103,7 @@ exports[`export data > should test a simple application > simple-application 1`]
"{
"src/main.ts": {
"module": ".",
"moduleType": "barrel-less",
"tags": [
"root"
],
Expand All @@ -105,6 +114,7 @@ exports[`export data > should test a simple application > simple-application 1`]
},
"src/holidays/feature/index.ts": {
"module": "src/holidays/feature",
"moduleType": "barrel",
"tags": [
"domain:holidays",
"type:feature"
Expand All @@ -116,6 +126,7 @@ exports[`export data > should test a simple application > simple-application 1`]
},
"src/holidays/feature/holidays-container.component.ts": {
"module": "src/holidays/feature",
"moduleType": "barrel",
"tags": [
"domain:holidays",
"type:feature"
Expand All @@ -133,6 +144,7 @@ exports[`export data > should test a simple application > simple-application 1`]
},
"src/holidays/data/index.ts": {
"module": "src/holidays/data",
"moduleType": "barrel",
"tags": [
"domain:holidays",
"type:data"
Expand All @@ -144,6 +156,7 @@ exports[`export data > should test a simple application > simple-application 1`]
},
"src/holidays/data/holidays-store.ts": {
"module": "src/holidays/data",
"moduleType": "barrel",
"tags": [
"domain:holidays",
"type:data"
Expand All @@ -157,6 +170,7 @@ exports[`export data > should test a simple application > simple-application 1`]
},
"src/holidays/model/index.ts": {
"module": "src/holidays/model",
"moduleType": "barrel",
"tags": [
"domain:holidays",
"type:model"
Expand All @@ -168,6 +182,7 @@ exports[`export data > should test a simple application > simple-application 1`]
},
"src/holidays/model/holiday.ts": {
"module": "src/holidays/model",
"moduleType": "barrel",
"tags": [
"domain:holidays",
"type:model"
Expand All @@ -177,6 +192,7 @@ exports[`export data > should test a simple application > simple-application 1`]
},
"src/holidays/ui/index.ts": {
"module": "src/holidays/ui",
"moduleType": "barrel",
"tags": [
"domain:holidays",
"type:ui"
Expand All @@ -188,6 +204,7 @@ exports[`export data > should test a simple application > simple-application 1`]
},
"src/holidays/ui/holidays.component.ts": {
"module": "src/holidays/ui",
"moduleType": "barrel",
"tags": [
"domain:holidays",
"type:ui"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ exports[`verify > should find errors > logs.log 1`] = `
Issues found:
Total Invalid Files: 2
Total Deep Imports: 1
Total Encapsulation Violations: 1
Total Dependency Rule Violations: 1
----------------------------------
|-- src/main.ts
| |-- Deep Imports
| |-- Encapsulation Violations
| | |-- ./customers/data
|-- src/holidays/holidays.component.ts
| |-- Dependency Rule Violations
Expand All @@ -28,12 +28,12 @@ exports[`verify > should find errors without sheriff.config.ts > logs.log 1`] =
Issues found:
Total Invalid Files: 1
Total Deep Imports: 1
Total Encapsulation Violations: 1
Total Dependency Rule Violations: 0
----------------------------------
|-- src/main.ts
| |-- Deep Imports
| |-- Encapsulation Violations
| | |-- ./customers/data"
`;

Expand Down
22 changes: 11 additions & 11 deletions packages/core/src/lib/cli/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { getEntryFromCliOrConfig } from './internal/get-entry-from-cli-or-config

type ValidationsMap = Record<
string,
{ deepImports: string[]; dependencyRules: string[] }
{ encapsulations: string[]; dependencyRules: string[] }
>;

export function verify(args: string[]) {
Expand All @@ -21,17 +21,17 @@ export function verify(args: string[]) {
const projectInfo = getEntryFromCliOrConfig(args[0]);

for (const { fileInfo } of traverseFileInfo(projectInfo.fileInfo)) {
const violations = Object.keys(hasEncapsulationViolations(fileInfo.path, projectInfo));
const encapsulations = Object.keys(hasEncapsulationViolations(fileInfo.path, projectInfo));

const dependencyRuleViolations = checkForDependencyRuleViolation(
fileInfo.path,
projectInfo,
);

if (violations.length > 0 || dependencyRuleViolations.length > 0) {
if (encapsulations.length > 0 || dependencyRuleViolations.length > 0) {
hasError = true;
filesCount++;
deepImportsCount += violations.length;
deepImportsCount += encapsulations.length;
dependencyRulesCount += dependencyRuleViolations.length;

const dependencyRules = dependencyRuleViolations.map(
Expand All @@ -40,7 +40,7 @@ export function verify(args: string[]) {
);

validationsMap[fs.relativeTo(fs.cwd(), fileInfo.path)] = {
deepImports: violations,
encapsulations,
dependencyRules,
};
}
Expand All @@ -52,7 +52,7 @@ export function verify(args: string[]) {
cli.log('');
cli.log('Issues found:');
cli.log(` Total Invalid Files: ${filesCount}`);
cli.log(` Total Deep Imports: ${deepImportsCount}`);
cli.log(` Total Encapsulation Violations: ${deepImportsCount}`);
cli.log(` Total Dependency Rule Violations: ${dependencyRulesCount}`);
cli.log('----------------------------------');
cli.log('');
Expand All @@ -62,14 +62,14 @@ export function verify(args: string[]) {
cli.endProcessOk();
}

for (const [file, { deepImports, dependencyRules }] of Object.entries(
for (const [file, { encapsulations, dependencyRules }] of Object.entries(
validationsMap,
)) {
cli.log('|-- ' + file);
if (deepImports.length > 0) {
cli.log('| |-- Deep Imports');
deepImports.forEach((deepImport) => {
cli.log('| | |-- ' + deepImport);
if (encapsulations.length > 0) {
cli.log('| |-- Encapsulation Violations');
encapsulations.forEach((encapsulation) => {
cli.log('| | |-- ' + encapsulation);
});
}

Expand Down
Loading

0 comments on commit 16bbf6e

Please sign in to comment.