Skip to content
This repository was archived by the owner on Sep 28, 2020. It is now read-only.

Commit 0532fe3

Browse files
committed
Changed: loader now use webpack.emitError or webpack.emitWarning automatically (according to eslint configuration)
Changed: `emitErrors` is now `emitError` Added: `emitWarning` can force eslint to report warning instead of the default behavior
1 parent 9d52d09 commit 0532fe3

File tree

8 files changed

+150
-100
lines changed

8 files changed

+150
-100
lines changed

CHANGELOG.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
1+
# 0.5.0 - 2015-02-11
2+
3+
- Changed: upgrade to eslint 0.16.x
4+
- Changed: `emitErrors` is now `emitError`
5+
- Changed: loader now use `webpack.emitError` or `webpack.emitWarning` automatically (according to eslint configuration).
6+
You can still override by using `emitError` or `emitWarning` options to override this behavior
7+
- Added: `emitWarning` can force eslint to report warning instead of the default behavior (see above)
8+
- Added: `quiet` option to hide warnings
9+
10+
111
# 0.4.0 - 2015-02-23
212

3-
- Changed: upgrate to eslint 0.15.x
13+
- Changed: upgrade to eslint 0.15.x
414
- Changed: more readable default reporter
515
- Added: `reporter` options allow to define a custom reporter function
616

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,15 @@ module.exports = {
4141
Loader accepts a function that will have one argument: an array of eslint messages (object).
4242
The function must return the output as a string.
4343

44-
#### `emitErrors` (default: `false`)
44+
#### Errors and Warning
4545

46-
Loader will return errors instead of warnings if this option is set to true
46+
**By default the loader will auto adjust error reporting depending
47+
on eslint errors/warnings counts.**
48+
You can still force this behavior
49+
50+
##### `emitError` (default: `false`)
51+
52+
Loader will always returns errors if this option is set to `true`.
4753

4854
```js
4955
module.exports = {
@@ -57,6 +63,10 @@ module.exports = {
5763
}
5864
```
5965

66+
##### `emitWarning` (default: `false`)
67+
68+
Loader will always returns warning if option is set to `true`.
69+
6070
#### `quiet` (default: `false`)
6171

6272
Loader will process and report errors only and ignore warnings if this option is set to true

index.js

Lines changed: 44 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,53 @@
11
"use strict";
22

3-
var eslint = require("eslint").linter
4-
var Config = require("eslint/lib/config");
5-
3+
var eslint = require("eslint")
64
var stylish = require("eslint/lib/formatters/stylish")
75

6+
// eslint empty filename
7+
var TEXT = "<text>"
8+
89
/**
910
* linter
1011
*
11-
* @param {String|Buffer} input
12-
* @param {Object} config
13-
* @param {Object} webpack
12+
* @param {String|Buffer} input JavaScript string
13+
* @param {Object} config eslint configuration
14+
* @param {Object} webpack webpack instance
15+
* @returns {void}
1416
*/
1517
function lint(input, config, webpack) {
16-
var res = eslint.verify(input, config)
17-
if (res.length) {
18+
var res = config.executeOnText(input)
19+
// executeOnText ensure we will have res.results[0] only
20+
21+
// quiet filter done now
22+
// eslint allow rules to be specified in the input between comments
23+
// so we can found warnings defined in the input itself
24+
if (res.warningCount && webpack.options.eslint.quiet) {
25+
res.warningCount = 0
26+
res.results[0].warningCount = 0
27+
res.results[0].messages = res.results[0].messages.filter(function(message) {
28+
return message.severity !== 1
29+
})
30+
}
31+
32+
if (res.errorCount || res.warningCount) {
1833
var reporter = webpack.options.eslint.reporter || function(results) {
19-
// in order to use eslint formatters
20-
// we need to reproduce the object passed to them
21-
var msgs = stylish([{
22-
filePath: "",
23-
messages: results
24-
}]).split("\n")
25-
// drop the line that should contains filepath we do not have
26-
msgs.splice(0, 1)
34+
return stylish(results).split("\n").filter(function(line) {
35+
// drop the line that should contains filepath we do not have
36+
return !line.match(TEXT)
37+
}).join("\n")
38+
}
39+
var messages = reporter(res.results)
2740

28-
return msgs.join("\n")
41+
// default behavior: emit error only if we have errors
42+
var emitter = res.errorCount ? webpack.emitError : webpack.emitWarning
43+
44+
// force emitError or emitWarning if user want this
45+
if (webpack.options.eslint.emitError) {
46+
emitter = webpack.emitError
47+
}
48+
else if (webpack.options.eslint.emitWarning) {
49+
emitter = webpack.emitWarning
2950
}
30-
var messages = reporter(res)
31-
var emitter = webpack.options.eslint.emitErrors ? webpack.emitError : webpack.emitWarning
3251

3352
if (emitter) {
3453
emitter(messages)
@@ -39,47 +58,21 @@ function lint(input, config, webpack) {
3958
}
4059
}
4160

42-
/**
43-
* quiet filter
44-
*
45-
* @param {Object} config
46-
* @return {Object}
47-
*/
48-
function quiet(config) {
49-
var rules = config.rules;
50-
51-
Object.keys(rules).forEach(function(key) {
52-
var rule = rules[key];
53-
54-
if (rule.constructor === Array && rule[0] === 1){
55-
rules[key][0] = 0;
56-
}
57-
else if (rule === 1) {
58-
rules[key] = 0;
59-
}
60-
});
61-
return config;
62-
}
63-
6461
/**
6562
* webpack loader
6663
*
67-
* @param {String|Buffer} input
68-
* @return {String|Buffer}
64+
* @param {String|Buffer} input JavaScript string
65+
* @returns {String|Buffer} original input
6966
*/
7067
module.exports = function(input) {
7168
this.options.eslint = this.options.eslint || {}
72-
7369
this.cacheable()
7470

75-
// sync
76-
var config = new Config(this.options.eslint).getConfig()
77-
78-
// remove warnings if quiet is set
79-
if (this.options.eslint.quiet) {
80-
config = quiet(config);
81-
}
71+
// sync loader
72+
var config = new eslint.CLIEngine(this.options.eslint)
8273

8374
lint(input, config, this)
75+
76+
// this loader do nothing
8477
return input
8578
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "eslint-loader",
3-
"version": "0.4.0",
3+
"version": "0.5.0",
44
"description": "eslint loader (for webpack)",
55
"keywords": [
66
"lint",
File renamed without changes.

test/fixtures/good.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1-
module.exports = {
2-
value: "value"
3-
};
1+
"use strict";
2+
3+
function test() {
4+
return "value"
5+
}
6+
7+
test()

test/fixtures/warn.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/*eslint no-unused-vars: 1*/
2+
var foo = this;

test/index.js

Lines changed: 73 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,89 +8,120 @@ var assign = require("object-assign")
88
var conf = {
99
output: {
1010
path: "./test/output/",
11-
filename: "bundle.js"
11+
filename: "bundle.js",
1212
},
1313
module: {
1414
loaders: [
15-
{test: /\.js$/, loader: "./index", exclude: /node_modules/}
16-
]
17-
}
15+
{
16+
test: /\.js$/,
17+
loader: "./index",
18+
exclude: /node_modules/,
19+
},
20+
],
21+
},
1822
}
1923

2024
test("eslint-loader don't throw error if file is ok", function(t) {
2125
webpack(assign({
22-
entry: "./test/fixtures/good.js"
26+
entry: "./test/fixtures/good.js",
2327
}, conf),
2428
function(err, stats) {
25-
if (err) {
26-
throw err
27-
}
29+
if (err) {throw err}
2830

2931
t.notOk(stats.hasErrors(), "a good file doesn't give any error")
3032
t.notOk(stats.hasWarnings(), "a good file doesn't give any warning")
3133
t.end()
3234
})
3335
})
3436

35-
test("eslint-loader can return warning if file is bad", function(t) {
37+
test("eslint-loader can return warning", function(t) {
3638
webpack(assign({
37-
entry: "./test/fixtures/bad.js"
39+
entry: "./test/fixtures/warn.js",
3840
}, conf),
3941
function(err, stats) {
40-
if (err) {
41-
throw err
42-
}
42+
if (err) {throw err}
4343

44-
t.ok(stats.hasWarnings(), "a bad file should returns warning")
45-
t.notOk(stats.hasErrors(), "a bad file should returns no error if not asked for")
44+
t.ok(stats.hasWarnings(), "a file that contains eslint warning should return warning")
45+
t.notOk(stats.hasErrors(), "a bad file should return no error if it contains only warning by default")
4646
t.end()
4747
})
4848
})
4949

50+
test("eslint-loader only returns errors and not warnings if quiet is set", function(t) {
51+
webpack(assign({
52+
entry: "./test/fixtures/warn.js",
53+
eslint: {
54+
quiet: true,
55+
},
56+
}, conf),
57+
function(err, stats) {
58+
if (err) {throw err}
59+
60+
t.notOk(stats.hasWarnings(), "a file that contains eslint warning should return nothing if quiet option is true")
61+
t.notOk(stats.hasErrors(), "a file that contains eslint warning should return no error if it contains only warning in quiet mode")
62+
t.end()
63+
})
64+
})
5065

5166
test("eslint-loader can return error if file is bad", function(t) {
5267
webpack(assign({
53-
entry: "./test/fixtures/bad.js",
68+
entry: "./test/fixtures/error.js",
69+
}, conf),
70+
function(err, stats) {
71+
if (err) {throw err}
72+
73+
t.ok(stats.hasErrors(), "a file that contains eslint errors should return error")
74+
t.end()
75+
})
76+
})
77+
78+
test("eslint-loader can force to emit error", function(t) {
79+
webpack(assign({
80+
entry: "./test/fixtures/warn.js",
5481
eslint: {
55-
emitErrors: true,
56-
rules: {
57-
"no-unused-vars": 1
58-
}
59-
}
82+
emitError: true,
83+
},
6084
}, conf),
6185
function(err, stats) {
62-
if (err) {
63-
throw err
64-
}
86+
if (err) {throw err}
6587

66-
t.ok(stats.hasErrors(), "a bad file should return error if asked")
67-
t.notOk(stats.hasWarnings(), "a bad file should return no warning if error asked")
88+
t.ok(stats.hasErrors(), "a file should return error if asked")
89+
t.notOk(stats.hasWarnings(), "a file should return no warning if error asked")
90+
t.end()
91+
})
92+
})
6893

69-
console.log("### Here is a example of the default reporter")
70-
console.log("# " + stats.compilation.errors[0].message.split("\n").join("\n# "))
94+
test("eslint-loader can force to emit warning", function(t) {
95+
webpack(assign({
96+
entry: "./test/fixtures/error.js",
97+
eslint: {
98+
emitWarning: true,
99+
},
100+
}, conf),
101+
function(err, stats) {
102+
if (err) {throw err}
103+
104+
t.ok(stats.hasWarnings(), "a file should return warning if asked")
105+
t.notOk(stats.hasErrors(), "a file should return no error if error asked")
71106
t.end()
72107
})
73108
})
74109

75-
test("eslint-loader only returns errors and not warnings if quiet is set", function(t) {
110+
test("eslint-loader can return error if file is bad", function(t) {
76111
webpack(assign({
77-
entry: "./test/fixtures/bad.js",
112+
entry: "./test/fixtures/error.js",
78113
eslint: {
79-
quiet: true,
80114
rules: {
81-
"no-unused-vars": 1
82-
}
83-
}
115+
"no-unused-vars": 1,
116+
},
117+
},
84118
}, conf),
85119
function(err, stats) {
86-
if (err) {
87-
throw err;
88-
}
120+
if (err) {throw err}
89121

90-
t.equal(Number(stats.compilation.warnings[0].message.match(/(\d+) warnings?/)[1]), 0, "the rules set as warnings should be ignored");
91-
92-
console.log("### Here is an example of the output when quiet is set to true");
93-
console.log("# " + stats.compilation.warnings[0].message.split("\n").join("\n# "));
94-
t.end();
122+
console.log("### Here is a example of the default reporter")
123+
console.log("# " + stats.compilation.errors[0].message.split("\n").join("\n# "))
124+
t.ok(stats.compilation.errors[0].message, "webpack have some output")
125+
t.end()
95126
})
96127
})

0 commit comments

Comments
 (0)