Skip to content

Commit f295b9b

Browse files
committed
feat: support import func
1 parent 016d2ef commit f295b9b

File tree

8 files changed

+40
-9
lines changed

8 files changed

+40
-9
lines changed

.vscode/settings.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,8 @@
4848
"testfile",
4949
"testproject",
5050
"Theo"
51-
]
51+
],
52+
"[typescript]": {
53+
"editor.defaultFormatter": "vscode.typescript-language-features"
54+
}
5255
}

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2018 Theo Sun
3+
Copyright (c) 2018 - NOW Theo Sun
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
ES6 circular import check tool, support `js`, `ts`, `jsx`, `tsx` and `mjs` files, and will ignore all `node_modules` files.
99

10-
Support `import`, `export` keywords and `require()` function now
10+
Support `import`, `export` keywords and `require()`, `import()` function now
1111

1212
## Why do we need this tool ?
1313

@@ -109,3 +109,7 @@ or
109109
Congratulation! Not found circular dependency in tests/testproject2
110110
111111
```
112+
113+
## [CHANGELOG](./CHANGELOG.md)
114+
115+
## [LICENSE (MIT)](./LICENSE)

src/file.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { sync } from "glob";
33
import { join as pathJoin, dirname, join, normalize, relative } from "path";
44
import { join as arrayJoin, map, isArray, isString, concat, filter, keys } from "@newdash/newdash";
55
import { includes } from "@newdash/newdash/includes"
6-
import { readFileSync, writeFileSync } from "fs";
6+
import { readFileSync, writeFileSync, existsSync } from "fs";
77
import { cwd } from "process";
88
import { tmpdir, platform } from "os";
99
import { exec } from "child_process";
@@ -13,6 +13,10 @@ require.extensions[".jsx"] = require.extensions[".js"]
1313
require.extensions[".tsx"] = require.extensions[".js"]
1414
require.extensions[".mjs"] = require.extensions[".js"]
1515

16+
const extensions = [
17+
"js", "jsx", "ts", "tsx", "mjs"
18+
]
19+
1620
const { resolve } = require
1721

1822
export const allDependencies = (absPath: string) => {
@@ -82,6 +86,7 @@ export const readFile = (absolutePath: string) => {
8286
export const resolveFilePath = (fromFileAbsolutePath: string, importFileRelativePath: string) => {
8387
const dir = dirname(fromFileAbsolutePath);
8488
const targetPath = join(dir, importFileRelativePath);
89+
// to do replace nodejs resolve function
8590
try {
8691
return normalize(resolve(targetPath));
8792
} catch (error) {

src/processor.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,13 @@ export const findFileDependencies = (fileAbsolutePath: string, fileCodeString: s
6161
},
6262
CallExpression: (p) => {
6363
const { node } = p;
64-
if (node.callee.type === "Identifier" && (node.callee.name === "require" || node.callee.name === "import")) {
65-
if (node.arguments.length >= 1 && node.arguments[0].type === "StringLiteral") {
66-
const sourceFile = resolveFilePath(fileAbsolutePath, node.arguments[0].value)
64+
if ((
65+
node.callee?.type === "Identifier" && node.callee.name === "require" ||
66+
node.callee?.type === "Import"
67+
)) {
68+
if (node.arguments?.length >= 1 && node?.arguments[0].type === "StringLiteral") {
69+
const importRelPath = node.arguments[0].value;
70+
const sourceFile = resolveFilePath(fileAbsolutePath, importRelPath)
6771
if (sourceFile) {
6872
result.push({
6973
fromFile: fileAbsolutePath,

tests/processor.spec.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,20 @@ describe('processor tests', () => {
3939
test('should parse minified code correctly', () => {
4040
// this gave false positives in the old regex / non-babel processor implementation, because it contains a string that looks like export from but isn't
4141
const Proj6MinifiedAbsPath = join(__dirname, "./testproject6/util/base64.js")
42-
const deps = findFileDependencies(Proj6MinifiedAbsPath, readFileSync(Proj6MinifiedAbsPath, {encoding: "utf8"}))
42+
const deps = findFileDependencies(Proj6MinifiedAbsPath, readFileSync(Proj6MinifiedAbsPath, { encoding: "utf8" }))
4343
expect(deps).toHaveLength(0);
4444
});
4545

46+
test('should support import() function', () => {
47+
const path = join(__dirname, "./testproject/d5/testfile7.mjs")
48+
const deps = findFileDependencies(path, readFileSync(path, { encoding: "utf8" }))
49+
expect(deps).toHaveLength(1)
50+
});
51+
4652
test('should be able to parse a variety of different kinds of imports', () => {
4753
const Proj7File1AbsPath = join(__dirname, "./testproject7/file1.js")
4854
const Proj7File2AbsPath = join(__dirname, "./testproject7/file2.js")
49-
const deps = findFileDependencies(Proj7File1AbsPath, readFileSync(Proj7File1AbsPath, {encoding: "utf8"}))
55+
const deps = findFileDependencies(Proj7File1AbsPath, readFileSync(Proj7File1AbsPath, { encoding: "utf8" }))
5056
expect(deps).toEqual([
5157
{
5258
fromFile: Proj7File1AbsPath,

tests/testproject/d5/d6/testfile8.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
const { a } = require("../testfile7.mjs")
3+
a += 2

tests/testproject/d5/testfile7.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
async () => {
3+
await import("./d6/testfile8.mjs")
4+
}
5+
6+
export const a = 11

0 commit comments

Comments
 (0)