forked from webpack-contrib/eslint-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
78 lines (66 loc) · 2.14 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"use strict";
var eslint = require("eslint")
// eslint empty filename
var TEXT = "<text>"
/**
* linter
*
* @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 = 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 messages = webpack.options.eslint.reporter(res.results)
if (messages.indexOf(TEXT) > -1) {
messages = messages.split("\n").filter(function(line) {
// drop the line that should contains filepath we do not have
return !line.match(TEXT)
}).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
}
if (emitter) {
emitter(messages)
}
else {
throw new Error("Your module system doesn't support emitWarning. Update available? \n" + messages)
}
}
}
/**
* webpack loader
*
* @param {String|Buffer} input JavaScript string
* @returns {String|Buffer} original input
*/
module.exports = function(input) {
this.options.eslint = this.options.eslint || {}
this.options.eslint.reporter = this.options.eslint.reporter || require("eslint/lib/formatters/stylish")
this.cacheable()
// sync loader
var config = new eslint.CLIEngine(this.options.eslint)
lint(input, config, this)
// this loader do nothing
return input
}