Skip to content

Commit 6ef55e0

Browse files
committed
Review: Update enzyme detector
1 parent c39ff24 commit 6ef55e0

File tree

1 file changed

+23
-25
lines changed

1 file changed

+23
-25
lines changed

packages/pluggable-widgets-tools/utils/enzyme-detector.js renamed to packages/pluggable-widgets-tools/src/utils/enzyme-detector.ts

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,47 @@
1-
const { existsSync, readdirSync, readFileSync, statSync } = require("fs");
2-
const { join } = require("path");
3-
const { yellow } = require("ansi-colors");
1+
import { existsSync, readdirSync, readFileSync, statSync } from "fs";
2+
import { join } from "path";
3+
import { yellow } from "ansi-colors";
44

5-
function checkForEnzymeUsage(srcDir = "src") {
5+
export function checkForEnzymeUsage(srcDir: string = "src"): void {
66
const projectRoot = process.cwd();
77
const srcPath = join(projectRoot, srcDir);
88

99
if (!existsSync(srcPath)) {
1010
return;
1111
}
1212

13-
const enzymeFiles = [];
13+
const enzymeFiles: string[] = [];
1414

15-
function scanDirectory(dir) {
15+
function scanDirectory(dir: string): void {
1616
try {
1717
const entries = readdirSync(dir);
18+
const isTestDir = /__tests__|[/\\]test[/\\]/.test(dir);
19+
1820
for (const entry of entries) {
1921
const fullPath = join(dir, entry);
2022
const stat = statSync(fullPath);
2123

2224
if (stat.isDirectory()) {
2325
scanDirectory(fullPath);
24-
} else if (
25-
stat.isFile() &&
26-
(entry.endsWith(".js") ||
27-
entry.endsWith(".jsx") ||
28-
entry.endsWith(".ts") ||
29-
entry.endsWith(".tsx"))
26+
continue;
27+
}
28+
29+
const isTestFile = /\.(spec|test)\.(jsx?|tsx?)$/.test(entry);
30+
if (!stat.isFile() || (!isTestFile && !isTestDir)) {
31+
continue;
32+
}
33+
34+
const content = readFileSync(fullPath, "utf8");
35+
if (
36+
/(from|require\s*\()\s*['"]enzyme['"]|enzyme.*(?:shallow|mount|render)|(?:shallow|mount|render).*enzyme/.test(
37+
content
38+
)
3039
) {
31-
const content = readFileSync(fullPath, "utf8");
32-
// Check for enzyme imports or requires
33-
if (
34-
/from\s+['"]enzyme['"]/.test(content) ||
35-
/require\s*\(\s*['"]enzyme['"]\s*\)/.test(content) ||
36-
/import\s+.*\s+from\s+['"]enzyme['"]/.test(content) ||
37-
/shallow|mount|render/.test(content) && /enzyme/.test(content)
38-
) {
39-
enzymeFiles.push(fullPath.replace(projectRoot, "."));
40-
}
40+
enzymeFiles.push(fullPath.replace(projectRoot, "."));
4141
}
4242
}
4343
} catch (error) {
44-
44+
console.error(`Error scanning directory ${dir}:`, error);
4545
}
4646
}
4747

@@ -64,5 +64,3 @@ function checkForEnzymeUsage(srcDir = "src") {
6464
console.log();
6565
}
6666
}
67-
68-
module.exports = { checkForEnzymeUsage };

0 commit comments

Comments
 (0)