Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: lint modules that are cached with webpack's filesystem cache #197

Merged
merged 4 commits into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const linter = require('./linter');
const { arrify, parseFiles, parseFoldersToGlobs } = require('./utils');

/** @typedef {import('webpack').Compiler} Compiler */
/** @typedef {import('webpack').Module} Module */
/** @typedef {import('webpack').NormalModule} NormalModule */
/** @typedef {import('./options').Options} Options */

const ESLINT_PLUGIN = 'ESLintWebpackPlugin';
Expand Down Expand Up @@ -103,9 +105,16 @@ class ESLintWebpackPlugin {
/** @type {string[]} */
const files = [];

// @ts-ignore
// Add the file to be linted
compilation.hooks.succeedModule.tap(this.key, ({ resource }) => {
compilation.hooks.succeedModule.tap(this.key, addFile);
compilation.hooks.stillValidModule.tap(this.key, addFile);

/**
* @param {Module} module
*/
function addFile(module) {
const { resource } = /** @type {NormalModule} */ (module);

if (resource) {
const [file, query] = resource.split('?');

Expand All @@ -123,7 +132,7 @@ class ESLintWebpackPlugin {
}
}
}
});
}

// Lint all files added
compilation.hooks.finishModules.tap(this.key, () => {
Expand Down
49 changes: 49 additions & 0 deletions test/cached.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { join } from 'path';

import { removeSync } from 'fs-extra';

import webpack from 'webpack';

import conf from './utils/conf';

describe('error (cached module)', () => {
const cacheLocation = join(__dirname, 'cache');

beforeEach(() => {
removeSync(cacheLocation);
});

afterAll(() => {
removeSync(cacheLocation);
});

it('should return error even if module is cached', (done) => {
const config = conf('error');
config.cache = {
type: 'filesystem',
idleTimeout: 0,
idleTimeoutAfterLargeChanges: 0,
idleTimeoutForInitialStore: 0,
cacheLocation,
};

const c1 = webpack(config);

c1.run((err1, stats1) => {
expect(err1).toBeNull();
expect(stats1.hasWarnings()).toBe(false);
expect(stats1.hasErrors()).toBe(true);

c1.close(() => {
const c2 = webpack(config);
c2.run((err2, stats2) => {
expect(err2).toBeNull();
expect(stats2.hasWarnings()).toBe(false);
expect(stats2.hasErrors()).toBe(true);

done();
});
});
});
});
});
4 changes: 3 additions & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ declare class ESLintWebpackPlugin {
getContext(compiler: Compiler): string;
}
declare namespace ESLintWebpackPlugin {
export { Compiler, Options };
export { Compiler, Module, NormalModule, Options };
}
type Compiler = import('webpack').Compiler;
type Options = import('./options').Options;
type Module = import('webpack').Module;
type NormalModule = import('webpack').NormalModule;