Skip to content

Commit

Permalink
feat: return graph from collect function
Browse files Browse the repository at this point in the history
  • Loading branch information
kazushisan committed Sep 22, 2024
1 parent b8262e7 commit e97b74c
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 12 deletions.
61 changes: 61 additions & 0 deletions lib/util/Graph.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
export class Graph {
vertexes = new Map<string, { to: Set<string>; from: Set<string> }>();

private addVertex(vertex: string) {
const selected = this.vertexes.get(vertex);
if (selected) {
return selected;
}

const created = { to: new Set<string>(), from: new Set<string>() };

this.vertexes.set(vertex, created);

return created;
}

deleteVertex(vertex: string) {
const selected = this.vertexes.get(vertex);

if (!selected) {
return;
}

for (const v of selected.to) {
const target = this.vertexes.get(v);

if (!target) {
continue;
}

target.from.delete(vertex);

if (target.from.size === 0 && target.to.size === 0) {
this.vertexes.delete(v);
}
}

for (const v of selected.from) {
const target = this.vertexes.get(v);

if (!target) {
continue;
}

target.to.delete(vertex);

if (target.from.size === 0 && target.to.size === 0) {
this.vertexes.delete(v);
}
}

this.vertexes.delete(vertex);
}

addEdge(source: string, destination: string): void {
const s = this.addVertex(source);
const d = this.addVertex(destination);
s.to.add(destination);
d.from.add(source);
}
}
28 changes: 19 additions & 9 deletions lib/util/collectDynamicImports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,44 @@ const getProgram = (languageService: ts.LanguageService) => {
};

describe('collectDynamicImports', () => {
it('should return a set of dynamic imports', () => {
it('should return a graph of dynamic imports', () => {
const { languageService, fileService } = setup();
fileService.set('/app/main.ts', `import('./a.js');`);
fileService.set('/app/a.js', `export const a = 'a';`);
fileService.set('/app/a.ts', `export const a = 'a';`);

const program = getProgram(languageService);

const result = collectDynamicImports({
const graph = collectDynamicImports({
fileService,
program,
});

assert.equal(result.size, 1);
assert.equal(result.has('/app/a.js'), true);
assert.equal(graph.vertexes.size, 2);
assert.equal(graph.vertexes.has('/app/main.ts'), true);
assert.equal(graph.vertexes.has('/app/a.ts'), true);
assert.equal(graph.vertexes.get('/app/main.ts')?.to.size, 1);
assert.equal(graph.vertexes.get('/app/main.ts')?.to.has('/app/a.ts'), true);
assert.equal(graph.vertexes.get('/app/main.ts')?.from.size, 0);
assert.equal(graph.vertexes.get('/app/a.ts')?.from.size, 1);
assert.equal(
graph.vertexes.get('/app/a.ts')?.from.has('/app/main.ts'),
true,
);
assert.equal(graph.vertexes.get('/app/a.ts')?.to.size, 0);
});

it('should return an empty set if no dynamic imports are found', () => {
it('should return an empty graph if no dynamic imports are found', () => {
const { languageService, fileService } = setup();
fileService.set('/app/main.ts', `import { a } from './a.js';`);
fileService.set('/app/a.js', `export const a = 'a';`);
fileService.set('/app/a.ts', `export const a = 'a';`);

const program = getProgram(languageService);

const result = collectDynamicImports({
const graph = collectDynamicImports({
fileService,
program,
});

assert.equal(result.size, 0);
assert.equal(graph.vertexes.size, 0);
});
});
7 changes: 4 additions & 3 deletions lib/util/collectDynamicImports.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ts from 'typescript';
import { getFileFromModuleSpecifierText } from './getFileFromModuleSpecifierText.js';
import { FileService } from './FileService.js';
import { Graph } from './Graph.js';

export const collectDynamicImports = ({
program,
Expand All @@ -9,7 +10,7 @@ export const collectDynamicImports = ({
program: ts.Program;
fileService: FileService;
}) => {
const result = new Set<string>();
const graph = new Graph();
const files = fileService.getFileNames();
for (const file of files) {
const sourceFile = program.getSourceFile(file);
Expand All @@ -33,7 +34,7 @@ export const collectDynamicImports = ({
});

if (file) {
result.add(file);
graph.addEdge(sourceFile.fileName, file);
}

return;
Expand All @@ -45,5 +46,5 @@ export const collectDynamicImports = ({
sourceFile.forEachChild(visit);
}

return result;
return graph;
};

0 comments on commit e97b74c

Please sign in to comment.