11const { eslintRulesExtra } = require ( "./official-eslint-rules" )
22const { pluginImportRulesExtra, pluginImportTypeScriptRulesExtra } = require ( "./plugin-import-rules" )
33const { pluginNodeRules } = require ( "./plugin-node-rules" )
4- const glob = require ( "fast-glob" )
4+ const fs = require ( "fs" )
5+ const path = require ( "path" )
56
6- const project = [ "./**/tsconfig.json" , "!./**/node_modules/**/tsconfig.json" ]
7+ const tsFiles = [ "**/*.tsx" , "**/*.ts" ]
8+ const project = [ "**/tsconfig.json" , "!**/node_modules/**/tsconfig.json" ]
79
8- const projectedBasedRules = glob . sync ( project , { onlyFiles : true , suppressErrors : true } ) . length !== 0
9- if ( ! projectedBasedRules ) {
10- console . warn (
11- "\x1b[33m%s\x1b[0m" ,
12- "No tsconfig.json found, disabling the project-based rules. To enable them, include all the **/*.ts(x)? files in the includes of the tsconfig.json files and run eslint again."
13- )
10+ function findOneFile ( cwd , fileEnding , ignoredFolders ) {
11+ // 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
12+ const files = fs . readdirSync ( cwd , { withFileTypes : true , recursive : false } )
13+ for ( const file of files ) {
14+ if ( file . isDirectory ( ) ) {
15+ if ( ! ignoredFolders . includes ( file . name ) ) {
16+ // if the folder is not ignored, search it recursively
17+ const found = findOneFile ( path . join ( cwd , file . name ) , fileEnding , ignoredFolders )
18+ if ( found ) {
19+ return true
20+ }
21+ }
22+ } else if ( file . name . endsWith ( fileEnding ) ) {
23+ // if the file ends with the given fileEnding, return true
24+ return true
25+ }
26+ }
27+ return false
28+ }
29+
30+ /** Check if there are any tsconfig.json files */
31+ function disableProjectBasedRules ( ) {
32+ const hasTsFile = findOneFile ( process . cwd ( ) , ".ts" , [ "node_modules" , ".git" ] )
33+ const hasTsConfig = findOneFile ( process . cwd ( ) , "tsconfig.json" , [ "node_modules" , ".git" ] )
34+
35+ // if there is no tsconfig.json file, but there are ts files, disable the project-based rules
36+ const disable = ! hasTsConfig && hasTsFile
37+
38+ if ( disable ) {
39+ console . warn (
40+ "\x1b[33m%s\x1b[0m" ,
41+ "No tsconfig.json found, disabling the project-based rules. To enable them, include all the **/*.ts(x)? files in the includes of the tsconfig.json files and run eslint again."
42+ )
43+ }
44+
45+ return disable
1446}
1547
16- // turn-off no-unused-vars for typescript files
17- const typeScriptEslintExtra = { ...eslintRulesExtra }
18- typeScriptEslintExtra [ "no-unused-vars" ] = "off"
48+ function javaScriptRules ( ) {
49+ // turn-off no-unused-vars for typescript files
50+ const typeScriptEslintExtra = { ...eslintRulesExtra }
51+ typeScriptEslintExtra [ "no-unused-vars" ] = "off"
52+
53+ return typeScriptEslintExtra
54+ }
1955
2056const pluginTypeScriptRulesExtra = {
2157 "@typescript-eslint/no-unused-vars" : [
@@ -46,8 +82,9 @@ const pluginTypeScriptRulesExtra = {
4682 // "@typescript-eslint/prefer-string-starts-ends-with": "error",
4783}
4884
49- const pluginTypeScriptProjectRules = projectedBasedRules
50- ? {
85+ const pluginTypeScriptProjectRules = disableProjectBasedRules ( )
86+ ? { }
87+ : {
5188 "@typescript-eslint/no-floating-promises" : "error" ,
5289 "@typescript-eslint/no-unnecessary-boolean-literal-compare" : "error" ,
5390 "@typescript-eslint/no-unnecessary-condition" : "error" ,
@@ -60,11 +97,10 @@ const pluginTypeScriptProjectRules = projectedBasedRules
6097 "@typescript-eslint/strict-boolean-expressions" : "error" ,
6198 "@typescript-eslint/switch-exhaustiveness-check" : "warn" ,
6299 }
63- : { }
64100
65101exports . tsConfig = {
66102 // TypeScript files
67- files : [ "**/*.tsx" , "**/*.ts" ] ,
103+ files : tsFiles ,
68104 parser : "@typescript-eslint/parser" ,
69105 parserOptions : {
70106 project,
@@ -80,7 +116,7 @@ exports.tsConfig = {
80116 "prettier" ,
81117 ] ,
82118 rules : {
83- ...typeScriptEslintExtra ,
119+ ...javaScriptRules ( ) ,
84120 ...pluginTypeScriptRulesExtra ,
85121 ...pluginTypeScriptProjectRules ,
86122 ...pluginNodeRules ,
0 commit comments