/
plugins.js
255 lines (203 loc) · 9.08 KB
/
plugins.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/**
* @fileoverview Tests for Plugins
* @author Nicholas C. Zakas
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const assert = require("chai").assert,
Plugins = require("../../../lib/config/plugins");
const proxyquire = require("proxyquire").noCallThru().noPreserveCache();
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
describe("Plugins", function() {
describe("get", function() {
it("should return null when plugin doesn't exist", function() {
assert.isNull(Plugins.get("foo"));
});
});
describe("load()", function() {
let StubbedPlugins,
Rules,
Environments,
plugin,
scopedPlugin;
beforeEach(function() {
plugin = {};
scopedPlugin = {};
Environments = require("../../../lib/config/environments");
Rules = require("../../../lib/rules");
StubbedPlugins = proxyquire("../../../lib/config/plugins", {
"eslint-plugin-example": plugin,
"@scope/eslint-plugin-example": scopedPlugin,
"./environments": Environments,
"../rules": Rules
});
});
it("should load a plugin when referenced by short name", function() {
StubbedPlugins.load("example");
assert.equal(StubbedPlugins.get("example"), plugin);
});
it("should load a plugin when referenced by long name", function() {
StubbedPlugins.load("eslint-plugin-example");
assert.equal(StubbedPlugins.get("example"), plugin);
});
it("should register environments when plugin has environments", function() {
plugin.environments = {
foo: {
globals: { foo: true }
},
bar: {
globals: { bar: false }
}
};
StubbedPlugins.load("eslint-plugin-example");
assert.deepEqual(Environments.get("example/foo"), plugin.environments.foo);
assert.deepEqual(Environments.get("example/bar"), plugin.environments.bar);
});
it("should register rules when plugin has rules", function() {
plugin.rules = {
baz: {},
qux: {}
};
StubbedPlugins.load("eslint-plugin-example");
assert.deepEqual(Rules.get("example/baz"), plugin.rules.baz);
assert.deepEqual(Rules.get("example/qux"), plugin.rules.qux);
});
it("should throw an error when a plugin has whitespace", function() {
assert.throws(function() {
StubbedPlugins.load("whitespace ");
}, /Whitespace found in plugin name 'whitespace '/);
assert.throws(function() {
StubbedPlugins.load("whitespace\t");
}, /Whitespace found in plugin name/);
assert.throws(function() {
StubbedPlugins.load("whitespace\n");
}, /Whitespace found in plugin name/);
assert.throws(function() {
StubbedPlugins.load("whitespace\r");
}, /Whitespace found in plugin name/);
});
it("should throw an error when a plugin doesn't exist", function() {
assert.throws(function() {
StubbedPlugins.load("nonexistentplugin");
}, /Failed to load plugin/);
});
it("should load a scoped plugin when referenced by short name", () => {
StubbedPlugins.load("@scope/example");
assert.equal(StubbedPlugins.get("@scope/example"), scopedPlugin);
});
it("should load a scoped plugin when referenced by long name", () => {
StubbedPlugins.load("@scope/eslint-plugin-example");
assert.equal(StubbedPlugins.get("@scope/example"), scopedPlugin);
});
it("should register environments when scoped plugin has environments", () => {
scopedPlugin.environments = {
foo: {}
};
StubbedPlugins.load("@scope/eslint-plugin-example");
assert.equal(Environments.get("@scope/example/foo"), scopedPlugin.environments.foo);
});
it("should register rules when scoped plugin has rules", () => {
scopedPlugin.rules = {
foo: {}
};
StubbedPlugins.load("@scope/eslint-plugin-example");
assert.equal(Rules.get("@scope/example/foo"), scopedPlugin.rules.foo);
});
describe("(NOTE: those behavior will be removed by 4.0.0)", () => {
it("should load a scoped plugin when referenced by short name, and should get the plugin even if '@scope/' is omitted", () => {
StubbedPlugins.load("@scope/example");
assert.equal(StubbedPlugins.get("example"), scopedPlugin);
});
it("should load a scoped plugin when referenced by long name, and should get the plugin even if '@scope/' is omitted", () => {
StubbedPlugins.load("@scope/eslint-plugin-example");
assert.equal(StubbedPlugins.get("example"), scopedPlugin);
});
it("should register environments when scoped plugin has environments, and should get the environment even if '@scope/' is omitted", () => {
scopedPlugin.environments = {
foo: {}
};
StubbedPlugins.load("@scope/eslint-plugin-example");
assert.equal(Environments.get("example/foo"), scopedPlugin.environments.foo);
});
it("should register rules when scoped plugin has rules, and should get the rule even if '@scope/' is omitted", () => {
scopedPlugin.rules = {
foo: {}
};
StubbedPlugins.load("@scope/eslint-plugin-example");
assert.equal(Rules.get("example/foo"), scopedPlugin.rules.foo);
});
});
});
describe("loadAll()", function() {
let StubbedPlugins,
Rules,
Environments,
plugin1,
plugin2;
beforeEach(function() {
plugin1 = {};
plugin2 = {};
Environments = require("../../../lib/config/environments");
Rules = require("../../../lib/rules");
StubbedPlugins = proxyquire("../../../lib/config/plugins", {
"eslint-plugin-example1": plugin1,
"eslint-plugin-example2": plugin2,
"./environments": Environments,
"../rules": Rules
});
});
it("should load plugins when passed multiple plugins", function() {
StubbedPlugins.loadAll(["example1", "example2"]);
assert.equal(StubbedPlugins.get("example1"), plugin1);
assert.equal(StubbedPlugins.get("example2"), plugin2);
});
it("should load environments from plugins when passed multiple plugins", function() {
plugin1.environments = {
foo: {}
};
plugin2.environments = {
bar: {}
};
StubbedPlugins.loadAll(["example1", "example2"]);
assert.equal(Environments.get("example1/foo"), plugin1.environments.foo);
assert.equal(Environments.get("example2/bar"), plugin2.environments.bar);
});
it("should load rules from plugins when passed multiple plugins", function() {
plugin1.rules = {
foo: {}
};
plugin2.rules = {
bar: {}
};
StubbedPlugins.loadAll(["example1", "example2"]);
assert.equal(Rules.get("example1/foo"), plugin1.rules.foo);
assert.equal(Rules.get("example2/bar"), plugin2.rules.bar);
});
});
describe("removePrefix()", function() {
it("should remove common prefix when passed a plugin name with a prefix", function() {
const pluginName = Plugins.removePrefix("eslint-plugin-test");
assert.equal(pluginName, "test");
});
it("should not modify plugin name when passed a plugin name without a prefix", function() {
const pluginName = Plugins.removePrefix("test");
assert.equal(pluginName, "test");
});
});
describe("getNamespace()", function() {
it("should remove namepace when passed with namepace", function() {
const namespace = Plugins.getNamespace("@namepace/eslint-plugin-test");
assert.equal(namespace, "@namepace/");
});
});
describe("removeNamespace()", function() {
it("should remove namepace when passed with namepace", function() {
const namespace = Plugins.removeNamespace("@namepace/eslint-plugin-test");
assert.equal(namespace, "eslint-plugin-test");
});
});
});