Skip to content

Commit f8c7141

Browse files
committed
Add detection for eslint errors and display friendly advice
1 parent 5866e2b commit f8c7141

File tree

10 files changed

+83
-21
lines changed

10 files changed

+83
-21
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "friendly-errors-webpack-plugin",
3-
"version": "0",
3+
"version": "0.0.1",
44
"description": "",
55
"main": "index.js",
66
"scripts": {
@@ -11,7 +11,7 @@
1111
"react",
1212
"cli"
1313
],
14-
"author": "Geoffroy Warin",
14+
"author": "Geoffroy Warin",@
1515
"repository": {
1616
"type": "git",
1717
"url": "git+https://github.com/geowarin/friendly-errors-webpack-plugin.git"

src/core/extractWebpackError.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

3+
const ErrorStackParser = require('error-stack-parser');
34
const RequestShortener = require("webpack/lib/RequestShortener");
45

56
// TODO: allow the location to be customized in options
@@ -18,9 +19,17 @@ function extractError (e) {
1819
name: e.name,
1920
severity: 0,
2021
webpackError: e,
22+
originalStack: getOriginalErrorStack(e)
2123
};
2224
}
2325

26+
function getOriginalErrorStack(e) {
27+
while (e.error != null) {
28+
e = e.error;
29+
}
30+
return ErrorStackParser.parse(e);
31+
}
32+
2433
function getFile (e) {
2534
if (e.file) {
2635
return e.file;

src/formatters/defaultError.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
'use strict';
22

33
const chalk = require('chalk');
4+
const concat = require('../utils').concat;
45

56
function displayError(index, severity, error) {
67
const baseError = chalk.red(`${index + 1}) ${severity}`);
78

8-
return [
9+
return concat(
910
`${baseError} ${error.file ? 'in ' + error.file : ''}`,
1011
'',
1112
error.message,
1213
(error.origin ? error.origin : undefined),
13-
''
14-
].filter(chunk => chunk !== undefined);
14+
'',
15+
error.infos
16+
);
1517
}
1618

1719
function isDefaultError(error) {

src/friendly-errors-plugin.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ const os = require('os');
66
const transformErrors = require('./core/transformErrors');
77
const formatErrors = require('./core/formatErrors');
88
const output = require('./output');
9+
const concat = require('./utils').concat;
910

1011
const defaultTransformers = [
1112
require('./transformers/babelSyntax'),
1213
require('./transformers/moduleNotFound'),
14+
require('./transformers/esLintError'),
1315
];
1416

1517
const defaultFormatters = [
@@ -99,15 +101,6 @@ function getMaxInt(collection, propertyName) {
99101
}, 0)
100102
}
101103

102-
/**
103-
* Concat and flattens non-null values. First arg must be an array.
104-
* Ex: concat([1], undefined, 2, [3, 4]) = [1, 2, 3, 4]
105-
*/
106-
function concat() {
107-
var args = Array.from(arguments).filter(e => e);
108-
return Array.prototype.concat.apply(args[0], args.slice(1));
109-
}
110-
111104
module.exports = FriendlyErrorsWebpackPlugin;
112105

113106
function displayCompilationMessage (message, color) {

src/transformers/esLintError.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
3+
const chalk = require('chalk');
4+
5+
function isEslintError (e) {
6+
return e.originalStack
7+
.some(stackframe => stackframe.fileName.indexOf('eslint-loader') > 0);
8+
}
9+
10+
function transform(error) {
11+
if (isEslintError(error)) {
12+
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+
]
18+
});
19+
}
20+
21+
return error;
22+
}
23+
24+
module.exports = transform;

src/transformers/moduleNotFound.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ const TYPE = 'module-not-found';
44

55
function isModuleNotFoundError (e) {
66
const webpackError = e.webpackError || {};
7-
return webpackError.dependencies && webpackError.dependencies.length &&
8-
(e.type === TYPE ||
9-
e.name === 'ModuleNotFoundError' &&
10-
e.message.indexOf('Module not found') === 0);
7+
return webpackError.dependencies
8+
&& webpackError.dependencies.length > 0
9+
&& e.name === 'ModuleNotFoundError'
10+
&& e.message.indexOf('Module not found') === 0;
1111
}
1212

1313
function transform(error) {
@@ -18,7 +18,7 @@ function transform(error) {
1818
message: `Module not found ${module}`,
1919
type: TYPE,
2020
severity: 900,
21-
module,
21+
module
2222
});
2323
}
2424

src/utils/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Concat and flattens non-null values.
3+
* Ex: concat(1, undefined, 2, [3, 4]) = [1, 2, 3, 4]
4+
*/
5+
function concat() {
6+
const args = Array.from(arguments).filter(e => e != null);
7+
const baseArray = Array.isArray(args[0]) ? args[0] : [args[0]];
8+
return Array.prototype.concat.apply(baseArray, args.slice(1));
9+
}
10+
11+
module.exports = {
12+
concat: concat
13+
};

test/integration.spec.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ test('integration : should display eslint warnings', t => {
4141
4242
✖ 1 problem (0 errors, 1 warning)
4343
`,
44-
''
44+
'',
45+
'You may use special comments to disable some warnings.',
46+
'Use // eslint-disable-next-line to ignore the next line.',
47+
'Use /* eslint-disable */ to ignore all warnings in a file.'
4548
]);
4649
});
4750

test/unit/transformers/moduleNotFound.spec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ const expect = require('expect');
33
const test = require('ava');
44

55
const error = {
6-
type: 'module-not-found',
6+
name: 'ModuleNotFoundError',
7+
message: 'Module not found : redux',
78
webpackError: {
89
dependencies: [{ request: 'redux' } ],
910
},

test/unit/utils/utils.spec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const concat = require('../../../src/utils').concat;
2+
const test = require('ava');
3+
var assert = require('assert-diff');
4+
5+
test('should concat removing undefined and null values', () => {
6+
var result = concat(1, undefined, '', null);
7+
assert.deepEqual(result,
8+
[1, '']
9+
);
10+
});
11+
12+
test('should handle arrays', () => {
13+
var result = concat(1, [2, 3], null);
14+
assert.deepEqual(result,
15+
[1, 2, 3]
16+
);
17+
});

0 commit comments

Comments
 (0)