Skip to content

Commit

Permalink
fix: lint dirty modules only (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardogobbosouza authored Jan 25, 2021
1 parent f819203 commit f7f372e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
13 changes: 11 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,16 @@ export class ESLintWebpackPlugin {
// From my testing of compiler.watch() ... compiler.watching is always
// undefined (webpack 4 doesn't define it either) I'm leaving it out
// for now.
compiler.hooks.watchRun.tapPromise(ESLINT_PLUGIN, this.run);
let isFirstRun = this.options.lintDirtyModulesOnly;
compiler.hooks.watchRun.tapPromise(ESLINT_PLUGIN, (c) => {
if (isFirstRun) {
isFirstRun = false;

return Promise.resolve();
}

return this.run(c);
});
}

/**
Expand Down Expand Up @@ -81,7 +90,7 @@ export class ESLintWebpackPlugin {
// @ts-ignore
const processModule = (module) => {
if (module.resource) {
const file = module.resource.split('?')[0];
const [file] = module.resource.split('?');

if (
file &&
Expand Down
41 changes: 38 additions & 3 deletions test/lint-dirty-modules-only.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,49 @@
import { join } from 'path';
import { writeFileSync } from 'fs';

import { removeSync } from 'fs-extra';

import pack from './utils/pack';

const target = join(__dirname, 'fixtures', 'lint-dirty-modules-only-entry.js');

describe('lint dirty modules only', () => {
let watch;
afterEach(() => {
if (watch) {
watch.close();
}
removeSync(target);
});

it('skips linting on initial run', (done) => {
const compiler = pack('error', { lintDirtyModulesOnly: true });
writeFileSync(target, 'const foo = false\n');

let next = firstPass;
const compiler = pack('lint-dirty-modules-only', {
lintDirtyModulesOnly: true,
});
watch = compiler.watch({}, (err, stats) => next(err, stats));

compiler.run((err, stats) => {
function firstPass(err, stats) {
expect(err).toBeNull();
expect(stats.hasWarnings()).toBe(false);
expect(stats.hasErrors()).toBe(false);

next = secondPass;

writeFileSync(target, 'const bar = false;\n');
}

function secondPass(err, stats) {
expect(err).toBeNull();
expect(stats.hasWarnings()).toBe(false);
expect(stats.hasErrors()).toBe(true);
const { errors } = stats.compilation;
expect(errors.length).toBe(1);
const [{ message }] = errors;
expect(message).toEqual(expect.stringMatching('no-unused-vars'));
done();
});
}
});
});

0 comments on commit f7f372e

Please sign in to comment.