Skip to content
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

feat: rename encapsulatedFolderNameForBarrelLess to `encapsulationP… #158

Merged
Show file tree
Hide file tree
Changes from all 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
29 changes: 18 additions & 11 deletions packages/core/src/lib/checks/has-encapsulation-violations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { FsPath } from '../file-info/fs-path';
import { Configuration } from '../config/configuration';
import { ProjectInfo } from '../main/init';
import { FileInfo } from '../modules/file.info';
import getFs from '../fs/getFs';

/**
* verifies if an existing file has imports which break
Expand All @@ -23,19 +24,30 @@ export function hasEncapsulationViolations(
isSameModule(importedFileInfo, assignedFileInfo) ||
isExcludedRootModule(rootDir, config, importedFileInfo) ||
accessesBarrelFileForBarrelModules(importedFileInfo) ||
accessesExposedFileForBarrelLessModules(importedFileInfo, config.enableBarrelLess)
accessesExposedFileForBarrelLessModules(
importedFileInfo,
config.enableBarrelLess,
config.encapsulationPatternForBarrelLess,
)
) {
// 👍 all good
} else {
const rawImport = assignedFileInfo.getRawImportForImportedFileInfo(importedFileInfo.path);
const rawImport = assignedFileInfo.getRawImportForImportedFileInfo(
importedFileInfo.path,
);
encapsulationViolations[rawImport] = importedFileInfo;
}
}

return encapsulationViolations;
}

function accessesExposedFileForBarrelLessModules(fileInfo: FileInfo, enableBarrelLess: boolean) {
function accessesExposedFileForBarrelLessModules(
fileInfo: FileInfo,
enableBarrelLess: boolean,
encapsulationPatternForBarrelLess: string,
) {
const fs = getFs();
if (!enableBarrelLess) {
return false;
}
Expand All @@ -44,13 +56,8 @@ function accessesExposedFileForBarrelLessModules(fileInfo: FileInfo, enableBarre
return false;
}

const possibleEncapsulatedFolderPath =
fileInfo.moduleInfo.getEncapsulatedFolder();
if (possibleEncapsulatedFolderPath === undefined) {
return true;
}

return !fileInfo.path.startsWith(possibleEncapsulatedFolderPath);
const relativePath = fs.relativeTo(fileInfo.moduleInfo.path, fileInfo.path);
return !relativePath.startsWith(encapsulationPatternForBarrelLess);
}

function accessesBarrelFileForBarrelModules(fileInfo: FileInfo) {
Expand All @@ -74,5 +81,5 @@ function isExcludedRootModule(
}

function isSameModule(importedFileInfo: FileInfo, assignedFileInfo: FileInfo) {
return importedFileInfo.moduleInfo.path === assignedFileInfo.moduleInfo.path
return importedFileInfo.moduleInfo.path === assignedFileInfo.moduleInfo.path;
}
159 changes: 106 additions & 53 deletions packages/core/src/lib/checks/tests/encapsulation-barrel-less.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,68 @@ import { hasEncapsulationViolations } from '../has-encapsulation-violations';
import { UserSheriffConfig } from '../../config/user-sheriff-config';
import { traverseFileInfo } from '../../modules/traverse-file-info';

function assertProject(config: Partial<UserSheriffConfig> = {}) {
return {
withCustomerRoute(customerFileTree: FileTree) {
return {
hasEncapsulationViolations(
encapsulationViolations: Record<string, string[]> = {},
) {
const projectInfo = testInit('src/main.ts', {
'tsconfig.json': tsConfig(),
'sheriff.config.ts': sheriffConfig({
...{
modules: {
'src/app/<domain>/<type>': ['domain:<domain>', 'type:<type>'],
},
depRules: {},
enableBarrelLess: true,
},
...config,
}),
src: {
'main.ts': ['./app/app.routes'],
app: {
'app.routes.ts': [
'./customer/feature/customer.component.ts',
'./customer/feature/customers.component.ts',
],
customer: customerFileTree,
},
},
});

for (const { fileInfo } of traverseFileInfo(projectInfo.fileInfo)) {
expect(
fileInfo.hasUnresolvedImports(),
`${fileInfo.path} has unresolved imports`,
).toBe(false);

const pathToLookup = fileInfo.path.replace(
'/project/src/app/customer/',
'',
);

const expectedDeepImports =
encapsulationViolations[pathToLookup] || [];
const violations = hasEncapsulationViolations(
fileInfo.path,
projectInfo,
);
const violatedImports = Object.keys(violations);
expect
.soft(
violatedImports,
`deep imports check failed for ${fileInfo.path}`,
)
.toEqual(expectedDeepImports);
}
},
};
},
};
}

describe('barrel-less', () => {
it('should check for deep imports', () => {
assertProject()
Expand Down Expand Up @@ -91,7 +153,7 @@ describe('barrel-less', () => {

it('should be able to change the name of internals', () => {
assertProject({
encapsulatedFolderNameForBarrelLess: 'private',
encapsulationPatternForBarrelLess: 'private',
})
.withCustomerRoute({
feature: {
Expand Down Expand Up @@ -125,60 +187,51 @@ describe('barrel-less', () => {
'feature/customers.component.ts': ['../data/open.service.ts'],
});
});
});

function assertProject(config: Partial<UserSheriffConfig> = {}) {
return {
withCustomerRoute(customerFileTree: FileTree) {
return {
hasEncapsulationViolations(encapsulationViolations: Record<string, string[]> = {}) {
const projectInfo = testInit('src/main.ts', {
'tsconfig.json': tsConfig(),
'sheriff.config.ts': sheriffConfig({
...{
modules: {
'src/app/<domain>/<type>': ['domain:<domain>', 'type:<type>'],
},
depRules: {},
enableBarrelLess: true,
},
...config,
}),
src: {
'main.ts': ['./app/app.routes'],
app: {
'app.routes.ts': [
'./customer/feature/customer.component.ts',
'./customer/feature/customers.component.ts',
],
customer: customerFileTree,
},
it('should be by default first level only', () => {
assertProject()
.withCustomerRoute({
feature: {
'customer.component.ts': [''],
'customers.component.ts': ['../data/sub1/internal/hidden.service.ts'],
},
data: {
sub1: {
internal: { 'hidden.service.ts': [] },
},
},
})
.hasEncapsulationViolations({});
});

it.skip('should support wildcards', () => {
assertProject({ encapsulationPatternForBarrelLess: '**/internal' })
.withCustomerRoute({
feature: {
'customer.component.ts': [],
'customers.component.ts': [
'../data/sub1/internal/hidden.service.ts',
'../data/sub2/sub3/internal/hidden.service.ts',
],
},
data: {
sub1: {
internal: { 'hidden.service.ts': [] },
},
sub2: {
sub3: {
internal: { 'hidden.service.ts': [] },
},
});
},
},
})
.hasEncapsulationViolations({
'feature/customers.component.ts': ['../data/open.service.ts'],
});
});

for (const { fileInfo } of traverseFileInfo(projectInfo.fileInfo)) {
expect(
fileInfo.hasUnresolvedImports(),
`${fileInfo.path} has unresolved imports`,
).toBe(false);
it.skip('should support nested wildcards', () => {});

const pathToLookup = fileInfo.path.replace(
'/project/src/app/customer/',
'',
);
it.skip('should apply regex to path', () => {});
});

const expectedDeepImports = encapsulationViolations[pathToLookup] || [];
const violations = hasEncapsulationViolations(fileInfo.path, projectInfo);
const violatedImports = Object.keys(violations);
expect
.soft(
violatedImports,
`deep imports check failed for ${fileInfo.path}`,
)
.toEqual(expectedDeepImports);
}
},
};
},
};
}
9 changes: 8 additions & 1 deletion packages/core/src/lib/config/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { UserSheriffConfig } from './user-sheriff-config';

export type Configuration = Required<Omit<UserSheriffConfig, 'tagging'>> & {
export type Configuration = Required<
Omit<
UserSheriffConfig,
| 'tagging'
| 'showWarningOnBarrelCollision'
| 'encapsulatedFolderNameForBarrelLess'
>
> & {
// dependency rules will skip if `isConfigFileMissing` is true
isConfigFileMissing: boolean;
};
3 changes: 1 addition & 2 deletions packages/core/src/lib/config/default-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ export const defaultConfig: Configuration = {
depRules: {},
excludeRoot: false,
enableBarrelLess: false,
encapsulatedFolderNameForBarrelLess: 'internal',
showWarningOnBarrelCollision: true,
encapsulationPatternForBarrelLess: 'internal',
log: false,
entryFile: '',
isConfigFileMissing: false,
Expand Down
22 changes: 17 additions & 5 deletions packages/core/src/lib/config/parse-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import getFs from '../fs/getFs';
import { Configuration } from './configuration';
import {
CollidingEncapsulationSettings,
MissingModulesWithoutAutoTaggingError,
TaggingAndModulesError,
} from '../error/user-error';
Expand All @@ -24,12 +25,23 @@
if (userSheriffConfig.tagging && userSheriffConfig.modules) {
throw new TaggingAndModulesError();
}

if (userSheriffConfig.tagging) {
const {tagging, ...rest} = userSheriffConfig;
return { ...defaultConfig, ...rest, modules: tagging };
userSheriffConfig.modules = userSheriffConfig.tagging;
}

} else {
return { ...defaultConfig, ...userSheriffConfig };
if (
userSheriffConfig.encapsulationPatternForBarrelLess !== undefined &&
userSheriffConfig.encapsulatedFolderNameForBarrelLess !== undefined
) {
throw new CollidingEncapsulationSettings();
}

if (userSheriffConfig.encapsulatedFolderNameForBarrelLess) {
userSheriffConfig.encapsulationPatternForBarrelLess =
userSheriffConfig.encapsulatedFolderNameForBarrelLess;
}

const { tagging, encapsulatedFolderNameForBarrelLess, ...rest } =

Check warning on line 44 in packages/core/src/lib/config/parse-config.ts

View workflow job for this annotation

GitHub Actions / build

'tagging' is assigned a value but never used. Allowed unused vars must match /^_/u

Check warning on line 44 in packages/core/src/lib/config/parse-config.ts

View workflow job for this annotation

GitHub Actions / build

'encapsulatedFolderNameForBarrelLess' is assigned a value but never used. Allowed unused vars must match /^_/u
userSheriffConfig;
return { ...defaultConfig, ...rest };
};
Loading
Loading