Skip to content

Commit 5f82855

Browse files
authored
Merge pull request #91 from atom-community/gitignore
2 parents 3a3f16e + ea81064 commit 5f82855

File tree

4 files changed

+59
-34
lines changed

4 files changed

+59
-34
lines changed

.vscode/settings.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
{
2-
"cSpell.words": [
3-
"astrojs",
4-
"Globified",
5-
"globify"
6-
],
2+
"cSpell.words": ["astrojs", "Globified", "globify"],
73
"explorer.fileNesting.patterns": {
84
"index.js": "*.js"
95
}

src/astro.cts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export const astroConfig: Linter.ConfigOverride<Linter.RulesRecord> = {
1212
plugins: ["astro", "only-warn"],
1313
extends: ["plugin:astro/recommended"],
1414
rules: {
15-
...pluginImportAstroRulesExtra
15+
...pluginImportAstroRulesExtra,
1616
},
1717
globals: {
1818
astroHTML: "readonly",

src/typescript.cts

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,30 @@ 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

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+
}
1629
}
1730
const globifyGitIgnoreFileSync = makeSynchronous(globifyGitIgnoreFileWithDeps) as (
1831
cwd: string,
@@ -41,18 +54,10 @@ function disableProjectBasedRules() {
4154
)
4255

4356
// 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)
5358

5459
// 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
5661

5762
if (disable) {
5863
console.warn(
@@ -119,7 +124,7 @@ export const tsConfig: Linter.ConfigOverride<Linter.RulesRecord> = {
119124
files: tsFiles,
120125
parser: "@typescript-eslint/parser",
121126
parserOptions: {
122-
project,
127+
project: tscConfigFiles,
123128
createDefaultProgram: true, // otherwise Eslint will error if a ts file is not covered by one of the tsconfig.json files
124129
},
125130
plugins: ["@typescript-eslint", "node", "import", "only-warn"],

src/utils.cts

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,47 @@ 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[]) {
5+
export function findFilesForGroups(
6+
cwd: string,
7+
earlyExitSearchGroup: string[],
8+
exhaustiveSearchGroup: string[],
9+
ignored: string[],
10+
) {
11+
const status = [false, false]
12+
searchDirectory(cwd, status, earlyExitSearchGroup, exhaustiveSearchGroup, ignored)
13+
14+
return status
15+
}
16+
17+
function searchDirectory(
18+
directory: string,
19+
status: boolean[],
20+
earlyExitSearchGroup: string[],
21+
exhaustiveSearchGroup: string[],
22+
ignored: string[],
23+
): boolean {
624
// 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 })
25+
const files = readdirSync(directory, { withFileTypes: true, recursive: false })
826
for (const file of files) {
9-
const path = join(cwd, file.name)
27+
const path = join(directory, file.name)
1028
if (file.isDirectory()) {
29+
// if the folder is not ignored, search it recursively
1130
if (!anymatch(ignored, path)) {
12-
// if the folder is not ignored, search it recursively
13-
const found = findOneFile(path, search, ignored)
14-
if (found) {
15-
return true
31+
if (searchDirectory(path, status, earlyExitSearchGroup, exhaustiveSearchGroup, ignored)) {
32+
return true // exit
1633
}
1734
}
18-
} else if (anymatch(search, path)) {
19-
// if the file ends with the given fileEnding, return true
20-
return true
35+
} else {
36+
// check the early exit search group first
37+
status[0] = status[0] || anymatch(exhaustiveSearchGroup, path)
38+
if (status[0]) {
39+
return true // exit
40+
}
41+
42+
// check the exhaustive search group
43+
status[1] = status[1] || anymatch(exhaustiveSearchGroup, path)
2144
}
2245
}
23-
return false
46+
47+
return false // continue
2448
}

0 commit comments

Comments
 (0)