Skip to content

Commit fb5198d

Browse files
committed
fix: only warn on project-based rules in case there is a TypeScript file
1 parent 5cfa3cb commit fb5198d

File tree

3 files changed

+52
-20
lines changed

3 files changed

+52
-20
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
"eslint-plugin-optimize-regex": "^1.2.1",
3838
"eslint-plugin-react": "^7.32.2",
3939
"eslint-plugin-yaml": "^0.5.0",
40-
"fast-glob": "^3.2.12",
4140
"prettier": "2.8.8",
4241
"read-pkg-up": "^7.0.1",
4342
"semver": "^7.5.1",

pnpm-lock.yaml

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/typescript.js

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,57 @@
11
const { eslintRulesExtra } = require("./official-eslint-rules")
22
const { pluginImportRulesExtra, pluginImportTypeScriptRulesExtra } = require("./plugin-import-rules")
33
const { 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

2056
const 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

65101
exports.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

Comments
 (0)