Skip to content

Commit f6bf4c0

Browse files
kevinastonesindresorhus
authored andcommitted
Respect .eslintignore (#377)
1 parent 05c30e2 commit f6bf4c0

File tree

7 files changed

+44
-10
lines changed

7 files changed

+44
-10
lines changed

index.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ const processReport = (report, options) => {
3434
const runEslint = (paths, options) => {
3535
const config = optionsManager.buildConfig(options);
3636
const engine = new eslint.CLIEngine(config);
37-
const report = engine.executeOnFiles(paths, config);
37+
const report = engine.executeOnFiles(
38+
paths.filter(path => !engine.isPathIgnored(path)),
39+
config,
40+
);
3841
return processReport(report, options);
3942
};
4043

@@ -58,12 +61,15 @@ module.exports.lintText = (string, options) => {
5861
throw new Error('The `ignores` option requires the `filename` option to be defined.');
5962
}
6063

64+
const engine = new eslint.CLIEngine(options);
65+
6166
if (options.filename) {
6267
const filename = path.relative(options.cwd, options.filename);
6368

6469
if (
6570
multimatch(filename, options.ignores).length > 0 ||
66-
globby.gitignore.sync({cwd: options.cwd, ignore: options.ignores})(options.filename)
71+
globby.gitignore.sync({cwd: options.cwd, ignore: options.ignores})(options.filename) ||
72+
engine.isPathIgnored(options.filename)
6773
) {
6874
return {
6975
errorCount: 0,
@@ -78,7 +84,6 @@ module.exports.lintText = (string, options) => {
7884
}
7985
}
8086

81-
const engine = new eslint.CLIEngine(options);
8287
const report = engine.executeOnText(string, options.filename);
8388

8489
return processReport(report, options);

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ Additional global variables your code accesses during execution.
177177

178178
Type: `string[]`
179179

180-
Some [paths](lib/options-manager.js) are ignored by default, including paths in `.gitignore`. Additional ignores can be added here.
180+
Some [paths](lib/options-manager.js) are ignored by default, including paths in `.gitignore` and [.eslintignore](https://eslint.org/docs/user-guide/configuring#eslintignore). Additional ignores can be added here.
181181

182182
### space
183183

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bar.js

test/fixtures/eslintignore/bar.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('no semicolon')

test/fixtures/eslintignore/foo.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('no semicolon')

test/lint-files.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,14 @@ test('enable rules based on nodeVersion', async t => {
120120
)
121121
);
122122
});
123+
124+
test('do not lint eslintignored files', async t => {
125+
const cwd = path.join(__dirname, 'fixtures/eslintignore/');
126+
const glob = path.posix.join(cwd, '*');
127+
const positive = path.resolve('fixtures/eslintignore/foo.js');
128+
const negative = path.resolve('fixtures/eslintignore/bar.js');
129+
const {results} = await fn.lintFiles(glob, {cwd});
130+
131+
t.is(results.some(r => r.filePath === positive), true);
132+
t.is(results.some(r => r.filePath === negative), false);
133+
});

test/lint-text.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,30 +161,30 @@ test('lintText() - overrides support', async t => {
161161
test('do not lint gitignored files if filename is given', async t => {
162162
const cwd = path.join(__dirname, 'fixtures/gitignore');
163163
const ignoredPath = path.resolve('fixtures/gitignore/test/foo.js');
164-
const ignored = await readFile(ignoredPath, 'utf-8');
164+
const ignored = await readFile(ignoredPath, 'utf8');
165165
const {results} = fn.lintText(ignored, {filename: ignoredPath, cwd});
166166
t.is(results[0].errorCount, 0);
167167
});
168168

169169
test('lint gitignored files if filename is not given', async t => {
170170
const ignoredPath = path.resolve('fixtures/gitignore/test/foo.js');
171-
const ignored = await readFile(ignoredPath, 'utf-8');
171+
const ignored = await readFile(ignoredPath, 'utf8');
172172
const {results} = fn.lintText(ignored);
173173
t.true(results[0].errorCount > 0);
174174
});
175175

176176
test('do not lint gitignored files in file with negative gitignores', async t => {
177177
const cwd = path.join(__dirname, 'fixtures/negative-gitignore');
178178
const ignoredPath = path.resolve('fixtures/negative-gitignore/bar.js');
179-
const ignored = await readFile(ignoredPath, 'utf-8');
179+
const ignored = await readFile(ignoredPath, 'utf8');
180180
const {results} = fn.lintText(ignored, {filename: ignoredPath, cwd});
181181
t.is(results[0].errorCount, 0);
182182
});
183183

184184
test('multiple negative patterns should act as positive patterns', async t => {
185185
const cwd = path.join(__dirname, 'fixtures', 'gitignore-multiple-negation');
186186
const filename = path.join(cwd, '!!!unicorn.js');
187-
const text = await readFile(filename, 'utf-8');
187+
const text = await readFile(filename, 'utf8');
188188
const {results} = fn.lintText(text, {filename, cwd});
189189
t.is(results[0].errorCount, 0);
190190
});
@@ -197,10 +197,25 @@ test('lint negatively gitignored files', async t => {
197197
t.true(results[0].errorCount > 0);
198198
});
199199

200+
test('do not lint eslintignored files if filename is given', async t => {
201+
const cwd = path.join(__dirname, 'fixtures/eslintignore');
202+
const ignoredPath = path.resolve('fixtures/eslintignore/bar.js');
203+
const ignored = await readFile(ignoredPath, 'utf8');
204+
const {results} = fn.lintText(ignored, {filename: ignoredPath, cwd});
205+
t.is(results[0].errorCount, 0);
206+
});
207+
208+
test('lint eslintignored files if filename is not given', async t => {
209+
const ignoredPath = path.resolve('fixtures/eslintignore/bar.js');
210+
const ignored = await readFile(ignoredPath, 'utf8');
211+
const {results} = fn.lintText(ignored);
212+
t.true(results[0].errorCount > 0);
213+
});
214+
200215
test('enable rules based on nodeVersion', async t => {
201216
const cwd = path.join(__dirname, 'fixtures', 'engines-overrides');
202217
const filename = path.join(cwd, 'promise-then.js');
203-
const text = await readFile(filename, 'utf-8');
218+
const text = await readFile(filename, 'utf8');
204219

205220
let {results} = fn.lintText(text, {nodeVersion: '>=8.0.0'});
206221
t.true(hasRule(results, 'promise/prefer-await-to-then'));
@@ -212,7 +227,7 @@ test('enable rules based on nodeVersion', async t => {
212227
test('enable rules based on nodeVersion in override', async t => {
213228
const cwd = path.join(__dirname, 'fixtures', 'engines-overrides');
214229
const filename = path.join(cwd, 'promise-then.js');
215-
const text = await readFile(filename, 'utf-8');
230+
const text = await readFile(filename, 'utf8');
216231

217232
let {results} = fn.lintText(text, {
218233
nodeVersion: '>=8.0.0',

0 commit comments

Comments
 (0)