Skip to content

Commit

Permalink
feat(schematics): allow wildcard in whitelisting of deep imports
Browse files Browse the repository at this point in the history
  • Loading branch information
MaximSagan authored and vsavkin committed Jun 4, 2019
1 parent 609a2f6 commit 82898f7
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,82 @@ describe('Enforce Module Boundaries', () => {
);
});

it('should not error about deep imports into library when fixed exception is set', () => {
const failures = runRule(
{ allow: ['@mycompany/other/src/blah'] },
`${process.cwd()}/proj/libs/mylib/src/main.ts`,
`
import "@mycompany/other/src/blah"
`,
[
{
name: 'mylibName',
root: 'libs/mylib',
type: ProjectType.lib,
tags: [],
implicitDependencies: [],
architect: {},
files: [`libs/mylib/src/main.ts`, `libs/mylib/src/another-file.ts`],
fileMTimes: {
'libs/mylib/src/main.ts': 1,
'libs/mylib/src/another-file.ts': 1
}
},
{
name: 'otherName',
root: 'libs/other',
type: ProjectType.lib,
tags: [],
implicitDependencies: [],
architect: {},
files: [`libs/other/src/blah.ts`],
fileMTimes: {
'libs/other/src/blah.ts': 1
}
}
]
);
expect(failures.length).toEqual(0);
});

it('should not error about deep imports into library when exception is specified with a wildcard', () => {
const failures = runRule(
{ allow: ['@mycompany/other/*'] },
`${process.cwd()}/proj/libs/mylib/src/main.ts`,
`
import "@mycompany/other/src/blah"
`,
[
{
name: 'mylibName',
root: 'libs/mylib',
type: ProjectType.lib,
tags: [],
implicitDependencies: [],
architect: {},
files: [`libs/mylib/src/main.ts`, `libs/mylib/src/another-file.ts`],
fileMTimes: {
'libs/mylib/src/main.ts': 1,
'libs/mylib/src/another-file.ts': 1
}
},
{
name: 'otherName',
root: 'libs/other',
type: ProjectType.lib,
tags: [],
implicitDependencies: [],
architect: {},
files: [`libs/other/src/blah.ts`],
fileMTimes: {
'libs/other/src/blah.ts': 1
}
}
]
);
expect(failures.length).toEqual(0);
});

it('should error on importing a lazy-loaded library', () => {
const failures = runRule(
{},
Expand Down
11 changes: 10 additions & 1 deletion packages/workspace/src/tslint/nxEnforceModuleBoundariesRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class EnforceModuleBoundariesWalker extends Lint.RuleWalker {
.substring(1, node.moduleSpecifier.getText().length - 1);

// whitelisted import
if (this.allow.indexOf(imp) > -1) {
if (this.allow.some(a => matchImportWithWildcard(a, imp))) {
super.visitImportDeclaration(node);
return;
}
Expand Down Expand Up @@ -347,3 +347,12 @@ function removeExt(file: string): string {
function normalizePath(osSpecificPath: string): string {
return osSpecificPath.split(path.sep).join('/');
}

function matchImportWithWildcard(
// This may or may not contain wildcards ("*")
allowableImport: string,
extractedImport: string
): boolean {
const regex = new RegExp('^' + allowableImport.split('*').join('.*') + '$');
return regex.test(extractedImport);
}

0 comments on commit 82898f7

Please sign in to comment.