1
1
const { eslintRulesExtra } = require ( "./official-eslint-rules" )
2
2
const { pluginImportRulesExtra, pluginImportTypeScriptRulesExtra } = require ( "./plugin-import-rules" )
3
3
const { pluginNodeRules } = require ( "./plugin-node-rules" )
4
- const glob = require ( "fast-glob" )
4
+ const fs = require ( "fs" )
5
+ const path = require ( "path" )
5
6
6
- const project = [ "./**/tsconfig.json" , "!./**/node_modules/**/tsconfig.json" ]
7
+ const tsFiles = [ "**/*.tsx" , "**/*.ts" ]
8
+ const project = [ "**/tsconfig.json" , "!**/node_modules/**/tsconfig.json" ]
7
9
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
14
46
}
15
47
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
+ }
19
55
20
56
const pluginTypeScriptRulesExtra = {
21
57
"@typescript-eslint/no-unused-vars" : [
@@ -46,8 +82,9 @@ const pluginTypeScriptRulesExtra = {
46
82
// "@typescript-eslint/prefer-string-starts-ends-with": "error",
47
83
}
48
84
49
- const pluginTypeScriptProjectRules = projectedBasedRules
50
- ? {
85
+ const pluginTypeScriptProjectRules = disableProjectBasedRules ( )
86
+ ? { }
87
+ : {
51
88
"@typescript-eslint/no-floating-promises" : "error" ,
52
89
"@typescript-eslint/no-unnecessary-boolean-literal-compare" : "error" ,
53
90
"@typescript-eslint/no-unnecessary-condition" : "error" ,
@@ -60,11 +97,10 @@ const pluginTypeScriptProjectRules = projectedBasedRules
60
97
"@typescript-eslint/strict-boolean-expressions" : "error" ,
61
98
"@typescript-eslint/switch-exhaustiveness-check" : "warn" ,
62
99
}
63
- : { }
64
100
65
101
exports . tsConfig = {
66
102
// TypeScript files
67
- files : [ "**/*.tsx" , "**/*.ts" ] ,
103
+ files : tsFiles ,
68
104
parser : "@typescript-eslint/parser" ,
69
105
parserOptions : {
70
106
project,
@@ -80,7 +116,7 @@ exports.tsConfig = {
80
116
"prettier" ,
81
117
] ,
82
118
rules : {
83
- ...typeScriptEslintExtra ,
119
+ ...javaScriptRules ( ) ,
84
120
...pluginTypeScriptRulesExtra ,
85
121
...pluginTypeScriptProjectRules ,
86
122
...pluginNodeRules ,
0 commit comments