Skip to content

Commit

Permalink
Optimize the detection of circular dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
ityuany committed Oct 7, 2024
1 parent 55af64f commit cde16f3
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 685 deletions.
2 changes: 1 addition & 1 deletion __test__/check_cycle/__snapshots__/index.spec.ts.snap
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"`;
36 changes: 11 additions & 25 deletions __test__/check_cycle/index.spec.ts
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 __test__/check_dependencies/__snapshots__/index.spec.ts.snap
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"`;
66 changes: 22 additions & 44 deletions __test__/check_dependencies/index.spec.ts
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();
});
Loading

0 comments on commit cde16f3

Please sign in to comment.