Skip to content

Commit

Permalink
Small refactor (#150)
Browse files Browse the repository at this point in the history
Small refactor

Rename some stuff
  • Loading branch information
guyca authored Jun 28, 2024
1 parent 9c35208 commit f6a2f19
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Clazz } from './class';
import type { Import } from './import';

export class ClassWithImports {
export class ClassFile {

constructor(
public readonly clazz: Clazz,
Expand Down
4 changes: 2 additions & 2 deletions packages/eslint-plugin-obsidian/rules/dto/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { TSESTree } from '@typescript-eslint/types';
import { Clazz } from './class';
import { getClassDeclaration, isClassLike, isImportDeclaration } from '../ast/utils';
import { Import } from './import';
import { ClassWithImports } from './classWithImports';
import { ClassFile } from './classFile';
import { assertDefined } from '../utils/assertions';

export class File {
Expand Down Expand Up @@ -31,7 +31,7 @@ export class File {

public toClassWithImports() {
return this.graphs.map((graph) => {
return new ClassWithImports(graph, this.imports, this.path);
return new ClassFile(graph, this.imports, this.path);
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ClassWithImports } from '../dto/classWithImports';
import { ClassFile } from '../dto/classFile';
import type { Import } from '../dto/import';
import type { Clazz } from '../dto/class';
import type { SubgraphResolver } from './subgraphResolver';
Expand All @@ -8,20 +8,20 @@ export class DependencyResolver {
constructor(private subgraphResolver: SubgraphResolver) { }

public resolve(context: Context, clazz: Clazz, imports: Import[]) {
const classWithImports = new ClassWithImports(clazz, imports, context.physicalFilename!);
const classWithImports = new ClassFile(clazz, imports, context.physicalFilename!);
return [
...this.getGraphDependencies(classWithImports),
...this.getDependenciesFromSubgraphs(classWithImports),
];
}

private getDependenciesFromSubgraphs(clazz: ClassWithImports): string[] {
private getDependenciesFromSubgraphs(clazz: ClassFile): string[] {
return this.subgraphResolver
.resolve(clazz)
.flatMap(this.getGraphDependencies);
}

private getGraphDependencies({ clazz }: ClassWithImports) {
private getGraphDependencies({ clazz }: ClassFile) {
return clazz
.getDecoratedMethods('Provides')
.map((method) => method.name);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import type { TSESTree } from '@typescript-eslint/types';
import { mapArrayExpression, requireProgram } from '../ast/utils';
import { ClassWithImports } from '../dto/classWithImports';
import { ClassFile } from '../dto/classFile';
import type { Property } from '../dto/property';
import { File } from '../dto/file';
import type { FileReader } from '../framework/fileReader';

export class SubgraphResolver {
constructor(private fileReader: FileReader) { }

public resolve(clazz: ClassWithImports): ClassWithImports[] {
public resolve(clazz: ClassFile): ClassFile[] {
return [
...this.getImportedGraphs(clazz),
...this.getLocalGraphs(clazz),
].flatMap((g) => [g, ...this.resolve(g)]);
}

private getImportedGraphs(clazz: ClassWithImports) {
private getImportedGraphs(clazz: ClassFile) {
const subgraphs = this.getSubgraphsPropertyFromGraphDecorator(clazz);
return this.getImportedSubgraphClasses(clazz, subgraphs);
}

private getImportedSubgraphClasses(clazz: ClassWithImports, subgraphs: Property | undefined) {
private getImportedSubgraphClasses(clazz: ClassFile, subgraphs: Property | undefined) {
if (!subgraphs) return [];
const subgraphNames = this.getSubgraphNamesFromDecoratorProperty(subgraphs);
const classes: ClassWithImports[] = [];
const classes: ClassFile[] = [];
clazz.imports.forEach(($import) => {
subgraphNames.forEach((subgraphName: string) => {
if ($import.includes(subgraphName)) {
Expand All @@ -34,28 +34,28 @@ export class SubgraphResolver {
return classes;
}

private getLocalGraphs(clazzWithImports: ClassWithImports) {
const subgraphs = this.getSubgraphsPropertyFromGraphDecorator(clazzWithImports);
return this.getLocalSubgraphClasses(subgraphs, clazzWithImports);
private getLocalGraphs(clazz: ClassFile) {
const subgraphs = this.getSubgraphsPropertyFromGraphDecorator(clazz);
return this.getLocalSubgraphClasses(subgraphs, clazz);
}

private getSubgraphsPropertyFromGraphDecorator({clazz}: ClassWithImports) {
private getSubgraphsPropertyFromGraphDecorator({clazz}: ClassFile) {
const graphDecorator = clazz.requireDecorator('Graph');
return graphDecorator.getProperty('subgraphs');
}

private getLocalSubgraphClasses(subgraphs: Property | undefined, clazz: ClassWithImports) {
private getLocalSubgraphClasses(subgraphs: Property | undefined, clazz: ClassFile) {
if (!subgraphs) return [];
const allSubgraphs = this.getSubgraphNamesFromDecoratorProperty(subgraphs);
const localGraphNames = this.getLocalGraphNames(allSubgraphs, clazz);
return this.createLocalGraphClasses(clazz, localGraphNames);
}

private createLocalGraphClasses({clazz, imports, path}: ClassWithImports, localGraphNames: string[]) {
private createLocalGraphClasses({clazz, imports, path}: ClassFile, localGraphNames: string[]) {
if (localGraphNames.length === 0) return [];
const parent = new File(requireProgram(clazz.node), path);
return localGraphNames.map((localGraphName) => {
return new ClassWithImports(parent.requireGraph(localGraphName), imports, path);
return new ClassFile(parent.requireGraph(localGraphName), imports, path);
});
}

Expand All @@ -66,7 +66,7 @@ export class SubgraphResolver {
);
}

private getLocalGraphNames(subgraphs: string[], {imports}: ClassWithImports) {
private getLocalGraphNames(subgraphs: string[], {imports}: ClassFile) {
return subgraphs.filter((subgraph) => {
return imports.some(($import) => {
return $import.includes(subgraph);
Expand Down

0 comments on commit f6a2f19

Please sign in to comment.