Skip to content

Commit f7f372e

Browse files
fix: lint dirty modules only (#67)
1 parent f819203 commit f7f372e

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

src/index.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,16 @@ export class ESLintWebpackPlugin {
3434
// From my testing of compiler.watch() ... compiler.watching is always
3535
// undefined (webpack 4 doesn't define it either) I'm leaving it out
3636
// for now.
37-
compiler.hooks.watchRun.tapPromise(ESLINT_PLUGIN, this.run);
37+
let isFirstRun = this.options.lintDirtyModulesOnly;
38+
compiler.hooks.watchRun.tapPromise(ESLINT_PLUGIN, (c) => {
39+
if (isFirstRun) {
40+
isFirstRun = false;
41+
42+
return Promise.resolve();
43+
}
44+
45+
return this.run(c);
46+
});
3847
}
3948

4049
/**
@@ -81,7 +90,7 @@ export class ESLintWebpackPlugin {
8190
// @ts-ignore
8291
const processModule = (module) => {
8392
if (module.resource) {
84-
const file = module.resource.split('?')[0];
93+
const [file] = module.resource.split('?');
8594

8695
if (
8796
file &&

test/lint-dirty-modules-only.test.js

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,49 @@
1+
import { join } from 'path';
2+
import { writeFileSync } from 'fs';
3+
4+
import { removeSync } from 'fs-extra';
5+
16
import pack from './utils/pack';
27

8+
const target = join(__dirname, 'fixtures', 'lint-dirty-modules-only-entry.js');
9+
310
describe('lint dirty modules only', () => {
11+
let watch;
12+
afterEach(() => {
13+
if (watch) {
14+
watch.close();
15+
}
16+
removeSync(target);
17+
});
18+
419
it('skips linting on initial run', (done) => {
5-
const compiler = pack('error', { lintDirtyModulesOnly: true });
20+
writeFileSync(target, 'const foo = false\n');
21+
22+
let next = firstPass;
23+
const compiler = pack('lint-dirty-modules-only', {
24+
lintDirtyModulesOnly: true,
25+
});
26+
watch = compiler.watch({}, (err, stats) => next(err, stats));
627

7-
compiler.run((err, stats) => {
28+
function firstPass(err, stats) {
829
expect(err).toBeNull();
930
expect(stats.hasWarnings()).toBe(false);
1031
expect(stats.hasErrors()).toBe(false);
32+
33+
next = secondPass;
34+
35+
writeFileSync(target, 'const bar = false;\n');
36+
}
37+
38+
function secondPass(err, stats) {
39+
expect(err).toBeNull();
40+
expect(stats.hasWarnings()).toBe(false);
41+
expect(stats.hasErrors()).toBe(true);
42+
const { errors } = stats.compilation;
43+
expect(errors.length).toBe(1);
44+
const [{ message }] = errors;
45+
expect(message).toEqual(expect.stringMatching('no-unused-vars'));
1146
done();
12-
});
47+
}
1348
});
1449
});

0 commit comments

Comments
 (0)