Skip to content

Commit

Permalink
Assassinate ASTFunctions (#148)
Browse files Browse the repository at this point in the history
This is the third and last part of the refactor to the eslint plugin. With this part, we can finally delete the ASTFunctions file which was very long and a bit complicated to understand.
  • Loading branch information
guyca authored Jun 26, 2024
1 parent aad9207 commit 59d1a25
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 41 deletions.
5 changes: 2 additions & 3 deletions packages/eslint-plugin-obsidian/rules/dto/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@ export class Clazz {
return this.node.body.body;
}

public getDecoratedMethods(decoratorName: string) {
public getDecoratedMethods(decoratorName: string): Method[] {
return this.body
.filter(isMethodDefinition)
.map((node) => new Method(node))
.filter((method) => method.isDecoratedWith(decoratorName))
.map((method) => method.name);
.filter((method) => method.isDecoratedWith(decoratorName));
}

public requireDecorator(name: string) {
Expand Down
7 changes: 6 additions & 1 deletion packages/eslint-plugin-obsidian/rules/dto/method.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import type { TSESTree } from '@typescript-eslint/types';
import { Decorator } from './decorator';
import { Parameter } from './parameter';

export class Method {

constructor(private node: TSESTree.MethodDefinition) {}
constructor(public readonly node: TSESTree.MethodDefinition) {}

get name() {
return (this.node.key as TSESTree.Identifier).name;
}

get parameters() {
return this.node.value.params.map((param) => new Parameter(param));
}

isDecoratedWith(decoratorName: string): boolean {
return this.decorators.some((decorator) => {
return decorator.expression.callee.name === decoratorName;
Expand Down
9 changes: 9 additions & 0 deletions packages/eslint-plugin-obsidian/rules/dto/parameter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { TSESTree } from '@typescript-eslint/types';

export class Parameter {
constructor(public readonly node: TSESTree.Parameter) {}

get name() {
return (this.node as TSESTree.Identifier).name;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export class DependencyResolver {
}

private getGraphDependencies({ clazz }: ClassWithImports) {
return clazz.getDecoratedMethods('Provides');
return clazz
.getDecoratedMethods('Provides')
.map((method) => method.name);
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import type { Clazz } from '../dto/class';
import type { Context } from './types';
import { checkDependencies } from './ASTFunctions';
import { reportErrorIfDependencyIsUnresolved } from './errorReporter';
import type { DependencyResolver } from './dependencyResolver';
import type { Import } from '../dto/import';
import { ResolvedDependencyChecker } from './resolvedDependencyChecker';

export class GraphHandler {
constructor(
private context: Context,
private dependencyResolver: DependencyResolver,
private resolvedDependencyChecker: ResolvedDependencyChecker = new ResolvedDependencyChecker(),
) { }

public handle(clazz: Clazz, imports: Import[]) {
if (this.hasGraphDecorator(clazz)) {
const dependencies = this.dependencyResolver.resolve(clazz, imports);
reportErrorIfDependencyIsUnresolved(this.context, checkDependencies(clazz, dependencies));
const resolvedDependenciesCheck = this.resolvedDependencyChecker.check(clazz, dependencies);
reportErrorIfDependencyIsUnresolved(this.context, resolvedDependenciesCheck);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { Clazz } from '../dto/class';
import type { Parameter } from '../dto/parameter';

type DependencyCheckResult = { error: boolean; param?: string; node?: any };

export class ResolvedDependencyChecker {

public check(clazz: Clazz, dependencies: string[]): DependencyCheckResult {
const unresolvedDependency = clazz
.getDecoratedMethods('Provides')
.flatMap((method) => method.parameters)
.find((provider) => !dependencies.includes(provider.name));
return this.getResult(unresolvedDependency);
}

private getResult(unresolvedDependency: Parameter | undefined): DependencyCheckResult {
if (unresolvedDependency) {
return { error: true, param: unresolvedDependency.name, node: unresolvedDependency.node };
}
return { error: false };
}
}

0 comments on commit 59d1a25

Please sign in to comment.