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

Commit

Permalink
Added: eslintPath option (#183)
Browse files Browse the repository at this point in the history
* Add eslintPath option

* Add test case

* Documentation

* Fix

* Fix test case
  • Loading branch information
trungdq88 authored and MoOx committed Jul 6, 2017
1 parent 579e727 commit 651576b
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 6 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,28 @@ module.exports = {
}
```

#### `eslintPath` (default: "eslint")

Path to `eslint` instance that will be used for linting.

```js
module.exports = {
entry: "...",
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: "eslint-loader",
options: {
eslintPath: path.join(__dirname, "reusable-eslint-rules.js"),
}
},
],
},
}
```

#### Errors and Warning

**By default the loader will auto adjust error reporting depending
Expand Down
19 changes: 13 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"use strict"

var eslint = require("eslint")
var assign = require("object-assign")
var loaderUtils = require("loader-utils")
var objectHash = require("object-hash")
Expand Down Expand Up @@ -68,6 +67,7 @@ function printLinterOutput(res, config, webpack) {

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

Expand Down Expand Up @@ -142,19 +142,25 @@ function printLinterOutput(res, config, webpack) {
*/
module.exports = function(input, map) {
var webpack = this

var userOptions = assign(
// user defaults
this.options.eslint || {},
// loader query string
loaderUtils.getOptions(this)
)

var config = assign(
// loader defaults
{
formatter: require("eslint/lib/formatters/stylish"),
cacheIdentifier: JSON.stringify({
"eslint-loader": pkg.version,
eslint: eslint.version,
eslint: require(userOptions.eslintPath || "eslint").version,
}),
eslintPath: "eslint",
},
// user defaults
this.options.eslint || {},
// loader query string
loaderUtils.getOptions(this)
userOptions
)

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

Expand Down
33 changes: 33 additions & 0 deletions test/eslint-path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
var path = require("path")
var test = require("ava")
var webpack = require("webpack")
var conf = require("./utils/conf")

test.cb("eslint-loader can use another instance of eslint via " +
"eslintPath config", function(t) {
t.plan(2)
webpack(conf(
{
entry: "./test/fixtures/good.js",
},
{
eslintPath: path.join(__dirname, "mock/eslint-mock.js"),
}
),
function(err, stats) {
if (err) {
throw err
}

// console.log(stats.compilation.errors)
t.true(
stats.hasErrors(),
"a file that does not contains error but mock eslint instance " +
"returned error"
)
t.true(
(stats.compilation.errors[0].message + "").indexOf("Fake error") > -1
)
t.end()
})
})
33 changes: 33 additions & 0 deletions test/mock/eslint-mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
function CLIEngine() {

}

CLIEngine.prototype.executeOnText = function() {
return {
results: [{
filePath: "",
messages: [{
ruleId: "no-undef",
severity: 2,
message: "Fake error",
line: 1,
column: 11,
nodeType: "Identifier",
source: "var foo = stuff",
}],
errorCount: 2,
warningCount: 0,
fixableErrorCount: 0,
fixableWarningCount: 0,
source: "",
}],
errorCount: 2,
warningCount: 0,
fixableErrorCount: 0,
fixableWarningCount: 0,
}
}

module.exports = {
CLIEngine: CLIEngine,
}

0 comments on commit 651576b

Please sign in to comment.