Skip to content

Commit

Permalink
fix: combine tsc/ts search for TypeScript project-based rules
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Aug 15, 2024
1 parent a3e36fe commit ea81064
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 20 deletions.
7 changes: 1 addition & 6 deletions src/typescript.cts
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,7 @@ function disableProjectBasedRules() {
)

// check if there are any ts files
const [hasTscConfig, hasTsFile] = findFilesForGroups(cwd, [tscConfigFiles, tsFiles], ignore)

// return if there are no ts files
if (!hasTsFile) {
return true
}
const [hasTscConfig, hasTsFile] = findFilesForGroups(cwd, tscConfigFiles, tsFiles, ignore)

// if there is no tsconfig.json file, but there are ts files, disable the project-based rules
const disable = !hasTscConfig && hasTsFile
Expand Down
45 changes: 31 additions & 14 deletions src/utils.cts
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,47 @@ import { readdirSync } from "fs"
import { join } from "path"
import { default as anymatch } from "anymatch"

export function findFilesForGroups(cwd: string, searchGroups: string[][], ignored: string[]) {
const status = searchGroups.map(() => false);
searchDirectory(cwd, status, searchGroups, ignored);
export function findFilesForGroups(
cwd: string,
earlyExitSearchGroup: string[],
exhaustiveSearchGroup: string[],
ignored: string[],
) {
const status = [false, false]
searchDirectory(cwd, status, earlyExitSearchGroup, exhaustiveSearchGroup, ignored)

return status
}

function searchDirectory(directory: string, status: boolean[], searchGroups: string[][], ignored: string[]) {
// 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
const files = readdirSync(directory, { withFileTypes: true, recursive: false });
function searchDirectory(
directory: string,
status: boolean[],
earlyExitSearchGroup: string[],
exhaustiveSearchGroup: string[],
ignored: string[],
): boolean {
// 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
const files = readdirSync(directory, { withFileTypes: true, recursive: false })
for (const file of files) {
const path = join(directory, file.name);
const path = join(directory, file.name)
if (file.isDirectory()) {
// if the folder is not ignored, search it recursively
if (!anymatch(ignored, path)) {
// if the folder is not ignored, search it recursively
searchDirectory(path, status, searchGroups, ignored);
if (searchDirectory(path, status, earlyExitSearchGroup, exhaustiveSearchGroup, ignored)) {
return true // exit
}
}
} else {
// for each search group, check if the file matches any of the patterns
for (const [index, searchGroup] of searchGroups.entries()) {
if (!status[index] && anymatch(searchGroup, path)) {
status[index] = true;
}
// check the early exit search group first
status[0] = status[0] || anymatch(exhaustiveSearchGroup, path)
if (status[0]) {
return true // exit
}

// check the exhaustive search group
status[1] = status[1] || anymatch(exhaustiveSearchGroup, path)
}
}

return false // continue
}

0 comments on commit ea81064

Please sign in to comment.