forked from softarc-consulting/sheriff
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(eslint): supersede
deep-import
rules with encapsulation
rule
The renaming is necessary since the concept of a deep import does not exist in barrel-less modules. The configs for ESLint have been changed. To use the old `deep-import` rule, you need to switch to the config `barrelModulesOnly`.
- Loading branch information
1 parent
86cf9f6
commit 3a4f132
Showing
25 changed files
with
515 additions
and
274 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
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
82 changes: 0 additions & 82 deletions
82
packages/core/src/lib/checks/tests/deep-import-with-exclude-root.spec.ts
This file was deleted.
Oops, something went wrong.
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
97 changes: 97 additions & 0 deletions
97
packages/core/src/lib/checks/tests/encapsulation-with-exclude-root.spec.ts
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,97 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { anyTag, violatesEncapsulationRule } from '@softarc/sheriff-core'; | ||
import getFs from '../../fs/getFs'; | ||
import { toFsPath } from '../../file-info/fs-path'; | ||
import { testInit } from '../../test/test-init'; | ||
import { sheriffConfig } from '../../test/project-configurator'; | ||
import { tsConfig } from '../../test/fixtures/ts-config'; | ||
|
||
describe('encapsulation and excludeRoot config property', () => { | ||
for (const { excludeRoot, enableBarrelLess, hasViolation } of [ | ||
{ excludeRoot: true, enableBarrelLess: false, hasViolation: false }, | ||
{ excludeRoot: false, enableBarrelLess: false, hasViolation: true }, | ||
{ excludeRoot: true, enableBarrelLess: true, hasViolation: false }, | ||
{ excludeRoot: false, enableBarrelLess: true, hasViolation: false }, | ||
]) { | ||
it(`should be ${ | ||
hasViolation ? 'valid' : 'invalid' | ||
} for rootExcluded: ${excludeRoot} and barrel-less: ${enableBarrelLess}`, () => { | ||
testInit('src/main.ts', { | ||
'tsconfig.json': tsConfig(), | ||
'sheriff.config.ts': sheriffConfig({ | ||
modules: { 'src/shared': 'shared' }, | ||
depRules: { '*': anyTag }, | ||
excludeRoot, | ||
enableBarrelLess, | ||
}), | ||
src: { | ||
'main.ts': '', | ||
'router.ts': ['./shared/dialog'], // always violation | ||
'config.ts': ['./shared/index'], | ||
shared: { | ||
'get.ts': ['../config', '../holidays/holidays-component'], // depends on `excludeRoot` | ||
'dialog.ts': '', | ||
'index.ts': '', | ||
}, | ||
holidays: { | ||
'holidays-component.ts': ['../config'], // always valid}, | ||
}, | ||
}, | ||
}); | ||
|
||
assertViolation('/project/src/router.ts', './shared/dialog', false, true); | ||
|
||
assertViolation( | ||
'/project/src/holidays/holidays-component.ts', | ||
'../config', | ||
true, | ||
false, | ||
); | ||
|
||
assertViolation( | ||
'/project/src/shared/get.ts', | ||
'../config', | ||
true, | ||
hasViolation, | ||
); | ||
|
||
assertViolation( | ||
'/project/src/shared/get.ts', | ||
'../holidays/holidays-component', | ||
true, | ||
hasViolation, | ||
); | ||
}); | ||
} | ||
}); | ||
|
||
function assertViolation( | ||
filename: string, | ||
importCommand: string, | ||
isImportToBarrelLess: boolean, | ||
hasViolation: boolean, | ||
) { | ||
expect( | ||
violatesEncapsulationRule( | ||
filename, | ||
importCommand, | ||
true, | ||
getFs().readFile(toFsPath(filename)), | ||
false, | ||
), | ||
`import in ${filename} to ${importCommand} should violate encapsulation: ${hasViolation}`, | ||
).toBe(getMessage(hasViolation, isImportToBarrelLess, importCommand)); | ||
} | ||
|
||
function getMessage( | ||
hasViolation: boolean, | ||
isImportToBarrelLess: boolean, | ||
importCommand: string, | ||
) { | ||
if (!hasViolation) { | ||
return ''; | ||
} | ||
return isImportToBarrelLess | ||
? `'${importCommand}' cannot be imported. It is encapsulated.` | ||
: `'${importCommand}' is a deep import from a barrel module. Use the module's barrel file (index.ts) instead.`; | ||
} |
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
Oops, something went wrong.