@@ -2,17 +2,30 @@ import { eslintRulesExtra } from "./official-eslint-rules.cjs"
2
2
import { pluginImportRulesExtra , pluginImportTypeScriptRulesExtra } from "./plugin-import-rules.cjs"
3
3
import { pluginNodeRules } from "./plugin-node-rules.cjs"
4
4
import makeSynchronous from "make-synchronous"
5
- import { findOneFile } from "./utils.cjs"
5
+ import { findFilesForGroups } from "./utils.cjs"
6
6
import type { GlobifiedEntry } from "globify-gitignore"
7
7
import { Linter } from "eslint"
8
8
9
9
const tsFiles = [ "**/*.tsx" , "**/*.ts" , "**/*.mts" , "**/*.cts" ]
10
- const project = [ "**/tsconfig.json" , "!**/node_modules/**/tsconfig.json" ]
10
+ const tscConfigFiles = [ "**/tsconfig.json" , "!**/node_modules/**/tsconfig.json" ]
11
11
12
- function globifyGitIgnoreFileWithDeps ( cwd : string , include : boolean ) {
13
- // eslint-disable-next-line @typescript-eslint/no-var-requires
14
- const { globifyGitIgnoreFile } = require ( "globify-gitignore" ) as typeof import ( "globify-gitignore" ) // prettier-ignore
15
- return globifyGitIgnoreFile ( cwd , include )
12
+ async function globifyGitIgnoreFileWithDeps ( cwd : string , include : boolean ) {
13
+ try {
14
+ // import in the function to allow makeSynchronous to work
15
+ /* eslint-disable @typescript-eslint/no-var-requires */
16
+ const { globifyGitIgnoreFile } = require ( "globify-gitignore" ) as typeof import ( "globify-gitignore" ) // prettier-ignore
17
+ const { existsSync } = require ( "fs" ) as typeof import ( "fs" )
18
+ const { join } = require ( "path" ) as typeof import ( "path" )
19
+ /* eslint-enable @typescript-eslint/no-var-requires */
20
+
21
+ if ( ! existsSync ( join ( cwd , ".gitignore" ) ) ) {
22
+ return [ ]
23
+ }
24
+ return await globifyGitIgnoreFile ( cwd , include )
25
+ } catch ( error ) {
26
+ console . error ( error )
27
+ return [ ]
28
+ }
16
29
}
17
30
const globifyGitIgnoreFileSync = makeSynchronous ( globifyGitIgnoreFileWithDeps ) as (
18
31
cwd : string ,
@@ -41,18 +54,10 @@ function disableProjectBasedRules() {
41
54
)
42
55
43
56
// check if there are any ts files
44
- const hasTsFile = findOneFile ( cwd , tsFiles , ignore )
45
-
46
- // return if there are no ts files
47
- if ( ! hasTsFile ) {
48
- return true
49
- }
50
-
51
- // check if there is a tsconfig.json file
52
- const hasTsConfig = findOneFile ( cwd , project , ignore )
57
+ const [ hasTscConfig , hasTsFile ] = findFilesForGroups ( cwd , tscConfigFiles , tsFiles , ignore )
53
58
54
59
// if there is no tsconfig.json file, but there are ts files, disable the project-based rules
55
- const disable = ! hasTsConfig && hasTsFile
60
+ const disable = ! hasTscConfig && hasTsFile
56
61
57
62
if ( disable ) {
58
63
console . warn (
@@ -119,7 +124,7 @@ export const tsConfig: Linter.ConfigOverride<Linter.RulesRecord> = {
119
124
files : tsFiles ,
120
125
parser : "@typescript-eslint/parser" ,
121
126
parserOptions : {
122
- project,
127
+ project : tscConfigFiles ,
123
128
createDefaultProgram : true , // otherwise Eslint will error if a ts file is not covered by one of the tsconfig.json files
124
129
} ,
125
130
plugins : [ "@typescript-eslint" , "node" , "import" , "only-warn" ] ,
0 commit comments