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(lint): extend circular dependency check #378

Merged
merged 1 commit into from
Mar 29, 2018
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
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,56 @@ describe('Enforce Module Boundaries', () => {
'Circular dependency between "anotherlib" and "mylib" detected'
);
});

it('should error when circular dependency detected (indirect)', () => {
const failures = runRule(
{},
`${process.cwd()}/proj/libs/mylib/src/main.ts`,
'import "@mycompany/badcirclelib"',
[
{
name: 'mylib',
root: 'libs/mylib/src',
type: ProjectType.lib,
tags: [],
files: [`libs/mylib/src/main.ts`]
},
{
name: 'anotherlib',
root: 'libs/anotherlib/src',
type: ProjectType.lib,
tags: [],
files: [`libs/anotherlib/src/main.ts`]
},
{
name: 'badcirclelib',
root: 'libs/badcirclelib/src',
type: ProjectType.lib,
tags: [],
files: [`libs/badcirclelib/src/main.ts`]
},
{
name: 'myapp',
root: 'apps/myapp/src',
type: ProjectType.app,
tags: [],
files: [`apps/myapp/index.ts`]
}
],
{
mylib: [
{ projectName: 'badcirclelib', type: DependencyType.es6Import }
],
badcirclelib: [
{ projectName: 'anotherlib', type: DependencyType.es6Import }
],
anotherlib: [{ projectName: 'mylib', type: DependencyType.es6Import }]
}
);
expect(failures[0].getFailure()).toEqual(
'Circular dependency between "mylib" and "badcirclelib" detected'
);
});
});

function runRule(
Expand Down
24 changes: 21 additions & 3 deletions packages/schematics/src/tslint/nxEnforceModuleBoundariesRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,27 @@ class EnforceModuleBoundariesWalker extends Lint.RuleWalker {
targetProject: ProjectNode
): boolean {
if (!this.deps[targetProject.name]) return false;
return this.deps[targetProject.name].some(
dep => dep.projectName == sourceProject.name
);
return this.isDependingOn(targetProject.name, sourceProject.name);
}

private isDependingOn(
sourceProjectName: string,
targetProjectName: string,
done: { [projectName: string]: boolean } = {}
): boolean {
if (done[sourceProjectName]) return false;
if (!this.deps[sourceProjectName]) return false;
return this.deps[sourceProjectName]
.map(
dep =>
dep.projectName === targetProjectName
? true
: this.isDependingOn(dep.projectName, targetProjectName, {
...done,
[`${sourceProjectName}`]: true
})
)
.some(result => result);
}

private onlyLoadChildren(
Expand Down