Skip to content

Commit 016d2ef

Browse files
committed
feat: refactor ut
1 parent 4d3cf8e commit 016d2ef

File tree

14 files changed

+60
-24
lines changed

14 files changed

+60
-24
lines changed

.vscode/launch.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"type": "node",
6+
"name": "vscode-jest-tests",
7+
"request": "launch",
8+
"args": [
9+
"--runInBand",
10+
"--watchAll=false"
11+
],
12+
"cwd": "${workspaceFolder}",
13+
"sourceMaps": true,
14+
"console": "integratedTerminal",
15+
"internalConsoleOptions": "neverOpen",
16+
"disableOptimisticBPs": true,
17+
"program": "${workspaceFolder}/node_modules/.bin/jest"
18+
}
19+
]
20+
}

.vscode/settings.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@
4444
"editor.tabSize": 2,
4545
"editor.fontSize": 14,
4646
"cSpell.words": [
47+
"graphlib",
4748
"testfile",
49+
"testproject",
4850
"Theo"
4951
]
5052
}

src/cli.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ try {
3131
const result = scanDirectoryWithResult(directory)
3232
if (result.haveCycle) {
3333
error(`Circular dependency existed in ${directory}`.red)
34-
forEach(result.cyclies, (cycle, index) => {
34+
forEach(result.cycleList, (cycle, index) => {
3535
error(`\ncycle ${index + 1}, size (${cycle.length}):${cycle.length === 1 ? " this file import itself".grey : " these files circular import each other".cyan}\n`)
3636
// @ts-ignore
3737
forEach(mapAbsPathesToRelPathes(cycle), c => error(` ${c}`.red))

src/graph.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import { FileImportDescription } from "./type";
55
/**
66
* calculate cycle import file
77
*
8-
* @param fileAbsPathes files absolutely path
8+
* @param fileAbsPathList files absolutely path
99
* @param imports each file's import
1010
*/
11-
export const calculateCycleImport = (fileAbsPathes: string[], imports: FileImportDescription[]) => {
11+
export const calculateCycleImport = (fileAbsPathList: string[], imports: FileImportDescription[]) => {
1212
const new_graph = new Graph({ directed: true })
13-
new_graph.setNodes(fileAbsPathes)
13+
new_graph.setNodes(fileAbsPathList)
1414
forEach(imports, i => new_graph.setEdge(i.fromFile, i.importFile))
1515
return alg.findCycles(new_graph)
1616
}

src/processor.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { resolveFilePath } from "./file";
22
import { FileImportDescription } from "./type";
3-
import {parse} from "@babel/parser";
3+
import { parse } from "@babel/parser";
44
import traverse from "@babel/traverse";
55

66
/**
@@ -21,7 +21,7 @@ export const findFileDependencies = (fileAbsolutePath: string, fileCodeString: s
2121
})
2222
traverse(ast, {
2323
ImportDeclaration: (p) => {
24-
const {node} = p;
24+
const { node } = p;
2525
if (node.source) {
2626
const sourceFile = resolveFilePath(fileAbsolutePath, node.source.value)
2727
if (sourceFile) {
@@ -34,7 +34,7 @@ export const findFileDependencies = (fileAbsolutePath: string, fileCodeString: s
3434
}
3535
},
3636
ExportNamedDeclaration: (p) => {
37-
const {node} = p;
37+
const { node } = p;
3838
if (node.source) {
3939
const sourceFile = resolveFilePath(fileAbsolutePath, node.source.value)
4040
if (sourceFile) {
@@ -47,7 +47,7 @@ export const findFileDependencies = (fileAbsolutePath: string, fileCodeString: s
4747
}
4848
},
4949
ExportAllDeclaration: (p) => {
50-
const {node} = p;
50+
const { node } = p;
5151
if (node.source) {
5252
const sourceFile = resolveFilePath(fileAbsolutePath, node.source.value)
5353
if (sourceFile) {
@@ -60,8 +60,8 @@ export const findFileDependencies = (fileAbsolutePath: string, fileCodeString: s
6060
}
6161
},
6262
CallExpression: (p) => {
63-
const {node} = p;
64-
if (node.callee.type === "Identifier" && node.callee.name === "require") {
63+
const { node } = p;
64+
if (node.callee.type === "Identifier" && (node.callee.name === "require" || node.callee.name === "import")) {
6565
if (node.arguments.length >= 1 && node.arguments[0].type === "StringLiteral") {
6666
const sourceFile = resolveFilePath(fileAbsolutePath, node.arguments[0].value)
6767
if (sourceFile) {

src/scanner.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ export const scanDirectoryWithResult = (directory: string): ScanResult => {
2727
if (result && result.length > 0) {
2828
return {
2929
haveCycle: true,
30-
cyclies: result,
30+
cycleList: result,
3131
nodes: filePath,
3232
imports: filteredImports,
3333
}
3434
} else {
3535
return {
3636
haveCycle: false,
37-
cyclies: [],
37+
cycleList: [],
3838
nodes: filePath,
3939
imports: filteredImports,
4040
}

src/type.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export interface PackageJson {
1414
export type Extension = "js" | "ts" | "tsx" | "jsx" | "mjs"
1515

1616
/**
17-
* circular dependent files
17+
* circular dependent files, each item will be a absolute file path
1818
*/
1919
export type Cycle = string[];
2020

@@ -44,7 +44,7 @@ export interface ScanResult {
4444
* wether these files have cycle dependency
4545
*/
4646
haveCycle: boolean;
47-
cyclies?: Cycle[];
47+
cycleList?: Cycle[];
4848
nodes?: string[];
4949
imports?: FileImportDescription[];
5050
}
@@ -54,11 +54,9 @@ export interface ScanResult {
5454
*/
5555
export interface ReportVO {
5656
/**
57-
* scnaed files
57+
* scanned files
5858
*/
59-
nodes: {
60-
name: string
61-
}[];
59+
nodes: { name: string }[];
6260
/**
6361
* files' deps
6462
*/
@@ -67,4 +65,4 @@ export interface ReportVO {
6765
target: string;
6866
value?: string;
6967
}[]
70-
}
68+
}

tests/scanner.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { scanDirectoryWithResult } from "../src/scanner";
22
import { join } from "path";
3+
import { testfile1AbsPath, testfile3AbsPath, testfile4AbsPath, testfile5AbsPath, testfile6AbsPath } from "./test.base";
34

45
describe('scanner related test', () => {
56

@@ -8,5 +9,18 @@ describe('scanner related test', () => {
89
expect(result.haveCycle).toEqual(false)
910
})
1011

12+
test('should scan cycles in result', () => {
13+
const result = scanDirectoryWithResult(join(__dirname, "./testproject"))
14+
expect(result.haveCycle).toEqual(true)
15+
expect(result.cycleList?.[0]).toStrictEqual([
16+
testfile5AbsPath,
17+
testfile1AbsPath,
18+
])
19+
expect(result.cycleList?.[1]).toStrictEqual([
20+
testfile3AbsPath,
21+
testfile4AbsPath,
22+
testfile6AbsPath,
23+
])
24+
})
1125

1226
})

tests/test.base.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { FileImportDescription } from "../src/type";
33

44
export const cwd: string = require("process").cwd();
55

6-
export const testfile1RelativePath = normalize("tests/testproject/d1/testfile.js")
6+
export const testfile1RelativePath = normalize("tests/testproject/d1/testfile.ts")
77
export const testfile2RelativePath = normalize("tests/testproject/d2/testfile2.js")
88
export const testfile3RelativePath = normalize("tests/testproject/d2/d3/testfile3.js")
99
export const testfile4RelativePath = normalize("tests/testproject/d2/d3/testfile4.js")

tests/testproject/d1/testfile.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/testproject/d1/testfile.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import "../testfile5.ts"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
import "../testfile4"
1+
import "../testfile4"

tests/testproject/testfile5.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
import "./d1/testfile"
1+
export * from "./d1/testfile"
2+
export const a = 15;

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"dom"
1010
],
1111
"module": "commonjs",
12-
"target": "es5",
12+
"target": "ES2015",
13+
"sourceMap": true,
1314
"moduleResolution": "node",
1415
"charset": "utf-8",
1516
"emitDecoratorMetadata": true,

0 commit comments

Comments
 (0)