-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize the detection of circular dependencies
- Loading branch information
Showing
8 changed files
with
152 additions
and
685 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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`Detect circular dependencies in the specified directory. 1`] = `"/a.js/b.js-/b.js/a.js::/c.js/a.js-/a.js/b.js-/b.js/c.js::/c.js/a.js-/a.js/c.js"`; | ||
exports[`Detect circular dependencies in the specified directory. 1`] = `"a.js -> b.js , b.js -> a.js :: a.js -> b.js , b.js -> c.js , c.js -> a.js :: a.js -> c.js , c.js -> a.js"`; |
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 |
---|---|---|
@@ -1,32 +1,18 @@ | ||
import { expect, test } from 'vitest' | ||
import { expect, test } from "vitest"; | ||
import { fileURLToPath } from "node:url"; | ||
import path, { dirname } from "node:path"; | ||
import { checkCycle , Cycle} from '../../index.js' | ||
|
||
|
||
function normalizePaths(cwd:string,node: Array<Array<Cycle>>): string { | ||
return node.map( | ||
item => | ||
item.map( | ||
x => ({ | ||
...x, | ||
source:x.source.replace(cwd,"").replace(/^\\/,"/"), | ||
target:x.target.replace(cwd,"").replace(/^\\/,"/"), | ||
}) | ||
).map(x => `${x.source}${x.target}`).join("-") | ||
).join("::") | ||
} | ||
import { checkCycle } from "../../index.js"; | ||
import { normalizePaths } from "../utils/normalize-paths.js"; | ||
|
||
const __filename = fileURLToPath(import.meta.url); | ||
|
||
test('Detect circular dependencies in the specified directory.', () => { | ||
const cwd = path.resolve(dirname(__filename),"features","cycle"); | ||
const response = checkCycle({ | ||
cwd, | ||
}) | ||
|
||
const normalizedPaths = normalizePaths(cwd,response); | ||
test("Detect circular dependencies in the specified directory.", () => { | ||
const cwd = path.resolve(dirname(__filename), "features", "cycle"); | ||
const response = checkCycle({ | ||
cwd, | ||
}); | ||
|
||
expect(normalizedPaths).toMatchSnapshot() | ||
const normalizedPaths = normalizePaths(cwd, response); | ||
|
||
}) | ||
expect(normalizedPaths).toMatchSnapshot(); | ||
}); |
266 changes: 2 additions & 264 deletions
266
__test__/check_dependencies/__snapshots__/index.spec.ts.snap
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 |
---|---|---|
@@ -1,267 +1,5 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`The dependency file list of the specified file should be obtained normally. 1`] = ` | ||
[ | ||
[ | ||
{ | ||
"astNode": { | ||
"loc": { | ||
"end": { | ||
"col": 29, | ||
"line": 1, | ||
}, | ||
"start": { | ||
"col": 1, | ||
"line": 1, | ||
}, | ||
}, | ||
"span": { | ||
"end": 28, | ||
"start": 0, | ||
}, | ||
}, | ||
"source": "/a.js", | ||
"target": "/utils.js", | ||
}, | ||
{ | ||
"astNode": { | ||
"loc": { | ||
"end": { | ||
"col": 25, | ||
"line": 1, | ||
}, | ||
"start": { | ||
"col": 1, | ||
"line": 1, | ||
}, | ||
}, | ||
"span": { | ||
"end": 24, | ||
"start": 0, | ||
}, | ||
}, | ||
"source": "/c.js", | ||
"target": "/a.js", | ||
}, | ||
], | ||
[ | ||
{ | ||
"astNode": { | ||
"loc": { | ||
"end": { | ||
"col": 29, | ||
"line": 1, | ||
}, | ||
"start": { | ||
"col": 1, | ||
"line": 1, | ||
}, | ||
}, | ||
"span": { | ||
"end": 28, | ||
"start": 0, | ||
}, | ||
}, | ||
"source": "/b.js", | ||
"target": "/utils.js", | ||
}, | ||
{ | ||
"astNode": { | ||
"loc": { | ||
"end": { | ||
"col": 25, | ||
"line": 2, | ||
}, | ||
"start": { | ||
"col": 1, | ||
"line": 2, | ||
}, | ||
}, | ||
"span": { | ||
"end": 49, | ||
"start": 25, | ||
}, | ||
}, | ||
"source": "/c.js", | ||
"target": "/b.js", | ||
}, | ||
], | ||
[ | ||
{ | ||
"astNode": { | ||
"loc": { | ||
"end": { | ||
"col": 25, | ||
"line": 1, | ||
}, | ||
"start": { | ||
"col": 1, | ||
"line": 1, | ||
}, | ||
}, | ||
"span": { | ||
"end": 24, | ||
"start": 0, | ||
}, | ||
}, | ||
"source": "/c.js", | ||
"target": "/a.js", | ||
}, | ||
], | ||
[ | ||
{ | ||
"astNode": { | ||
"loc": { | ||
"end": { | ||
"col": 25, | ||
"line": 2, | ||
}, | ||
"start": { | ||
"col": 1, | ||
"line": 2, | ||
}, | ||
}, | ||
"span": { | ||
"end": 49, | ||
"start": 25, | ||
}, | ||
}, | ||
"source": "/c.js", | ||
"target": "/b.js", | ||
}, | ||
], | ||
] | ||
`; | ||
exports[`The dependency file list of the specified file should be obtained normally. 1`] = `"c.js -> a.js :: c.js -> a.js , a.js -> utils.js :: c.js -> b.js :: c.js -> b.js , b.js -> utils.js"`; | ||
|
||
exports[`The dependency file list of the specified file should be obtained normally. 2`] = ` | ||
[ | ||
[ | ||
{ | ||
"astNode": { | ||
"loc": { | ||
"end": { | ||
"col": 29, | ||
"line": 1, | ||
}, | ||
"start": { | ||
"col": 1, | ||
"line": 1, | ||
}, | ||
}, | ||
"span": { | ||
"end": 28, | ||
"start": 0, | ||
}, | ||
}, | ||
"source": "/a.js", | ||
"target": "/utils.js", | ||
}, | ||
{ | ||
"astNode": { | ||
"loc": { | ||
"end": { | ||
"col": 25, | ||
"line": 1, | ||
}, | ||
"start": { | ||
"col": 1, | ||
"line": 1, | ||
}, | ||
}, | ||
"span": { | ||
"end": 24, | ||
"start": 0, | ||
}, | ||
}, | ||
"source": "/c.js", | ||
"target": "/a.js", | ||
}, | ||
], | ||
[ | ||
{ | ||
"astNode": { | ||
"loc": { | ||
"end": { | ||
"col": 29, | ||
"line": 1, | ||
}, | ||
"start": { | ||
"col": 1, | ||
"line": 1, | ||
}, | ||
}, | ||
"span": { | ||
"end": 28, | ||
"start": 0, | ||
}, | ||
}, | ||
"source": "/b.js", | ||
"target": "/utils.js", | ||
}, | ||
{ | ||
"astNode": { | ||
"loc": { | ||
"end": { | ||
"col": 25, | ||
"line": 2, | ||
}, | ||
"start": { | ||
"col": 1, | ||
"line": 2, | ||
}, | ||
}, | ||
"span": { | ||
"end": 49, | ||
"start": 25, | ||
}, | ||
}, | ||
"source": "/c.js", | ||
"target": "/b.js", | ||
}, | ||
], | ||
[ | ||
{ | ||
"astNode": { | ||
"loc": { | ||
"end": { | ||
"col": 25, | ||
"line": 1, | ||
}, | ||
"start": { | ||
"col": 1, | ||
"line": 1, | ||
}, | ||
}, | ||
"span": { | ||
"end": 24, | ||
"start": 0, | ||
}, | ||
}, | ||
"source": "/c.js", | ||
"target": "/a.js", | ||
}, | ||
], | ||
[ | ||
{ | ||
"astNode": { | ||
"loc": { | ||
"end": { | ||
"col": 25, | ||
"line": 2, | ||
}, | ||
"start": { | ||
"col": 1, | ||
"line": 2, | ||
}, | ||
}, | ||
"span": { | ||
"end": 49, | ||
"start": 25, | ||
}, | ||
}, | ||
"source": "/c.js", | ||
"target": "/b.js", | ||
}, | ||
], | ||
] | ||
`; | ||
exports[`The dependency file list of the specified file should be obtained normally. 2`] = `"c.js -> a.js :: c.js -> a.js , a.js -> utils.js :: c.js -> b.js :: c.js -> b.js , b.js -> utils.js"`; |
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 |
---|---|---|
@@ -1,54 +1,32 @@ | ||
import { expect, test, } from 'vitest' | ||
import { expect, test } from "vitest"; | ||
import { fileURLToPath } from "node:url"; | ||
import path, { dirname } from "node:path"; | ||
import { checkDependencies, Cycle, DependencyNode} from '../../index.js' | ||
|
||
|
||
function normalizePaths(cwd:string,node: Array<Array<Cycle>>): Array<Array<Cycle>> { | ||
return node.map( | ||
item => | ||
item.map( | ||
x => ({ | ||
...x, | ||
source:x.source.replace(cwd,"").replace(/^\\/,"/"), | ||
target:x.target.replace(cwd,"").replace(/^\\/,"/"), | ||
}) | ||
).sort((a,b)=>`${a.source}${a.target}`.localeCompare(`${b.source}${b.target}`)) | ||
).sort((a,b)=> { | ||
|
||
return a.map(x => x.source).join("").localeCompare(b.map(x => x.source).join("")) | ||
|
||
}); | ||
} | ||
import { checkDependencies, Cycle, DependencyNode } from "../../index.js"; | ||
import { normalizePaths } from "../utils/normalize-paths.js"; | ||
|
||
const __filename = fileURLToPath(import.meta.url); | ||
|
||
test("The dependency file list of the specified file should be obtained normally.", () => { | ||
const cwd = path.resolve(dirname(__filename), "features", "normal"); | ||
const response = checkDependencies(path.join(cwd, "c.js"), { | ||
cwd, | ||
}); | ||
|
||
test('The dependency file list of the specified file should be obtained normally.', () => { | ||
const cwd = path.resolve(dirname(__filename),"features","normal"); | ||
const response = checkDependencies(path.join(cwd,"c.js"),{ | ||
cwd, | ||
}) | ||
|
||
const normalizedPaths = normalizePaths(cwd,response); | ||
|
||
expect(normalizedPaths).toMatchSnapshot() | ||
|
||
}) | ||
|
||
test('The dependency file list of the specified file should be obtained normally.', () => { | ||
const cwd = path.resolve(dirname(__filename),"features","alias"); | ||
const response = checkDependencies(path.join(cwd,"c.js"),{ | ||
alias:{ | ||
"@":[cwd] | ||
}, | ||
cwd, | ||
}) | ||
const normalizedPaths = normalizePaths(cwd, response); | ||
|
||
const normalizedPaths = normalizePaths(cwd,response); | ||
expect(normalizedPaths).toMatchSnapshot(); | ||
}); | ||
|
||
expect(normalizedPaths).toMatchSnapshot() | ||
|
||
test("The dependency file list of the specified file should be obtained normally.", () => { | ||
const cwd = path.resolve(dirname(__filename), "features", "alias"); | ||
const response = checkDependencies(path.join(cwd, "c.js"), { | ||
alias: { | ||
"@": [cwd], | ||
}, | ||
cwd, | ||
}); | ||
|
||
const normalizedPaths = normalizePaths(cwd, response); | ||
|
||
}) | ||
expect(normalizedPaths).toMatchSnapshot(); | ||
}); |
Oops, something went wrong.