Skip to content

Commit

Permalink
Report circular dependency error on the provider node (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
guyca committed Jul 10, 2024
1 parent 625c5b8 commit d9b5b69
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { TSESTree } from '@typescript-eslint/types';
import type { Clazz } from '../../dto/class';
import type { Method } from '../../dto/method';
import type { Parameter } from '../../dto/parameter';

type DetectionResult =
{ hasCircularDependency: true; path: string[] } |
{ hasCircularDependency: false; path?: never };
{ hasCircularDependency: true; path: string[]; node: TSESTree.Node } |
{ hasCircularDependency: false; path?: never; node?: never };

export class CircularDependenciesDetector {

Expand All @@ -13,8 +13,7 @@ export class CircularDependenciesDetector {
const visited = new Set<string>();
for (const provider of providers) {
const result = this.isCircular(
provider.name,
provider.parameters,
provider,
providers,
visited,
);
Expand All @@ -26,25 +25,25 @@ export class CircularDependenciesDetector {
}

isCircular(
providerName: string,
dependencies: Parameter[],
provider: Method,
providers: Method[],
path: Set<string>,
): DetectionResult {
if (path.has(providerName)) {
if (path.has(provider.name)) {
return {
hasCircularDependency: true,
path: [...path, providerName],
path: [...path, provider.name],
node: provider.node,
};
}

const newPath = new Set(path);
newPath.add(providerName);
newPath.add(provider.name);

for (const dep of dependencies) {
for (const dep of provider.parameters) {
const depProvider = providers.find(p => p.name === dep.name);
if (depProvider) {
const result = this.isCircular(depProvider.name, depProvider.parameters, providers, newPath);
const result = this.isCircular(depProvider, providers, newPath);
if (result.hasCircularDependency) return result;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import type { TSESTree } from '@typescript-eslint/types';
import type { Context } from '../../dto/context';

type Report = {
isError: boolean;
path?: string[];
node?: TSESTree.Node;
};

export class ErrorReporter {
constructor(private context: Context) { }

public report(
{ error, path, node }: { error: boolean; path?: string[]; node?: TSESTree.Node },
) {
if (error && node && path) {
public report({ isError, path, node }: Report) {
if (isError && path && node) {
this.context.reportError(
node,
'no-circular-dependencies',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class GraphHandler {
public handle(clazz: Clazz) {
if (this.hasGraphDecorator(clazz)) {
const result = this.circularDependenciesDetector.detect(clazz);
this.errorReporter.report({ error: result.hasCircularDependency, path: result.path, node: clazz.node });
this.errorReporter.report({ isError: result.hasCircularDependency, ...result });
}
}

Expand Down

0 comments on commit d9b5b69

Please sign in to comment.