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

Commit

Permalink
Changed: loader now use webpack.emitError or webpack.emitWarning
Browse files Browse the repository at this point in the history
…automatically (according to eslint configuration)

Changed: `emitErrors` is now `emitError`
Added: `emitWarning` can force eslint to report warning instead of the
default behavior
  • Loading branch information
MoOx committed Mar 11, 2015
1 parent 9d52d09 commit 0532fe3
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 100 deletions.
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
# 0.5.0 - 2015-02-11

- Changed: upgrade to eslint 0.16.x
- Changed: `emitErrors` is now `emitError`
- Changed: loader now use `webpack.emitError` or `webpack.emitWarning` automatically (according to eslint configuration).
You can still override by using `emitError` or `emitWarning` options to override this behavior
- Added: `emitWarning` can force eslint to report warning instead of the default behavior (see above)
- Added: `quiet` option to hide warnings


# 0.4.0 - 2015-02-23

- Changed: upgrate to eslint 0.15.x
- Changed: upgrade to eslint 0.15.x
- Changed: more readable default reporter
- Added: `reporter` options allow to define a custom reporter function

Expand Down
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,15 @@ module.exports = {
Loader accepts a function that will have one argument: an array of eslint messages (object).
The function must return the output as a string.

#### `emitErrors` (default: `false`)
#### Errors and Warning

Loader will return errors instead of warnings if this option is set to true
**By default the loader will auto adjust error reporting depending
on eslint errors/warnings counts.**
You can still force this behavior

##### `emitError` (default: `false`)

Loader will always returns errors if this option is set to `true`.

```js
module.exports = {
Expand All @@ -57,6 +63,10 @@ module.exports = {
}
```

##### `emitWarning` (default: `false`)

Loader will always returns warning if option is set to `true`.

#### `quiet` (default: `false`)

Loader will process and report errors only and ignore warnings if this option is set to true
Expand Down
95 changes: 44 additions & 51 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,53 @@
"use strict";

var eslint = require("eslint").linter
var Config = require("eslint/lib/config");

var eslint = require("eslint")
var stylish = require("eslint/lib/formatters/stylish")

// eslint empty filename
var TEXT = "<text>"

/**
* linter
*
* @param {String|Buffer} input
* @param {Object} config
* @param {Object} webpack
* @param {String|Buffer} input JavaScript string
* @param {Object} config eslint configuration
* @param {Object} webpack webpack instance
* @returns {void}
*/
function lint(input, config, webpack) {
var res = eslint.verify(input, config)
if (res.length) {
var res = config.executeOnText(input)
// executeOnText ensure we will have res.results[0] only

// quiet filter done now
// eslint allow rules to be specified in the input between comments
// so we can found warnings defined in the input itself
if (res.warningCount && webpack.options.eslint.quiet) {
res.warningCount = 0
res.results[0].warningCount = 0
res.results[0].messages = res.results[0].messages.filter(function(message) {
return message.severity !== 1
})
}

if (res.errorCount || res.warningCount) {
var reporter = webpack.options.eslint.reporter || function(results) {
// in order to use eslint formatters
// we need to reproduce the object passed to them
var msgs = stylish([{
filePath: "",
messages: results
}]).split("\n")
// drop the line that should contains filepath we do not have
msgs.splice(0, 1)
return stylish(results).split("\n").filter(function(line) {
// drop the line that should contains filepath we do not have
return !line.match(TEXT)
}).join("\n")
}
var messages = reporter(res.results)

return msgs.join("\n")
// default behavior: emit error only if we have errors
var emitter = res.errorCount ? webpack.emitError : webpack.emitWarning

// force emitError or emitWarning if user want this
if (webpack.options.eslint.emitError) {
emitter = webpack.emitError
}
else if (webpack.options.eslint.emitWarning) {
emitter = webpack.emitWarning
}
var messages = reporter(res)
var emitter = webpack.options.eslint.emitErrors ? webpack.emitError : webpack.emitWarning

if (emitter) {
emitter(messages)
Expand All @@ -39,47 +58,21 @@ function lint(input, config, webpack) {
}
}

/**
* quiet filter
*
* @param {Object} config
* @return {Object}
*/
function quiet(config) {
var rules = config.rules;

Object.keys(rules).forEach(function(key) {
var rule = rules[key];

if (rule.constructor === Array && rule[0] === 1){
rules[key][0] = 0;
}
else if (rule === 1) {
rules[key] = 0;
}
});
return config;
}

/**
* webpack loader
*
* @param {String|Buffer} input
* @return {String|Buffer}
* @param {String|Buffer} input JavaScript string
* @returns {String|Buffer} original input
*/
module.exports = function(input) {
this.options.eslint = this.options.eslint || {}

this.cacheable()

// sync
var config = new Config(this.options.eslint).getConfig()

// remove warnings if quiet is set
if (this.options.eslint.quiet) {
config = quiet(config);
}
// sync loader
var config = new eslint.CLIEngine(this.options.eslint)

lint(input, config, this)

// this loader do nothing
return input
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-loader",
"version": "0.4.0",
"version": "0.5.0",
"description": "eslint loader (for webpack)",
"keywords": [
"lint",
Expand Down
File renamed without changes.
10 changes: 7 additions & 3 deletions test/fixtures/good.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
module.exports = {
value: "value"
};
"use strict";

function test() {
return "value"
}

test()
2 changes: 2 additions & 0 deletions test/fixtures/warn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/*eslint no-unused-vars: 1*/
var foo = this;
115 changes: 73 additions & 42 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,89 +8,120 @@ var assign = require("object-assign")
var conf = {
output: {
path: "./test/output/",
filename: "bundle.js"
filename: "bundle.js",
},
module: {
loaders: [
{test: /\.js$/, loader: "./index", exclude: /node_modules/}
]
}
{
test: /\.js$/,
loader: "./index",
exclude: /node_modules/,
},
],
},
}

test("eslint-loader don't throw error if file is ok", function(t) {
webpack(assign({
entry: "./test/fixtures/good.js"
entry: "./test/fixtures/good.js",
}, conf),
function(err, stats) {
if (err) {
throw err
}
if (err) {throw err}

t.notOk(stats.hasErrors(), "a good file doesn't give any error")
t.notOk(stats.hasWarnings(), "a good file doesn't give any warning")
t.end()
})
})

test("eslint-loader can return warning if file is bad", function(t) {
test("eslint-loader can return warning", function(t) {
webpack(assign({
entry: "./test/fixtures/bad.js"
entry: "./test/fixtures/warn.js",
}, conf),
function(err, stats) {
if (err) {
throw err
}
if (err) {throw err}

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

test("eslint-loader only returns errors and not warnings if quiet is set", function(t) {
webpack(assign({
entry: "./test/fixtures/warn.js",
eslint: {
quiet: true,
},
}, conf),
function(err, stats) {
if (err) {throw err}

t.notOk(stats.hasWarnings(), "a file that contains eslint warning should return nothing if quiet option is true")
t.notOk(stats.hasErrors(), "a file that contains eslint warning should return no error if it contains only warning in quiet mode")
t.end()
})
})

test("eslint-loader can return error if file is bad", function(t) {
webpack(assign({
entry: "./test/fixtures/bad.js",
entry: "./test/fixtures/error.js",
}, conf),
function(err, stats) {
if (err) {throw err}

t.ok(stats.hasErrors(), "a file that contains eslint errors should return error")
t.end()
})
})

test("eslint-loader can force to emit error", function(t) {
webpack(assign({
entry: "./test/fixtures/warn.js",
eslint: {
emitErrors: true,
rules: {
"no-unused-vars": 1
}
}
emitError: true,
},
}, conf),
function(err, stats) {
if (err) {
throw err
}
if (err) {throw err}

t.ok(stats.hasErrors(), "a bad file should return error if asked")
t.notOk(stats.hasWarnings(), "a bad file should return no warning if error asked")
t.ok(stats.hasErrors(), "a file should return error if asked")
t.notOk(stats.hasWarnings(), "a file should return no warning if error asked")
t.end()
})
})

console.log("### Here is a example of the default reporter")
console.log("# " + stats.compilation.errors[0].message.split("\n").join("\n# "))
test("eslint-loader can force to emit warning", function(t) {
webpack(assign({
entry: "./test/fixtures/error.js",
eslint: {
emitWarning: true,
},
}, conf),
function(err, stats) {
if (err) {throw err}

t.ok(stats.hasWarnings(), "a file should return warning if asked")
t.notOk(stats.hasErrors(), "a file should return no error if error asked")
t.end()
})
})

test("eslint-loader only returns errors and not warnings if quiet is set", function(t) {
test("eslint-loader can return error if file is bad", function(t) {
webpack(assign({
entry: "./test/fixtures/bad.js",
entry: "./test/fixtures/error.js",
eslint: {
quiet: true,
rules: {
"no-unused-vars": 1
}
}
"no-unused-vars": 1,
},
},
}, conf),
function(err, stats) {
if (err) {
throw err;
}
if (err) {throw err}

t.equal(Number(stats.compilation.warnings[0].message.match(/(\d+) warnings?/)[1]), 0, "the rules set as warnings should be ignored");

console.log("### Here is an example of the output when quiet is set to true");
console.log("# " + stats.compilation.warnings[0].message.split("\n").join("\n# "));
t.end();
console.log("### Here is a example of the default reporter")
console.log("# " + stats.compilation.errors[0].message.split("\n").join("\n# "))
t.ok(stats.compilation.errors[0].message, "webpack have some output")
t.end()
})
})

0 comments on commit 0532fe3

Please sign in to comment.