-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from line/fix/dynamic-imports
fix: add support for dynamic imports
- Loading branch information
Showing
10 changed files
with
307 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { describe, it } from 'node:test'; | ||
import assert from 'node:assert/strict'; | ||
import { Graph } from './Graph.js'; | ||
|
||
describe('Graph', () => { | ||
it('should add edges correctly', () => { | ||
const graph = new Graph(); | ||
graph.addEdge('A', 'B'); | ||
graph.addEdge('A', 'C'); | ||
|
||
assert.equal(graph.vertexes.size, 3); | ||
assert.deepEqual(graph.vertexes.get('A')?.to, new Set(['B', 'C'])); | ||
assert.deepEqual(graph.vertexes.get('B')?.from, new Set(['A'])); | ||
assert.deepEqual(graph.vertexes.get('C')?.from, new Set(['A'])); | ||
}); | ||
|
||
it('should delete vertex correctly', () => { | ||
const graph = new Graph(); | ||
graph.addEdge('A', 'B'); | ||
graph.addEdge('A', 'C'); | ||
graph.addEdge('B', 'C'); | ||
|
||
graph.deleteVertex('A'); | ||
|
||
assert.equal(graph.vertexes.size, 2); | ||
assert.equal(graph.vertexes.has('A'), false); | ||
assert.deepEqual(graph.vertexes.get('B')?.to, new Set(['C'])); | ||
assert.deepEqual(graph.vertexes.get('C')?.from, new Set(['B'])); | ||
}); | ||
|
||
it('should remove vertexes without any edges', () => { | ||
const graph = new Graph(); | ||
graph.addEdge('A', 'B'); | ||
graph.addEdge('A', 'C'); | ||
graph.addEdge('B', 'C'); | ||
graph.deleteVertex('B'); | ||
|
||
assert.equal(graph.vertexes.size, 2); | ||
assert.equal(graph.vertexes.has('B'), false); | ||
assert.equal(graph.vertexes.has('A'), true); | ||
assert.equal(graph.vertexes.has('C'), true); | ||
assert.deepEqual(graph.vertexes.get('A')?.to, new Set(['C'])); | ||
assert.deepEqual(graph.vertexes.get('A')?.from.size, 0); | ||
assert.equal(graph.vertexes.get('C')?.to.size, 0); | ||
assert.deepEqual(graph.vertexes.get('C')?.from, new Set(['A'])); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { describe, it } from 'node:test'; | ||
import { setup } from '../../test/helpers/setup.js'; | ||
import { collectDynamicImports } from './collectDynamicImports.js'; | ||
import ts from 'typescript'; | ||
import assert from 'node:assert/strict'; | ||
|
||
const getProgram = (languageService: ts.LanguageService) => { | ||
const program = languageService.getProgram(); | ||
|
||
if (!program) { | ||
throw new Error('Program not found'); | ||
} | ||
|
||
return program; | ||
}; | ||
|
||
describe('collectDynamicImports', () => { | ||
it('should return a graph of dynamic imports', () => { | ||
const { languageService, fileService } = setup(); | ||
fileService.set('/app/main.ts', `import('./a.js');`); | ||
fileService.set('/app/a.ts', `export const a = 'a';`); | ||
|
||
const program = getProgram(languageService); | ||
|
||
const graph = collectDynamicImports({ | ||
fileService, | ||
program, | ||
}); | ||
|
||
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 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.ts', `export const a = 'a';`); | ||
|
||
const program = getProgram(languageService); | ||
|
||
const graph = collectDynamicImports({ | ||
fileService, | ||
program, | ||
}); | ||
|
||
assert.equal(graph.vertexes.size, 0); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import ts from 'typescript'; | ||
import { getFileFromModuleSpecifierText } from './getFileFromModuleSpecifierText.js'; | ||
import { FileService } from './FileService.js'; | ||
import { Graph } from './Graph.js'; | ||
|
||
export const collectDynamicImports = ({ | ||
program, | ||
fileService, | ||
}: { | ||
program: ts.Program; | ||
fileService: FileService; | ||
}) => { | ||
const graph = new Graph(); | ||
const files = fileService.getFileNames(); | ||
for (const file of files) { | ||
const sourceFile = program.getSourceFile(file); | ||
|
||
if (!sourceFile) { | ||
continue; | ||
} | ||
|
||
const visit = (node: ts.Node) => { | ||
if ( | ||
ts.isCallExpression(node) && | ||
node.expression.kind === ts.SyntaxKind.ImportKeyword && | ||
node.arguments[0] && | ||
ts.isStringLiteral(node.arguments[0]) | ||
) { | ||
const file = getFileFromModuleSpecifierText({ | ||
specifier: node.arguments[0].text, | ||
program, | ||
fileService, | ||
fileName: sourceFile.fileName, | ||
}); | ||
|
||
if (file) { | ||
graph.addEdge(sourceFile.fileName, file); | ||
} | ||
|
||
return; | ||
} | ||
|
||
node.forEachChild(visit); | ||
}; | ||
|
||
sourceFile.forEachChild(visit); | ||
} | ||
|
||
return graph; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import ts from 'typescript'; | ||
import { FileService } from './FileService.js'; | ||
|
||
export const getFileFromModuleSpecifierText = ({ | ||
specifier, | ||
fileName, | ||
program, | ||
fileService, | ||
}: { | ||
specifier: string; | ||
fileName: string; | ||
program: ts.Program; | ||
fileService: FileService; | ||
}) => | ||
ts.resolveModuleName(specifier, fileName, program.getCompilerOptions(), { | ||
fileExists(fileName) { | ||
return fileService.exists(fileName); | ||
}, | ||
readFile(fileName) { | ||
return fileService.get(fileName); | ||
}, | ||
}).resolvedModule?.resolvedFileName; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.