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

Commit 651576b

Browse files
trungdq88MoOx
authored andcommitted
Added: eslintPath option (#183)
* Add eslintPath option * Add test case * Documentation * Fix * Fix test case
1 parent 579e727 commit 651576b

File tree

4 files changed

+101
-6
lines changed

4 files changed

+101
-6
lines changed

README.md

+22
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,28 @@ module.exports = {
142142
}
143143
```
144144

145+
#### `eslintPath` (default: "eslint")
146+
147+
Path to `eslint` instance that will be used for linting.
148+
149+
```js
150+
module.exports = {
151+
entry: "...",
152+
module: {
153+
rules: [
154+
{
155+
test: /\.js$/,
156+
exclude: /node_modules/,
157+
loader: "eslint-loader",
158+
options: {
159+
eslintPath: path.join(__dirname, "reusable-eslint-rules.js"),
160+
}
161+
},
162+
],
163+
},
164+
}
165+
```
166+
145167
#### Errors and Warning
146168

147169
**By default the loader will auto adjust error reporting depending

index.js

+13-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"use strict"
22

3-
var eslint = require("eslint")
43
var assign = require("object-assign")
54
var loaderUtils = require("loader-utils")
65
var objectHash = require("object-hash")
@@ -68,6 +67,7 @@ function printLinterOutput(res, config, webpack) {
6867

6968
// if enabled, use eslint auto-fixing where possible
7069
if (config.fix && res.results[0].output) {
70+
var eslint = require(config.eslintPath)
7171
eslint.CLIEngine.outputFixes(res)
7272
}
7373

@@ -142,19 +142,25 @@ function printLinterOutput(res, config, webpack) {
142142
*/
143143
module.exports = function(input, map) {
144144
var webpack = this
145+
146+
var userOptions = assign(
147+
// user defaults
148+
this.options.eslint || {},
149+
// loader query string
150+
loaderUtils.getOptions(this)
151+
)
152+
145153
var config = assign(
146154
// loader defaults
147155
{
148156
formatter: require("eslint/lib/formatters/stylish"),
149157
cacheIdentifier: JSON.stringify({
150158
"eslint-loader": pkg.version,
151-
eslint: eslint.version,
159+
eslint: require(userOptions.eslintPath || "eslint").version,
152160
}),
161+
eslintPath: "eslint",
153162
},
154-
// user defaults
155-
this.options.eslint || {},
156-
// loader query string
157-
loaderUtils.getOptions(this)
163+
userOptions
158164
)
159165

160166
var cacheDirectory = config.cache
@@ -166,6 +172,7 @@ module.exports = function(input, map) {
166172
// Create the engine only once per config
167173
var configHash = objectHash(config)
168174
if (!engines[configHash]) {
175+
var eslint = require(config.eslintPath)
169176
engines[configHash] = new eslint.CLIEngine(config)
170177
}
171178

test/eslint-path.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
var path = require("path")
2+
var test = require("ava")
3+
var webpack = require("webpack")
4+
var conf = require("./utils/conf")
5+
6+
test.cb("eslint-loader can use another instance of eslint via " +
7+
"eslintPath config", function(t) {
8+
t.plan(2)
9+
webpack(conf(
10+
{
11+
entry: "./test/fixtures/good.js",
12+
},
13+
{
14+
eslintPath: path.join(__dirname, "mock/eslint-mock.js"),
15+
}
16+
),
17+
function(err, stats) {
18+
if (err) {
19+
throw err
20+
}
21+
22+
// console.log(stats.compilation.errors)
23+
t.true(
24+
stats.hasErrors(),
25+
"a file that does not contains error but mock eslint instance " +
26+
"returned error"
27+
)
28+
t.true(
29+
(stats.compilation.errors[0].message + "").indexOf("Fake error") > -1
30+
)
31+
t.end()
32+
})
33+
})

test/mock/eslint-mock.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
function CLIEngine() {
2+
3+
}
4+
5+
CLIEngine.prototype.executeOnText = function() {
6+
return {
7+
results: [{
8+
filePath: "",
9+
messages: [{
10+
ruleId: "no-undef",
11+
severity: 2,
12+
message: "Fake error",
13+
line: 1,
14+
column: 11,
15+
nodeType: "Identifier",
16+
source: "var foo = stuff",
17+
}],
18+
errorCount: 2,
19+
warningCount: 0,
20+
fixableErrorCount: 0,
21+
fixableWarningCount: 0,
22+
source: "",
23+
}],
24+
errorCount: 2,
25+
warningCount: 0,
26+
fixableErrorCount: 0,
27+
fixableWarningCount: 0,
28+
}
29+
}
30+
31+
module.exports = {
32+
CLIEngine: CLIEngine,
33+
}

0 commit comments

Comments
 (0)