Skip to content

Commit 1dfc05a

Browse files
fix: make ESLint work without a .gitignore file (#27)
1 parent 5f075f7 commit 1dfc05a

File tree

4 files changed

+53
-9
lines changed

4 files changed

+53
-9
lines changed

src/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,10 @@ export interface Options extends Linter.Config {
9797
*/
9898
ignores?: string[];
9999
/**
100-
* .gitignore file path relative to ESLint configuration file.
100+
* .gitignore file path relative to ESLint configuration file. Set to `false` to disable.
101101
* @default './.gitignore'
102102
*/
103-
gitignorePath?: string;
103+
gitignore?: string | false;
104104
}
105105

106106
export declare function defineConfig(initOptions?: Options, ...extend: Linter.Config[]): Linter.Config[];

src/index.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const defaultOptions = {
3333
disableExpensiveRules: false,
3434
esm: false,
3535
ignores: [],
36-
gitignorePath: './.gitignore',
36+
gitignore: './.gitignore',
3737
import: {},
3838
jest: isPackageExists('jest'),
3939
next: isPackageExists('next'),
@@ -72,7 +72,7 @@ export function defineConfig(initOptions = {}, ...extend) {
7272
disableExpensiveRules,
7373
esm: enableEsm,
7474
ignores: enableIgnores,
75-
gitignorePath,
75+
gitignore,
7676
import: enableImport,
7777
jest: enableJest,
7878
next: enableNext,

src/modules/ignore.mjs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
1-
import { includeIgnoreFile } from '@eslint/compat';
21
import { globalIgnores } from 'eslint/config';
3-
import path from 'node:path';
42

3+
import { getGitignorePatterns } from '../utils/getGitignorePatterns.mjs';
54
import { ignoreGlobs } from '../utils/globs.mjs';
65

76
/**
87
* @param { import('..').Options } options
98
* @return { import('eslint').Linter.Config }
109
*/
1110
function ignores(options = {}) {
12-
const gitignorePath = path.resolve(process.cwd(), options.gitignorePath);
13-
const gitignore = includeIgnoreFile(gitignorePath);
14-
return globalIgnores([...gitignore.ignores, ...ignoreGlobs, ...(options.ignores ?? [])], 'ignores');
11+
const ignorePatterns = [...ignoreGlobs, ...(options.ignores ?? [])];
12+
13+
const shouldUseGitignore = options.gitignore && options.gitignore.length > 0;
14+
if (shouldUseGitignore) {
15+
const gitignorePatterns = getGitignorePatterns(options.gitignore);
16+
if (gitignorePatterns.length > 0) {
17+
ignorePatterns.push(...gitignorePatterns);
18+
}
19+
}
20+
21+
const uniqueIgnorePatterns = [...new Set(ignorePatterns)];
22+
23+
return globalIgnores(uniqueIgnorePatterns, 'ignores');
1524
}
1625

1726
export default ignores;

src/utils/getGitignorePatterns.mjs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/* eslint 'no-console': ['warn', { allow: ['warn'] }] */
2+
3+
import { includeIgnoreFile } from '@eslint/compat';
4+
import fs from 'node:fs';
5+
import path from 'node:path';
6+
7+
const warningMessage = '\u001B[38;2;229;229;14mWarning:\u001B[0m';
8+
const fallbackMessage =
9+
'Falling back to default ignore patterns. You can suppress this warning by setting \u001B[48;5;234m`gitignore: false`\u001B[0m in the config.';
10+
11+
/**
12+
* @param {string} gitignorePath relative path to the .gitignore file
13+
* @returns {string[]}
14+
*/
15+
export function getGitignorePatterns(gitignorePath) {
16+
const gitignoreAbsolutePath = path.resolve(process.cwd(), gitignorePath);
17+
const gitignoreExists = fs.existsSync(gitignoreAbsolutePath); // eslint-disable-line n/no-sync
18+
const gitignorePatterns = [];
19+
20+
if (!gitignoreExists) {
21+
console.warn(warningMessage, `No .gitignore file found at "${gitignoreAbsolutePath}".`, fallbackMessage);
22+
23+
return gitignorePatterns;
24+
}
25+
26+
const ignorePatterns = includeIgnoreFile(gitignoreAbsolutePath).ignores ?? [];
27+
28+
if (ignorePatterns.length > 0) {
29+
gitignorePatterns.push(...ignorePatterns);
30+
} else {
31+
console.warn(warningMessage, `No patterns found in .gitignore file at "${gitignoreAbsolutePath}".`, fallbackMessage);
32+
}
33+
34+
return gitignorePatterns;
35+
}

0 commit comments

Comments
 (0)