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: allow regex for encapsulationPattern #163

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
10 changes: 8 additions & 2 deletions packages/core/src/lib/checks/has-encapsulation-violations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function hasEncapsulationViolations(
function accessesExposedFileForBarrelLessModules(
fileInfo: FileInfo,
enableBarrelLess: boolean,
encapsulationPatternForBarrelLess: string,
encapsulationPattern: string | RegExp,
) {
const fs = getFs();
if (!enableBarrelLess) {
Expand All @@ -57,7 +57,13 @@ function accessesExposedFileForBarrelLessModules(
}

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

if (typeof encapsulationPattern === 'string') {
return !relativePath.startsWith(encapsulationPattern);
} else {
const matches = relativePath.match(encapsulationPattern);
return !matches
}
}

function accessesBarrelFileForBarrelModules(fileInfo: FileInfo) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,35 @@ describe('barrel-less', () => {

it.skip('should support nested wildcards', () => {});

it.skip('should apply regex to path', () => {});
it('should apply regex to path', () => {
assertProject({ encapsulationPattern: /(^|\/)_/ })
.withCustomerRoute({
feature: {
'customer.component.ts': [],
'customers.component.ts': [
'../data/_main.ts',
'../data/sub/_file.ts',
'../data/_sub/file.ts',
'../data/main.ts',
'../data/main_file.ts',
'../data/internal/file.ts',
],
},
data: {
'_main.ts': [],
sub: { '_file.ts': [] },
_sub: { 'file.ts': [] },
'main.ts': [],
'main_file.ts': [],
'internal/file.ts': [],
},
})
.hasEncapsulationViolations({
'feature/customers.component.ts': [
'../data/_main.ts',
'../data/sub/_file.ts',
'../data/_sub/file.ts',
],
});
});
});

32 changes: 26 additions & 6 deletions packages/core/src/lib/config/user-sheriff-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,16 +191,36 @@ export interface UserSheriffConfig {
* or a regex to define the location/pattern for
* encapsulation in barrel-less modules.
*
* Example with a simple string:
* Examples:
*
* <code>
* ---
* A **string** `encapsulationPattern: 'private'` encapsulates
*
* </code>
* - `private/main.ts`
* - `private/sub/sub.ts`
*
* This is a more powerful alternative to
* {@link encapsulatedFolderNameForBarrelLess}.
* But would expose
* - `main.ts`
* - `internal/hidden.ts`
*
* ---
*
* A **regular expression** `encapsulationPattern: /(^|\/)_/` encapsulates
* any file or directory starting with an underscore:
*
* - _main.ts
* - internal/_hidden
* - _sub/main.ts
*
* But would expose
*
* - main.ts
* - internal/hidden
* - sub/main_file.ts
*
* ---
*/
encapsulationPattern?: string
encapsulationPattern?: string | RegExp

/**
* @deprecated no warning is shown.
Expand Down
28 changes: 25 additions & 3 deletions packages/core/src/lib/test/project-creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,20 @@ class ProjectCreator {
} else if (typeof value === 'string') {
this.fs.writeFile(`${currentDir}/${child}`, value);
} else if (isSheriffConfigContent(value)) {
const serializedConfig = JSON.stringify(
serializeDepRules({ ...defaultConfig, ...value.content }),
).replace(/"α([^ω]+)ω"/g, '$1');
let serializedConfig = JSON.stringify(
serializeEncapsulationPattern(
serializeDepRules({ ...defaultConfig, ...value.content }),
),
);

if (value.content.encapsulationPattern instanceof RegExp) {
serializedConfig = serializedConfig.replace(
/"Δ.*Δ"/,
value.content.encapsulationPattern.toString(),
);
}

serializedConfig = serializedConfig.replace(/"α([^ω]+)ω"/g, '$1');
this.fs.writeFile(
`${currentDir}/${child}`,
`export const config = ${serializedConfig};`,
Expand All @@ -75,3 +86,14 @@ function serializeDepRules(config: Configuration): Configuration {
),
};
}

function serializeEncapsulationPattern(config: Configuration): Configuration {
if (typeof config.encapsulationPattern === 'string') {
return config;
} else {
return {
...config,
encapsulationPattern: `Δ${config.encapsulationPattern.toString()}Δ`,
};
}
}
Loading