Skip to content

Commit d2435b8

Browse files
Make sure it only tries to load all plugins once, avoiding noisy warnings
Related #192
1 parent 38e2498 commit d2435b8

File tree

4 files changed

+100
-22
lines changed

4 files changed

+100
-22
lines changed

lib/eslint-patch.js

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
11
'use strict';
2-
var fs = require('fs');
3-
var path = require('path');
2+
const Plugins = require("eslint/lib/config/plugins");
43

5-
var Plugins = require("eslint/lib/config/plugins");
4+
const Config = require("eslint/lib/config");
5+
const ConfigUpgrader = require('./config_upgrader');
66

7-
var docs = require('./docs');
8-
var Config = require("eslint/lib/config");
9-
var ConfigUpgrader = require('./config_upgrader');
7+
const docs = require('./docs');
108

11-
12-
module.exports = function patcher(engine) {
9+
module.exports = function patch(eslint) {
1310

1411
Plugins.loadAll = function(pluginNames) {
15-
for(var index in pluginNames) {
16-
var name = pluginNames[index];
12+
const loadedPlugins = Object.keys(Plugins.getAll());
13+
if(loadedPlugins.length > 0) {
14+
return;
15+
}
1716

17+
for(const index in pluginNames) {
18+
const name = pluginNames[index];
1819
try {
20+
1921
Plugins.load(name);
20-
console.error('>>>> Plugin loaded', name);
22+
2123
} catch(e) {
22-
console.error('>>>> Problem loading plugin', name);
24+
console.error(`[DEBUG] Plugin ${name} not supported`);
2325
}
2426
}
25-
26-
}
27+
};
2728

2829

2930
const originalGetConfig = Config.prototype.getConfig;
@@ -34,8 +35,8 @@ module.exports = function patcher(engine) {
3435
return configUpgrader.upgrade(originalConfig);
3536
};
3637

37-
engine.docs = docs;
38+
eslint.docs = docs;
3839

3940

40-
return engine;
41+
return eslint;
4142
};

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"devDependencies": {
5454
"chai": "^3.5.0",
5555
"mocha": "^2.5.3",
56+
"sinon": "^1.17.7",
5657
"temp": "^0.8.3"
5758
},
5859
"scripts": {

test/eslint-patch_test.js

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,57 @@
1-
var eslint = require("eslint");
2-
var expect = require("chai").expect;
1+
const expect = require("chai").expect;
2+
const sinon = require("sinon");
3+
const eslint = require("eslint");
4+
5+
const Plugins = require('eslint/lib/config/plugins');
6+
const patch = require("../lib/eslint-patch");
37

48
describe('eslint-patch', function() {
5-
it('intercept plugins', function() {
6-
var Plugins = require('eslint/lib/config/plugins');
7-
var loadAll = Plugins.loadAll;
9+
let loadAll;
810

9-
var patch = require("../lib/eslint-patch");
11+
before(function() {
12+
loadAll = Plugins.loadAll;
1013
patch(eslint);
14+
});
15+
16+
after(function() {
17+
Plugins.loadAll = loadAll;
18+
});
1119

20+
it('intercept plugins', function() {
1221
expect(loadAll).to.not.equal(Plugins.loadAll, 'Plugins.loadAll is not patched');
1322
});
23+
24+
describe('Plugins.loadAll', function() {
25+
it('delegates each plugin to be loaded', function () {
26+
Plugins.getAll = sinon.stub().returns([]);
27+
Plugins.load = sinon.spy();
28+
29+
Plugins.loadAll([ "jasmine", "mocha" ]);
30+
31+
expect(Plugins.load.calledWith("jasmine")).to.be.true;
32+
expect(Plugins.load.calledWith("mocha")).to.be.true;
33+
});
34+
35+
it('only load plugins once', function () {
36+
Plugins.getAll = sinon.stub().returns([ "node" ]);
37+
Plugins.load = sinon.spy();
38+
39+
Plugins.loadAll([ "node" ]);
40+
41+
expect(Plugins.load.called).to.be.false;
42+
});
43+
44+
it('does not raise exception for unsupported plugins', function() {
45+
Plugins.getAll = sinon.stub().returns([]);
46+
Plugins.load = sinon.stub().throws();
47+
48+
function loadPlugin() {
49+
Plugins.loadAll([ 'unsupported-plugin' ]);
50+
}
51+
52+
expect(loadPlugin).to.not.throw();
53+
});
54+
55+
});
56+
1457
});

yarn.lock

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,12 @@ foreach@^2.0.5:
839839
version "2.0.5"
840840
resolved "https://registry.yarnpkg.com/foreach/-/foreach-2.0.5.tgz#0bee005018aeb260d0a3af3ae658dd0136ec1b99"
841841

842+
843+
version "1.1.1"
844+
resolved "https://registry.yarnpkg.com/formatio/-/formatio-1.1.1.tgz#5ed3ccd636551097383465d996199100e86161e9"
845+
dependencies:
846+
samsam "~1.1"
847+
842848
fs.realpath@^1.0.0:
843849
version "1.0.0"
844850
resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f"
@@ -959,6 +965,10 @@ inherits@2, inherits@~2.0.1:
959965
version "2.0.3"
960966
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de"
961967

968+
969+
version "2.0.1"
970+
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.1.tgz#b17d08d326b4423e568eff719f91b0b1cbdf69f1"
971+
962972
inquirer@^0.12.0:
963973
version "0.12.0"
964974
resolved "https://registry.yarnpkg.com/inquirer/-/inquirer-0.12.0.tgz#1ef2bfd63504df0bc75785fff8c2c41df12f077e"
@@ -1132,6 +1142,10 @@ lodash@^4.0.0, lodash@^4.15.0, lodash@^4.2.0, lodash@^4.3.0, lodash@^4.5.1:
11321142
version "4.17.2"
11331143
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.2.tgz#34a3055babe04ce42467b607d700072c7ff6bf42"
11341144

1145+
1146+
version "1.3.2"
1147+
resolved "https://registry.yarnpkg.com/lolex/-/lolex-1.3.2.tgz#7c3da62ffcb30f0f5a80a2566ca24e45d8a01f31"
1148+
11351149
loose-envify@^1.0.0:
11361150
version "1.3.0"
11371151
resolved "https://registry.yarnpkg.com/loose-envify/-/loose-envify-1.3.0.tgz#6b26248c42f6d4fa4b0d8542f78edfcde35642a8"
@@ -1414,6 +1428,10 @@ safe-regex@^1.1.0:
14141428
dependencies:
14151429
ret "~0.1.10"
14161430

1431+
[email protected], samsam@~1.1:
1432+
version "1.1.2"
1433+
resolved "https://registry.yarnpkg.com/samsam/-/samsam-1.1.2.tgz#bec11fdc83a9fda063401210e40176c3024d1567"
1434+
14171435
14181436
version "5.3.0"
14191437
resolved "https://registry.yarnpkg.com/semver/-/semver-5.3.0.tgz#9b2ce5d3de02d17c6012ad326aa6b4d0cf54f94f"
@@ -1430,6 +1448,15 @@ sigmund@~1.0.0:
14301448
version "1.0.1"
14311449
resolved "https://registry.yarnpkg.com/sigmund/-/sigmund-1.0.1.tgz#3ff21f198cad2175f9f3b781853fd94d0d19b590"
14321450

1451+
sinon@^1.17.7:
1452+
version "1.17.7"
1453+
resolved "https://registry.yarnpkg.com/sinon/-/sinon-1.17.7.tgz#4542a4f49ba0c45c05eb2e9dd9d203e2b8efe0bf"
1454+
dependencies:
1455+
formatio "1.1.1"
1456+
lolex "1.3.2"
1457+
samsam "1.1.2"
1458+
util ">=0.10.3 <1"
1459+
14331460
slash@^1.0.0:
14341461
version "1.0.0"
14351462
resolved "https://registry.yarnpkg.com/slash/-/slash-1.0.0.tgz#c41f2f6c39fc16d1cd17ad4b5d896114ae470d55"
@@ -1563,6 +1590,12 @@ util-deprecate@~1.0.1:
15631590
version "1.0.2"
15641591
resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf"
15651592

1593+
"util@>=0.10.3 <1":
1594+
version "0.10.3"
1595+
resolved "https://registry.yarnpkg.com/util/-/util-0.10.3.tgz#7afb1afe50805246489e3db7fe0ed379336ac0f9"
1596+
dependencies:
1597+
inherits "2.0.1"
1598+
15661599
wordwrap@~1.0.0:
15671600
version "1.0.0"
15681601
resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-1.0.0.tgz#27584810891456a4171c8d0226441ade90cbcaeb"

0 commit comments

Comments
 (0)