Skip to content

Commit 17f7421

Browse files
fix: use hook afterEmit and emit error on catch
1 parent e16d03c commit 17f7421

4 files changed

Lines changed: 39 additions & 21 deletions

File tree

src/linter.js

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,23 @@ export default function linter(options, compiler, callback) {
1111
.then(({ results }) => {
1212
({ errors, warnings } = parseResults(options, results));
1313

14+
compiler.hooks.afterEmit.tapAsync(
15+
'StylelintWebpackPlugin',
16+
(compilation, next) => {
17+
if (warnings.length) {
18+
compilation.warnings.push(StylelintError.format(options, warnings));
19+
warnings = [];
20+
}
21+
22+
if (errors.length) {
23+
compilation.errors.push(StylelintError.format(options, errors));
24+
errors = [];
25+
}
26+
27+
next();
28+
}
29+
);
30+
1431
if (options.failOnError && errors.length) {
1532
callback(StylelintError.format(options, errors));
1633
} else if (options.failOnWarning && warnings.length) {
@@ -19,24 +36,17 @@ export default function linter(options, compiler, callback) {
1936
callback();
2037
}
2138
})
22-
.catch(callback);
23-
24-
compiler.hooks.afterCompile.tapAsync(
25-
'StylelintWebpackPlugin',
26-
(compilation, next) => {
27-
if (warnings.length) {
28-
compilation.warnings.push(StylelintError.format(options, warnings));
29-
warnings = [];
30-
}
31-
32-
if (errors.length) {
33-
compilation.errors.push(StylelintError.format(options, errors));
34-
errors = [];
35-
}
39+
.catch((e) => {
40+
compiler.hooks.afterEmit.tapAsync(
41+
'StylelintWebpackPlugin',
42+
(compilation, next) => {
43+
compilation.errors.push(new StylelintError(e.message));
44+
next();
45+
}
46+
);
3647

37-
next();
38-
}
39-
);
48+
callback();
49+
});
4050
}
4151

4252
function parseResults(options, results) {

test/empty.test.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@ describe('empty', () => {
1616
plugins: [new StylelintPlugin()],
1717
});
1818

19-
compiler.run((err) => {
20-
expect(err.message).toContain('No files matching the pattern');
19+
compiler.run((err, stats) => {
20+
const { errors } = stats.compilation;
21+
expect(stats.hasWarnings()).toBe(false);
22+
expect(stats.hasErrors()).toBe(true);
23+
expect(errors).toHaveLength(1);
24+
expect(errors[0].message).toContain('No files matching the pattern');
2125
done();
2226
});
2327
});

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, '.badstylelintrc');
88
const compiler = pack('error', { configFile });
99

10-
compiler.run((err) => {
11-
expect(err.message).toMatch(/duplicated mapping key|Failed to parse/);
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(/Map keys must be unique/);
1216
done();
1317
});
1418
});

test/fixtures/empty/index.js

Whitespace-only changes.

0 commit comments

Comments
 (0)