/
config-array.js
734 lines (641 loc) · 26.8 KB
/
config-array.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
/**
* @fileoverview Tests for ConfigArray class.
* @author Toru Nagashima <https://github.com/mysticatea>
*/
"use strict";
const path = require("path");
const { assert } = require("chai");
const { ConfigArray, OverrideTester, getUsedExtractedConfigs } = require("../../../../lib/cli-engine/config-array");
describe("ConfigArray", () => {
it("should be a sub class of Array.", () => {
assert(new ConfigArray() instanceof Array);
});
describe("'constructor(...elements)' should adopt the elements as array elements.", () => {
const patterns = [
{ elements: [] },
{ elements: [{ value: 1 }] },
{ elements: [{ value: 2 }, { value: 3 }] },
{ elements: [{ value: 4 }, { value: 5 }, { value: 6 }] }
];
for (const { elements } of patterns) {
describe(`if it gave ${JSON.stringify(elements)} then`, () => {
let configArray;
beforeEach(() => {
configArray = new ConfigArray(...elements);
});
it(`should have ${elements.length} as the length.`, () => {
assert.strictEqual(configArray.length, elements.length);
});
for (let i = 0; i < elements.length; ++i) {
it(`should have ${JSON.stringify(elements[i])} at configArray[${i}].`, () => { // eslint-disable-line no-loop-func
assert.strictEqual(configArray[i], elements[i]);
});
}
});
}
});
describe("'isRoot()' method should be the value of the last element which has 'root' property.", () => {
const patterns = [
{ elements: [], expected: false },
{ elements: [{}], expected: false },
{ elements: [{}, {}], expected: false },
{ elements: [{ root: false }], expected: false },
{ elements: [{ root: true }], expected: true },
{ elements: [{ root: true }, { root: false }], expected: false },
{ elements: [{ root: false }, { root: true }], expected: true },
{ elements: [{ root: false }, { root: true }, { rules: {} }], expected: true }, // ignore undefined.
{ elements: [{ root: true }, { root: 1 }], expected: true } // ignore non-boolean value
];
for (const { elements, expected } of patterns) {
it(`should be ${expected} if the elements are ${JSON.stringify(elements)}.`, () => {
assert.strictEqual(new ConfigArray(...elements).isRoot(), expected);
});
}
});
describe("'pluginEnvironments' property should be the environments of all plugins.", () => {
const env = {
"aaa/xxx": {},
"bbb/xxx": {}
};
let configArray;
beforeEach(() => {
configArray = new ConfigArray(
{
plugins: {
aaa: {
definition: {
environments: {
xxx: env["aaa/xxx"]
}
}
}
}
},
{
criteria: OverrideTester.create(["*.ts"], [], process.cwd()),
plugins: {
bbb: {
definition: {
environments: {
xxx: env["bbb/xxx"]
}
}
}
}
}
);
});
it("should return null for built-in env", () => {
assert.strictEqual(configArray.pluginEnvironments.get("node"), void 0);
});
it("should return 'aaa/xxx' if it exists.", () => {
assert.strictEqual(configArray.pluginEnvironments.get("aaa/xxx"), env["aaa/xxx"]);
});
it("should return 'bbb/xxx' if it exists.", () => {
assert.strictEqual(configArray.pluginEnvironments.get("bbb/xxx"), env["bbb/xxx"]);
});
it("should throw an error if it tried to mutate.", () => {
assert.throws(() => {
configArray.pluginEnvironments.set("ccc/xxx", {});
});
});
});
describe("'pluginProcessors' property should be the processors of all plugins.", () => {
const processors = {
"aaa/.xxx": {},
"bbb/.xxx": {}
};
let configArray;
beforeEach(() => {
configArray = new ConfigArray(
{
plugins: {
aaa: {
definition: {
processors: {
".xxx": processors["aaa/.xxx"]
}
}
}
}
},
{
criteria: OverrideTester.create(["*.ts"], [], process.cwd()),
plugins: {
bbb: {
definition: {
processors: {
".xxx": processors["bbb/.xxx"]
}
}
}
}
}
);
});
it("should return 'aaa/.xxx' if it exists.", () => {
assert.strictEqual(configArray.pluginProcessors.get("aaa/.xxx"), processors["aaa/.xxx"]);
});
it("should return 'bbb/.xxx' if it exists.", () => {
assert.strictEqual(configArray.pluginProcessors.get("bbb/.xxx"), processors["bbb/.xxx"]);
});
it("should throw an error if it tried to mutate.", () => {
assert.throws(() => {
configArray.pluginProcessors.set("ccc/.xxx", {});
});
});
});
describe("'pluginRules' property should be the rules of all plugins.", () => {
const rules = {
"aaa/xxx": {},
"bbb/xxx": {}
};
let configArray;
beforeEach(() => {
configArray = new ConfigArray(
{
plugins: {
aaa: {
definition: {
rules: {
xxx: rules["aaa/xxx"]
}
}
}
}
},
{
criteria: OverrideTester.create(["*.ts"], [], process.cwd()),
plugins: {
bbb: {
definition: {
rules: {
xxx: rules["bbb/xxx"]
}
}
}
}
}
);
});
it("should return null for built-in rules", () => {
assert.strictEqual(configArray.pluginRules.get("eqeqeq"), void 0);
});
it("should return 'aaa/xxx' if it exists.", () => {
assert.strictEqual(configArray.pluginRules.get("aaa/xxx"), rules["aaa/xxx"]);
});
it("should return 'bbb/xxx' if it exists.", () => {
assert.strictEqual(configArray.pluginRules.get("bbb/xxx"), rules["bbb/xxx"]);
});
it("should throw an error if it tried to mutate.", () => {
assert.throws(() => {
configArray.pluginRules.set("ccc/xxx", {});
});
});
});
describe("'extractConfig(filePath)' method should retrieve the merged config for a given file.", () => {
it("should throw an error if a 'parser' has the loading error.", () => {
assert.throws(() => {
new ConfigArray(
{
parser: { error: new Error("Failed to load a parser.") }
}
).extractConfig(__filename);
}, "Failed to load a parser.");
});
it("should not throw if the errored 'parser' was not used; overwriten", () => {
const parser = { id: "a parser" };
const config = new ConfigArray(
{
parser: { error: new Error("Failed to load a parser.") }
},
{
parser
}
).extractConfig(__filename);
assert.strictEqual(config.parser, parser);
});
it("should not throw if the errored 'parser' was not used; not matched", () => {
const config = new ConfigArray(
{
criteria: OverrideTester.create(["*.ts"], [], process.cwd()),
parser: { error: new Error("Failed to load a parser.") }
}
).extractConfig(__filename);
assert.strictEqual(config.parser, null);
});
it("should throw an error if a 'plugins' value has the loading error.", () => {
assert.throws(() => {
new ConfigArray(
{
plugins: {
foo: { error: new Error("Failed to load a plugin.") }
}
}
).extractConfig(__filename);
}, "Failed to load a plugin.");
});
it("should not throw if the errored 'plugins' value was not used; not matched", () => {
const config = new ConfigArray(
{
criteria: OverrideTester.create(["*.ts"], [], process.cwd()),
plugins: {
foo: { error: new Error("Failed to load a plugin.") }
}
}
).extractConfig(__filename);
assert.deepStrictEqual(config.plugins, {});
});
it("should not merge the elements which were not matched.", () => {
const config = new ConfigArray(
{
rules: {
"no-redeclare": "error"
}
},
{
criteria: OverrideTester.create(["*.js"], [], process.cwd()),
rules: {
"no-undef": "error"
}
},
{
criteria: OverrideTester.create(["*.js"], [path.basename(__filename)], process.cwd()),
rules: {
"no-use-before-define": "error"
}
},
{
criteria: OverrideTester.create(["*.ts"], [], process.cwd()),
rules: {
"no-unused-vars": "error"
}
}
).extractConfig(__filename);
assert.deepStrictEqual(config.rules, {
"no-redeclare": ["error"],
"no-undef": ["error"]
});
});
it("should return the same instance for every the same matching.", () => {
const configArray = new ConfigArray(
{
rules: {
"no-redeclare": "error"
}
},
{
criteria: OverrideTester.create(["*.js"], [], process.cwd()),
rules: {
"no-undef": "error"
}
},
{
criteria: OverrideTester.create(["*.js"], [path.basename(__filename)], process.cwd()),
rules: {
"no-use-before-define": "error"
}
},
{
criteria: OverrideTester.create(["*.ts"], [], process.cwd()),
rules: {
"no-unused-vars": "error"
}
}
);
assert.strictEqual(
configArray.extractConfig(path.join(__dirname, "a.js")),
configArray.extractConfig(path.join(__dirname, "b.js"))
);
});
/**
* Merge two config data.
*
* The test cases which depend on this function were moved from
* 'tests/lib/config/config-ops.js' when refactoring to keep the
* cumulated test cases.
*
* Previously, the merging logic of multiple config data had been
* implemented in `ConfigOps.merge()` function. But currently, it's
* implemented in `ConfigArray#extractConfig()` method.
*
* @param {Object} target A config data.
* @param {Object} source Another config data.
* @returns {Object} The merged config data.
*/
function merge(target, source) {
return new ConfigArray(target, source).extractConfig(__filename);
}
it("should combine two objects when passed two objects with different top-level properties", () => {
const config = [
{ env: { browser: true } },
{ globals: { foo: "bar" } }
];
const result = merge(config[0], config[1]);
assert.strictEqual(result.globals.foo, "bar");
assert.isTrue(result.env.browser);
});
it("should combine without blowing up on null values", () => {
const config = [
{ env: { browser: true } },
{ env: { node: null } }
];
const result = merge(config[0], config[1]);
assert.strictEqual(result.env.node, null);
assert.isTrue(result.env.browser);
});
it("should combine two objects with parser when passed two objects with different top-level properties", () => {
const config = [
{ env: { browser: true }, parser: "espree" },
{ globals: { foo: "bar" } }
];
const result = merge(config[0], config[1]);
assert.strictEqual(result.parser, "espree");
});
it("should combine configs and override rules when passed configs with the same rules", () => {
const config = [
{ rules: { "no-mixed-requires": [0, false] } },
{ rules: { "no-mixed-requires": [1, true] } }
];
const result = merge(config[0], config[1]);
assert.isArray(result.rules["no-mixed-requires"]);
assert.strictEqual(result.rules["no-mixed-requires"][0], 1);
assert.strictEqual(result.rules["no-mixed-requires"][1], true);
});
it("should combine configs when passed configs with parserOptions", () => {
const config = [
{ parserOptions: { ecmaFeatures: { jsx: true } } },
{ parserOptions: { ecmaFeatures: { globalReturn: true } } }
];
const result = merge(config[0], config[1]);
assert.deepStrictEqual(result, {
configNameOfNoInlineConfig: "",
env: {},
globals: {},
noInlineConfig: void 0,
parser: null,
parserOptions: {
ecmaFeatures: {
jsx: true,
globalReturn: true
}
},
plugins: {},
processor: null,
rules: {},
settings: {}
});
// double-check that originals were not changed
assert.deepStrictEqual(config[0], { parserOptions: { ecmaFeatures: { jsx: true } } });
assert.deepStrictEqual(config[1], { parserOptions: { ecmaFeatures: { globalReturn: true } } });
});
it("should override configs when passed configs with the same ecmaFeatures", () => {
const config = [
{ parserOptions: { ecmaFeatures: { globalReturn: false } } },
{ parserOptions: { ecmaFeatures: { globalReturn: true } } }
];
const result = merge(config[0], config[1]);
assert.deepStrictEqual(result, {
configNameOfNoInlineConfig: "",
env: {},
globals: {},
noInlineConfig: void 0,
parser: null,
parserOptions: {
ecmaFeatures: {
globalReturn: true
}
},
plugins: {},
processor: null,
rules: {},
settings: {}
});
});
it("should combine configs and override rules when merging two configs with arrays and int", () => {
const config = [
{ rules: { "no-mixed-requires": [0, false] } },
{ rules: { "no-mixed-requires": 1 } }
];
const result = merge(config[0], config[1]);
assert.isArray(result.rules["no-mixed-requires"]);
assert.strictEqual(result.rules["no-mixed-requires"][0], 1);
assert.strictEqual(result.rules["no-mixed-requires"][1], false);
assert.deepStrictEqual(config[0], { rules: { "no-mixed-requires": [0, false] } });
assert.deepStrictEqual(config[1], { rules: { "no-mixed-requires": 1 } });
});
it("should combine configs and override rules options completely", () => {
const config = [
{ rules: { "no-mixed-requires1": [1, { event: ["evt", "e"] }] } },
{ rules: { "no-mixed-requires1": [1, { err: ["error", "e"] }] } }
];
const result = merge(config[0], config[1]);
assert.isArray(result.rules["no-mixed-requires1"]);
assert.deepStrictEqual(result.rules["no-mixed-requires1"][1], { err: ["error", "e"] });
assert.deepStrictEqual(config[0], { rules: { "no-mixed-requires1": [1, { event: ["evt", "e"] }] } });
assert.deepStrictEqual(config[1], { rules: { "no-mixed-requires1": [1, { err: ["error", "e"] }] } });
});
it("should combine configs and override rules options without array or object", () => {
const config = [
{ rules: { "no-mixed-requires1": ["warn", "nconf", "underscore"] } },
{ rules: { "no-mixed-requires1": [2, "requirejs"] } }
];
const result = merge(config[0], config[1]);
assert.strictEqual(result.rules["no-mixed-requires1"][0], 2);
assert.strictEqual(result.rules["no-mixed-requires1"][1], "requirejs");
assert.isUndefined(result.rules["no-mixed-requires1"][2]);
assert.deepStrictEqual(config[0], { rules: { "no-mixed-requires1": ["warn", "nconf", "underscore"] } });
assert.deepStrictEqual(config[1], { rules: { "no-mixed-requires1": [2, "requirejs"] } });
});
it("should combine configs and override rules options without array or object but special case", () => {
const config = [
{ rules: { "no-mixed-requires1": [1, "nconf", "underscore"] } },
{ rules: { "no-mixed-requires1": "error" } }
];
const result = merge(config[0], config[1]);
assert.strictEqual(result.rules["no-mixed-requires1"][0], "error");
assert.strictEqual(result.rules["no-mixed-requires1"][1], "nconf");
assert.strictEqual(result.rules["no-mixed-requires1"][2], "underscore");
assert.deepStrictEqual(config[0], { rules: { "no-mixed-requires1": [1, "nconf", "underscore"] } });
assert.deepStrictEqual(config[1], { rules: { "no-mixed-requires1": "error" } });
});
it("should combine configs correctly", () => {
const config = [
{
rules: {
"no-mixed-requires1": [1, { event: ["evt", "e"] }],
"valid-jsdoc": 1,
semi: 1,
quotes1: [2, { exception: ["hi"] }],
smile: [1, ["hi", "bye"]]
},
parserOptions: {
ecmaFeatures: { jsx: true }
},
env: { browser: true },
globals: { foo: false }
},
{
rules: {
"no-mixed-requires1": [1, { err: ["error", "e"] }],
"valid-jsdoc": 2,
test: 1,
smile: [1, ["xxx", "yyy"]]
},
parserOptions: {
ecmaFeatures: { globalReturn: true }
},
env: { browser: false },
globals: { foo: true }
}
];
const result = merge(config[0], config[1]);
assert.deepStrictEqual(result, {
configNameOfNoInlineConfig: "",
parser: null,
parserOptions: {
ecmaFeatures: {
jsx: true,
globalReturn: true
}
},
plugins: {},
env: {
browser: false
},
globals: {
foo: true
},
rules: {
"no-mixed-requires1": [1,
{
err: [
"error",
"e"
]
}
],
quotes1: [2,
{
exception: [
"hi"
]
}
],
semi: [1],
smile: [1, ["xxx", "yyy"]],
test: [1],
"valid-jsdoc": [2]
},
settings: {},
processor: null,
noInlineConfig: void 0
});
assert.deepStrictEqual(config[0], {
rules: {
"no-mixed-requires1": [1, { event: ["evt", "e"] }],
"valid-jsdoc": 1,
semi: 1,
quotes1: [2, { exception: ["hi"] }],
smile: [1, ["hi", "bye"]]
},
parserOptions: {
ecmaFeatures: { jsx: true }
},
env: { browser: true },
globals: { foo: false }
});
assert.deepStrictEqual(config[1], {
rules: {
"no-mixed-requires1": [1, { err: ["error", "e"] }],
"valid-jsdoc": 2,
test: 1,
smile: [1, ["xxx", "yyy"]]
},
parserOptions: {
ecmaFeatures: { globalReturn: true }
},
env: { browser: false },
globals: { foo: true }
});
});
it("should copy deeply if there is not the destination's property", () => {
const a = {};
const b = { settings: { bar: 1 } };
const result = merge(a, b);
assert(a.settings === void 0);
assert(b.settings.bar === 1);
assert(result.settings.bar === 1);
result.settings.bar = 2;
assert(b.settings.bar === 1);
assert(result.settings.bar === 2);
});
});
describe("'getUsedExtractedConfigs(instance)' function should retrieve used extracted configs from the instance's internal cache.", () => {
let configArray;
beforeEach(() => {
configArray = new ConfigArray(
{
rules: {
"no-redeclare": "error"
}
},
{
criteria: OverrideTester.create(["*.js"], [], process.cwd()),
rules: {
"no-undef": "error"
}
},
{
criteria: OverrideTester.create(["*.js"], [path.basename(__filename)], process.cwd()),
rules: {
"no-use-before-define": "error"
}
},
{
criteria: OverrideTester.create(["*.ts"], [], process.cwd()),
rules: {
"no-unused-vars": "error"
}
}
);
});
it("should return empty array before it called 'extractConfig(filePath)'.", () => {
assert.deepStrictEqual(getUsedExtractedConfigs(configArray), []);
});
for (const { filePaths } of [
{ filePaths: [__filename] },
{ filePaths: [__filename, `${__filename}.ts`] },
{ filePaths: [__filename, `${__filename}.ts`, path.join(__dirname, "foo.js")] }
]) {
describe(`after it called 'extractConfig(filePath)' ${filePaths.length} time(s) with ${JSON.stringify(filePaths, null, 4)}, the returned array`, () => { // eslint-disable-line no-loop-func
let configs;
let usedConfigs;
beforeEach(() => {
configs = filePaths.map(filePath => configArray.extractConfig(filePath));
usedConfigs = getUsedExtractedConfigs(configArray);
});
it(`should have ${filePaths.length} as the length.`, () => {
assert.strictEqual(usedConfigs.length, configs.length);
});
for (let i = 0; i < filePaths.length; ++i) {
it(`should contain 'configs[${i}]'.`, () => { // eslint-disable-line no-loop-func
assert(usedConfigs.includes(configs[i]));
});
}
});
}
it("should not contain duplicate values.", () => {
// Call some times, including with the same arguments.
configArray.extractConfig(__filename);
configArray.extractConfig(`${__filename}.ts`);
configArray.extractConfig(path.join(__dirname, "foo.js"));
configArray.extractConfig(__filename);
configArray.extractConfig(path.join(__dirname, "foo.js"));
configArray.extractConfig(path.join(__dirname, "bar.js"));
configArray.extractConfig(path.join(__dirname, "baz.js"));
const usedConfigs = getUsedExtractedConfigs(configArray);
assert.strictEqual(new Set(usedConfigs).size, usedConfigs.length);
});
});
});