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: fails when failOnError or failOnWarning enabled #72

Merged
merged 3 commits into from
Feb 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,9 @@ The warnings found will always be emitted, to disable set to `false`.
#### `failOnError`

- Type: `Boolean`
- Default: `false`
- Default: `true`

Will cause the module build to fail if there are any errors, if set to `true`.
Will cause the module build to fail if there are any errors, to disable set to `false`.

#### `failOnWarning`

Expand Down
7 changes: 3 additions & 4 deletions src/ESLintError.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
// @ts-ignore
import WebpackError from 'webpack/lib/WebpackError';

export default class ESLintError extends WebpackError {
class ESLintError extends Error {
/**
* @param {string=} messages
*/
Expand All @@ -11,3 +8,5 @@ export default class ESLintError extends WebpackError {
this.stack = '';
}
}

export default ESLintError;
13 changes: 10 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,25 @@ class ESLintWebpackPlugin {

// Gather Files to lint
compilation.hooks.succeedModule.tap(ESLINT_PLUGIN, processModule);

// await and interpret results
compilation.hooks.afterSeal.tapPromise(ESLINT_PLUGIN, processResults);
compilation.hooks.additionalAssets.tapPromise(
ESLINT_PLUGIN,
processResults
);

async function processResults() {
const { errors, warnings, generateReportAsset } = await report();

if (warnings) {
if (warnings && !options.failOnWarning) {
// @ts-ignore
compilation.warnings.push(warnings);
} else if (warnings && options.failOnWarning) {
// @ts-ignore
compilation.errors.push(warnings);
}

if (errors) {
if (errors && options.failOnError) {
// @ts-ignore
compilation.errors.push(errors);
}
Expand Down
6 changes: 0 additions & 6 deletions src/linter.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,6 @@ export default function linter(key, options, compilation) {
parseResults(options, results)
);

if (options.failOnError && errors) {
throw errors;
} else if (options.failOnWarning && warnings) {
throw warnings;
}

return {
errors,
warnings,
Expand Down
1 change: 1 addition & 0 deletions src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export function getOptions(pluginOptions) {
extensions: 'js',
emitError: true,
emitWarning: true,
ricardogobbosouza marked this conversation as resolved.
Show resolved Hide resolved
failOnError: true,
...pluginOptions,
...(pluginOptions.quiet ? { emitError: true, emitWarning: false } : {}),
};
Expand Down
2 changes: 1 addition & 1 deletion src/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"anyOf": [{ "type": "string" }, { "type": "array" }]
},
"failOnError": {
"description": "Will cause the module build to fail if there are any errors, if set to `true`.",
"description": "Will cause the module build to fail if there are any errors, to disable set to `false`.",
"type": "boolean"
},
"failOnWarning": {
Expand Down
5 changes: 3 additions & 2 deletions test/fail-on-error.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ describe('fail on error', () => {
it('should emits errors', (done) => {
const compiler = pack('error', { failOnError: true });

compiler.run((err) => {
expect(err.message).toContain('error.js');
compiler.run((err, stats) => {
expect(err).toBeNull();
expect(stats.hasErrors()).toBe(true);
done();
});
});
Expand Down
5 changes: 3 additions & 2 deletions test/fail-on-warning.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ describe('fail on warning', () => {
it('should emits errors', (done) => {
const compiler = pack('warn', { failOnWarning: true });

compiler.run((err) => {
expect(err.message).toContain('warn.js');
compiler.run((err, stats) => {
expect(err).toBeNull();
expect(stats.hasErrors()).toBe(true);
done();
});
});
Expand Down