Skip to content

Commit ea81064

Browse files
committed
fix: combine tsc/ts search for TypeScript project-based rules
1 parent a3e36fe commit ea81064

File tree

2 files changed

+32
-20
lines changed

2 files changed

+32
-20
lines changed

src/typescript.cts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,7 @@ function disableProjectBasedRules() {
5454
)
5555

5656
// check if there are any ts files
57-
const [hasTscConfig, hasTsFile] = findFilesForGroups(cwd, [tscConfigFiles, tsFiles], ignore)
58-
59-
// return if there are no ts files
60-
if (!hasTsFile) {
61-
return true
62-
}
57+
const [hasTscConfig, hasTsFile] = findFilesForGroups(cwd, tscConfigFiles, tsFiles, ignore)
6358

6459
// if there is no tsconfig.json file, but there are ts files, disable the project-based rules
6560
const disable = !hasTscConfig && hasTsFile

src/utils.cts

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,47 @@ import { readdirSync } from "fs"
22
import { join } from "path"
33
import { default as anymatch } from "anymatch"
44

5-
export function findFilesForGroups(cwd: string, searchGroups: string[][], ignored: string[]) {
6-
const status = searchGroups.map(() => false);
7-
searchDirectory(cwd, status, searchGroups, ignored);
5+
export function findFilesForGroups(
6+
cwd: string,
7+
earlyExitSearchGroup: string[],
8+
exhaustiveSearchGroup: string[],
9+
ignored: string[],
10+
) {
11+
const status = [false, false]
12+
searchDirectory(cwd, status, earlyExitSearchGroup, exhaustiveSearchGroup, ignored)
813

914
return status
1015
}
1116

12-
function searchDirectory(directory: string, status: boolean[], searchGroups: string[][], ignored: string[]) {
13-
// recursively search the current folder for a file with the given fileEnding, ignoring the given folders, and return true as soon as one is found
14-
const files = readdirSync(directory, { withFileTypes: true, recursive: false });
17+
function searchDirectory(
18+
directory: string,
19+
status: boolean[],
20+
earlyExitSearchGroup: string[],
21+
exhaustiveSearchGroup: string[],
22+
ignored: string[],
23+
): boolean {
24+
// recursively search the current folder for a file with the given fileEnding, ignoring the given folders, and return true as soon as one is found
25+
const files = readdirSync(directory, { withFileTypes: true, recursive: false })
1526
for (const file of files) {
16-
const path = join(directory, file.name);
27+
const path = join(directory, file.name)
1728
if (file.isDirectory()) {
29+
// if the folder is not ignored, search it recursively
1830
if (!anymatch(ignored, path)) {
19-
// if the folder is not ignored, search it recursively
20-
searchDirectory(path, status, searchGroups, ignored);
31+
if (searchDirectory(path, status, earlyExitSearchGroup, exhaustiveSearchGroup, ignored)) {
32+
return true // exit
33+
}
2134
}
2235
} else {
23-
// for each search group, check if the file matches any of the patterns
24-
for (const [index, searchGroup] of searchGroups.entries()) {
25-
if (!status[index] && anymatch(searchGroup, path)) {
26-
status[index] = true;
27-
}
36+
// check the early exit search group first
37+
status[0] = status[0] || anymatch(exhaustiveSearchGroup, path)
38+
if (status[0]) {
39+
return true // exit
2840
}
41+
42+
// check the exhaustive search group
43+
status[1] = status[1] || anymatch(exhaustiveSearchGroup, path)
2944
}
3045
}
46+
47+
return false // continue
3148
}

0 commit comments

Comments
 (0)