Skip to content

Commit 3e7c36e

Browse files
fix: hooks and callback error
1 parent b7f3679 commit 3e7c36e

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

src/linter.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export default function linter(options, compiler, callback) {
3434

3535
let { errors, warnings } = parseResults(options, results);
3636

37-
compiler.hooks.afterCompile.tapAsync(
37+
compiler.hooks.afterEmit.tapAsync(
3838
'ESLintWebpackPlugin',
3939
(compilation, next) => {
4040
if (warnings.length > 0) {
@@ -85,7 +85,15 @@ export default function linter(options, compiler, callback) {
8585
callback();
8686
}
8787
} catch (e) {
88-
callback(e);
88+
compiler.hooks.afterEmit.tapAsync(
89+
'ESLintWebpackPlugin',
90+
(compilation, next) => {
91+
compilation.errors.push(new ESLintError(e.message));
92+
next();
93+
}
94+
);
95+
96+
callback();
8997
}
9098
}
9199

test/empty.test.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,16 @@ describe('empty', () => {
99
const compiler = webpack({
1010
context: join(__dirname, 'fixtures', 'empty'),
1111
mode: 'development',
12+
entry: '../index',
1213
plugins: [new ESLintPlugin()],
1314
});
1415

15-
compiler.run((err) => {
16-
expect(err.message).toMatch(/No files matching/i);
16+
compiler.run((err, stats) => {
17+
const { errors } = stats.compilation;
18+
expect(stats.hasWarnings()).toBe(false);
19+
expect(stats.hasErrors()).toBe(true);
20+
expect(errors).toHaveLength(1);
21+
expect(errors[0].message).toMatch(/No files matching/i);
1722
done();
1823
});
1924
});

test/fail-on-config.test.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@ describe('fail on config', () => {
77
const configFile = join(__dirname, '.badeslintrc');
88
const compiler = pack('error', { configFile });
99

10-
compiler.run((err) => {
11-
expect(err.message).toMatch(
10+
compiler.run((err, stats) => {
11+
const { errors } = stats.compilation;
12+
expect(stats.hasWarnings()).toBe(false);
13+
expect(stats.hasErrors()).toBe(true);
14+
expect(errors).toHaveLength(1);
15+
expect(errors[0].message).toMatch(
1216
/ESLint configuration in --config is invalid/i
1317
);
1418
done();

0 commit comments

Comments
 (0)