Skip to content
This repository was archived by the owner on Jul 27, 2021. It is now read-only.

Commit af3194a

Browse files
authored
Improve eslint messages (#20)
1 parent 46b2560 commit af3194a

File tree

6 files changed

+65
-24
lines changed

6 files changed

+65
-24
lines changed

src/formatters/eslintError.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
3+
const concat = require('../utils').concat;
4+
const chalk = require('chalk');
5+
6+
const infos = [
7+
'You may use special comments to disable some warnings.',
8+
'Use ' + chalk.yellow('// eslint-disable-next-line') + ' to ignore the next line.',
9+
'Use ' + chalk.yellow('/* eslint-disable */') + ' to ignore all warnings in a file.'
10+
];
11+
12+
function displayError(error) {
13+
return [error.message, '']
14+
}
15+
16+
function format(errors, type) {
17+
const lintErrors = errors.filter(e => e.type === 'lint-error');
18+
if (lintErrors.length > 0) {
19+
const flatten = (accum, curr) => accum.concat(curr);
20+
return concat(
21+
lintErrors
22+
.map(error => displayError(error))
23+
.reduce(flatten, []),
24+
infos
25+
)
26+
}
27+
28+
return [];
29+
}
30+
31+
module.exports = format;

src/friendly-errors-plugin.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const defaultTransformers = [
1919

2020
const defaultFormatters = [
2121
require('./formatters/moduleNotFound'),
22+
require('./formatters/eslintError'),
2223
require('./formatters/defaultError'),
2324
];
2425

src/transformers/esLintError.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,8 @@ function isEslintError (e) {
1010
function transform(error) {
1111
if (isEslintError(error)) {
1212
return Object.assign({}, error, {
13-
infos: [
14-
'You may use special comments to disable some warnings.',
15-
'Use ' + chalk.yellow('// eslint-disable-next-line') + ' to ignore the next line.',
16-
'Use ' + chalk.yellow('/* eslint-disable */') + ' to ignore all warnings in a file.'
17-
],
1813
name: 'Lint error',
14+
type: 'lint-error',
1915
});
2016
}
2117

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1+
require('./module');
2+
13
const unused = 'I am unused';
4+
const unused2 = 'I am unused too';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const unused = 'I am unused';

test/integration.spec.js

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ const output = require('../src/output');
44
const webpack = require('webpack');
55
const FriendlyErrorsWebpackPlugin = require('../src/friendly-errors-plugin');
66
const MemoryFileSystem = require('memory-fs');
7+
const path = require('path');
78

8-
const webpackPromise = function(config, globalPlugins) {
9+
const webpackPromise = function (config, globalPlugins) {
910
const compiler = webpack(config);
1011
compiler.outputFileSystem = new MemoryFileSystem();
1112
if (Array.isArray(globalPlugins)) {
@@ -32,14 +33,14 @@ async function executeAndGetLogs(fixture, globalPlugins) {
3233
}
3334
}
3435

35-
it('integration : success', async () => {
36+
it('integration : success', async() => {
3637

3738
const logs = await executeAndGetLogs('./fixtures/success/webpack.config')
3839

3940
expect(logs.join('\n')).toMatch(/DONE Compiled successfully in (.\d*)ms/);
4041
});
4142

42-
it('integration : module-errors', async () => {
43+
it('integration : module-errors', async() => {
4344

4445
const logs = await executeAndGetLogs('./fixtures/module-errors/webpack.config.js');
4546

@@ -55,27 +56,35 @@ it('integration : module-errors', async () => {
5556
]);
5657
});
5758

58-
it('integration : should display eslint warnings', async () => {
59+
function filename(filePath) {
60+
return path.join(__dirname, path.normalize(filePath))
61+
}
62+
63+
it.only('integration : should display eslint warnings', async() => {
5964

6065
const logs = await executeAndGetLogs('./fixtures/eslint-warnings/webpack.config.js');
6166

62-
expect(logs).toEqual([
63-
'WARNING Compiled with 1 warnings',
64-
'',
65-
'warning in ./test/fixtures/eslint-warnings/index.js',
66-
'',
67-
`${__dirname}/fixtures/eslint-warnings/index.js
67+
expect(logs.join('\n')).toEqual(
68+
`WARNING Compiled with 2 warnings
69+
70+
${filename('fixtures/eslint-warnings/index.js')}
71+
3:7 warning 'unused' is assigned a value but never used no-unused-vars
72+
4:7 warning 'unused2' is assigned a value but never used no-unused-vars
73+
74+
✖ 2 problems (0 errors, 2 warnings)
75+
76+
${filename('fixtures/eslint-warnings/module.js')}
6877
1:7 warning 'unused' is assigned a value but never used no-unused-vars
6978
70-
✖ 1 problem (0 errors, 1 warning)`,
71-
'',
72-
'You may use special comments to disable some warnings.',
73-
'Use // eslint-disable-next-line to ignore the next line.',
74-
'Use /* eslint-disable */ to ignore all warnings in a file.'
75-
]);
79+
✖ 1 problem (0 errors, 1 warning)
80+
81+
You may use special comments to disable some warnings.
82+
Use // eslint-disable-next-line to ignore the next line.
83+
Use /* eslint-disable */ to ignore all warnings in a file.`
84+
)
7685
});
7786

78-
it('integration : babel syntax error', async () => {
87+
it('integration : babel syntax error', async() => {
7988

8089
const logs = await executeAndGetLogs('./fixtures/babel-syntax/webpack.config');
8190

@@ -96,7 +105,7 @@ it('integration : babel syntax error', async () => {
96105
]);
97106
});
98107

99-
it('integration : webpack multi compiler : success', async () => {
108+
it('integration : webpack multi compiler : success', async() => {
100109

101110
// We apply the plugin directly to the compiler when targeting multi-compiler
102111
let globalPlugins = [new FriendlyErrorsWebpackPlugin()];
@@ -105,7 +114,7 @@ it('integration : webpack multi compiler : success', async () => {
105114
expect(logs.join('\n')).toMatch(/DONE Compiled successfully in (.\d*)ms/)
106115
});
107116

108-
it('integration : webpack multi compiler : module-errors', async () => {
117+
it('integration : webpack multi compiler : module-errors', async() => {
109118

110119
// We apply the plugin directly to the compiler when targeting multi-compiler
111120
let globalPlugins = [new FriendlyErrorsWebpackPlugin()];

0 commit comments

Comments
 (0)