Skip to content

Commit a3e36fe

Browse files
committed
fix: optimize TypeScript project disable search
1 parent 211cb65 commit a3e36fe

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

src/typescript.cts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import { eslintRulesExtra } from "./official-eslint-rules.cjs"
22
import { pluginImportRulesExtra, pluginImportTypeScriptRulesExtra } from "./plugin-import-rules.cjs"
33
import { pluginNodeRules } from "./plugin-node-rules.cjs"
44
import makeSynchronous from "make-synchronous"
5-
import { findOneFile } from "./utils.cjs"
5+
import { findFilesForGroups } from "./utils.cjs"
66
import type { GlobifiedEntry } from "globify-gitignore"
77
import { Linter } from "eslint"
88

99
const tsFiles = ["**/*.tsx", "**/*.ts", "**/*.mts", "**/*.cts"]
10-
const project = ["**/tsconfig.json", "!**/node_modules/**/tsconfig.json"]
10+
const tscConfigFiles = ["**/tsconfig.json", "!**/node_modules/**/tsconfig.json"]
1111

1212
async function globifyGitIgnoreFileWithDeps(cwd: string, include: boolean) {
1313
try {
@@ -54,18 +54,15 @@ function disableProjectBasedRules() {
5454
)
5555

5656
// check if there are any ts files
57-
const hasTsFile = findOneFile(cwd, tsFiles, ignore)
57+
const [hasTscConfig, hasTsFile] = findFilesForGroups(cwd, [tscConfigFiles, tsFiles], ignore)
5858

5959
// return if there are no ts files
6060
if (!hasTsFile) {
6161
return true
6262
}
6363

64-
// check if there is a tsconfig.json file
65-
const hasTsConfig = findOneFile(cwd, project, ignore)
66-
6764
// if there is no tsconfig.json file, but there are ts files, disable the project-based rules
68-
const disable = !hasTsConfig && hasTsFile
65+
const disable = !hasTscConfig && hasTsFile
6966

7067
if (disable) {
7168
console.warn(
@@ -132,7 +129,7 @@ export const tsConfig: Linter.ConfigOverride<Linter.RulesRecord> = {
132129
files: tsFiles,
133130
parser: "@typescript-eslint/parser",
134131
parserOptions: {
135-
project,
132+
project: tscConfigFiles,
136133
createDefaultProgram: true, // otherwise Eslint will error if a ts file is not covered by one of the tsconfig.json files
137134
},
138135
plugins: ["@typescript-eslint", "node", "import", "only-warn"],

src/utils.cts

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

5-
export function findOneFile(cwd: string, search: string[], ignored: string[]) {
6-
// 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
7-
const files = readdirSync(cwd, { withFileTypes: true, recursive: false })
5+
export function findFilesForGroups(cwd: string, searchGroups: string[][], ignored: string[]) {
6+
const status = searchGroups.map(() => false);
7+
searchDirectory(cwd, status, searchGroups, ignored);
8+
9+
return status
10+
}
11+
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 });
815
for (const file of files) {
9-
const path = join(cwd, file.name)
16+
const path = join(directory, file.name);
1017
if (file.isDirectory()) {
1118
if (!anymatch(ignored, path)) {
1219
// if the folder is not ignored, search it recursively
13-
const found = findOneFile(path, search, ignored)
14-
if (found) {
15-
return true
20+
searchDirectory(path, status, searchGroups, ignored);
21+
}
22+
} 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;
1627
}
1728
}
18-
} else if (anymatch(search, path)) {
19-
// if the file ends with the given fileEnding, return true
20-
return true
2129
}
2230
}
23-
return false
2431
}

0 commit comments

Comments
 (0)