Skip to content

Commit

Permalink
feat: allow regex for encapsulationPattern
Browse files Browse the repository at this point in the history
  • Loading branch information
rainerhahnekamp committed Oct 29, 2024
1 parent e0b4b81 commit 4dda1fa
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 13 deletions.
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()}Δ`,
};
}
}

0 comments on commit 4dda1fa

Please sign in to comment.