Skip to content

Commit

Permalink
fix: use hook afterEmit and emit error on catch
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardogobbosouza committed Dec 1, 2019
1 parent e16d03c commit 17f7421
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 21 deletions.
44 changes: 27 additions & 17 deletions src/linter.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,23 @@ export default function linter(options, compiler, callback) {
.then(({ results }) => {
({ errors, warnings } = parseResults(options, results));

compiler.hooks.afterEmit.tapAsync(
'StylelintWebpackPlugin',
(compilation, next) => {
if (warnings.length) {
compilation.warnings.push(StylelintError.format(options, warnings));
warnings = [];
}

if (errors.length) {
compilation.errors.push(StylelintError.format(options, errors));
errors = [];
}

next();
}
);

if (options.failOnError && errors.length) {
callback(StylelintError.format(options, errors));
} else if (options.failOnWarning && warnings.length) {
Expand All @@ -19,24 +36,17 @@ export default function linter(options, compiler, callback) {
callback();
}
})
.catch(callback);

compiler.hooks.afterCompile.tapAsync(
'StylelintWebpackPlugin',
(compilation, next) => {
if (warnings.length) {
compilation.warnings.push(StylelintError.format(options, warnings));
warnings = [];
}

if (errors.length) {
compilation.errors.push(StylelintError.format(options, errors));
errors = [];
}
.catch((e) => {
compiler.hooks.afterEmit.tapAsync(
'StylelintWebpackPlugin',
(compilation, next) => {
compilation.errors.push(new StylelintError(e.message));
next();
}
);

next();
}
);
callback();
});
}

function parseResults(options, results) {
Expand Down
8 changes: 6 additions & 2 deletions test/empty.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ describe('empty', () => {
plugins: [new StylelintPlugin()],
});

compiler.run((err) => {
expect(err.message).toContain('No files matching the pattern');
compiler.run((err, stats) => {
const { errors } = stats.compilation;
expect(stats.hasWarnings()).toBe(false);
expect(stats.hasErrors()).toBe(true);
expect(errors).toHaveLength(1);
expect(errors[0].message).toContain('No files matching the pattern');
done();
});
});
Expand Down
8 changes: 6 additions & 2 deletions test/fail-on-config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ describe('fail on config', () => {
const configFile = join(__dirname, '.badstylelintrc');
const compiler = pack('error', { configFile });

compiler.run((err) => {
expect(err.message).toMatch(/duplicated mapping key|Failed to parse/);
compiler.run((err, stats) => {
const { errors } = stats.compilation;
expect(stats.hasWarnings()).toBe(false);
expect(stats.hasErrors()).toBe(true);
expect(errors).toHaveLength(1);
expect(errors[0].message).toMatch(/Map keys must be unique/);
done();
});
});
Expand Down
Empty file added test/fixtures/empty/index.js
Empty file.

0 comments on commit 17f7421

Please sign in to comment.