/
cascading-config-array-factory.js
1613 lines (1379 loc) · 70 KB
/
cascading-config-array-factory.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
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* STOP!!! DO NOT MODIFY.
*
* This file is part of the ongoing work to move the eslintrc-style config
* system into the @eslint/eslintrc package. This file needs to remain
* unchanged in order for this work to proceed.
*
* If you think you need to change this file, please contact @nzakas first.
*
* Thanks in advance for your cooperation.
*/
/**
* @fileoverview Tests for CascadingConfigArrayFactory class.
* @author Toru Nagashima <https://github.com/mysticatea>
*/
"use strict";
const fs = require("fs");
const path = require("path");
const os = require("os");
const { assert } = require("chai");
const sh = require("shelljs");
const sinon = require("sinon");
const { ConfigArrayFactory } = require("../../../lib/cli-engine/config-array-factory");
const { ExtractedConfig } = require("../../../lib/cli-engine/config-array/extracted-config");
const { defineCascadingConfigArrayFactoryWithInMemoryFileSystem } = require("../../_utils");
/** @typedef {InstanceType<ReturnType<defineCascadingConfigArrayFactoryWithInMemoryFileSystem>["CascadingConfigArrayFactory"]>} CascadingConfigArrayFactory */
/** @typedef {ReturnType<CascadingConfigArrayFactory["getConfigArrayForFile"]>} ConfigArray */
const cwdIgnorePatterns = new ConfigArrayFactory()
.loadDefaultESLintIgnore()[0]
.ignorePattern
.patterns;
describe("CascadingConfigArrayFactory", () => {
describe("'getConfigArrayForFile(filePath)' method should retrieve the proper configuration.", () => {
describe("with three directories ('lib', 'lib/nested', 'test') that contains 'one.js' and 'two.js'", () => {
const root = path.join(os.tmpdir(), "eslint/cli-engine/cascading-config-array-factory");
const files = {
/* eslint-disable quote-props */
"lib": {
"nested": {
"one.js": "",
"two.js": "",
"parser.js": "",
".eslintrc.yml": "parser: './parser'"
},
"one.js": "",
"two.js": ""
},
"test": {
"one.js": "",
"two.js": "",
".eslintrc.yml": "env: { mocha: true }"
},
".eslintignore": "/lib/nested/parser.js",
".eslintrc.json": JSON.stringify({
rules: {
"no-undef": "error",
"no-unused-vars": "error"
}
})
/* eslint-enable quote-props */
};
const { CascadingConfigArrayFactory } = defineCascadingConfigArrayFactoryWithInMemoryFileSystem({ cwd: () => root, files });
/** @type {CascadingConfigArrayFactory} */
let factory;
beforeEach(() => {
factory = new CascadingConfigArrayFactory();
});
it("should retrieve the config '.eslintrc.json' if 'lib/one.js' was given.", () => {
const config = factory.getConfigArrayForFile("lib/one.js");
assert.strictEqual(config.length, 3);
assert.strictEqual(config[0].name, "DefaultIgnorePattern");
assert.strictEqual(config[1].filePath, path.join(root, ".eslintrc.json"));
assert.strictEqual(config[2].filePath, path.join(root, ".eslintignore"));
});
it("should retrieve the merged config of '.eslintrc.json' and 'lib/nested/.eslintrc.yml' if 'lib/nested/one.js' was given.", () => {
const config = factory.getConfigArrayForFile("lib/nested/one.js");
assert.strictEqual(config.length, 4);
assert.strictEqual(config[0].name, "DefaultIgnorePattern");
assert.strictEqual(config[1].filePath, path.join(root, ".eslintrc.json"));
assert.strictEqual(config[2].filePath, path.join(root, "lib/nested/.eslintrc.yml"));
assert.strictEqual(config[3].filePath, path.join(root, ".eslintignore"));
});
it("should retrieve the config '.eslintrc.json' if 'lib/non-exist.js' was given.", () => {
const config = factory.getConfigArrayForFile("lib/non-exist.js");
assert.strictEqual(config.length, 3);
assert.strictEqual(config[0].name, "DefaultIgnorePattern");
assert.strictEqual(config[1].filePath, path.join(root, ".eslintrc.json"));
assert.strictEqual(config[2].filePath, path.join(root, ".eslintignore"));
});
});
describe("deprecation warnings", () => {
let uid = 0;
let uniqueHomeDirName = "";
let homeDir = "";
let cwd = "";
/** @type {{code:string, message:string}[]} */
let warnings = [];
/** @type {CascadingConfigArrayFactory} */
let factory = null;
/** @type {ConfigArray} */
let config = null;
/**
* Store a reported warning object if that code starts with `ESLINT_`.
* @param {{code:string, message:string}} w The warning object to store.
* @returns {void}
*/
function onWarning(w) {
if (w.code.startsWith("ESLINT_")) {
warnings.push({ code: w.code, message: w.message });
}
}
/**
* Delay to wait for 'warning' events.
* @returns {Promise<void>} The promise that will be fulfilled after wait a timer.
*/
function delay() {
return new Promise(resolve => setTimeout(resolve, 0));
}
beforeEach(() => {
uniqueHomeDirName = `home_${++uid}`;
homeDir = path.join(__dirname, `../../../${uniqueHomeDirName}`);
warnings = [];
sinon.stub(os, "homedir").returns(homeDir);
process.on("warning", onWarning);
});
afterEach(() => {
os.homedir.restore();
process.removeListener("warning", onWarning);
});
describe("when '~/.eslintrc.json' exists and CWD is `~/`", () => {
beforeEach(() => {
cwd = homeDir;
const { CascadingConfigArrayFactory } = defineCascadingConfigArrayFactoryWithInMemoryFileSystem({
cwd: () => cwd,
files: {
// ~/.eslintrc.json
".eslintrc.json": JSON.stringify({ rules: { eqeqeq: "error" } }),
// other files
"exist-with-root/test.js": "",
"exist-with-root/.eslintrc.json": JSON.stringify({ root: true, rules: { yoda: "error" } }),
"exist/test.js": "",
"exist/.eslintrc.json": JSON.stringify({ rules: { yoda: "error" } }),
"not-exist/test.js": ""
}
});
factory = new CascadingConfigArrayFactory({ cwd });
});
// no warning.
describe("when it lints 'subdir/exist-with-root/test.js'", () => {
beforeEach(async () => {
config = factory.getConfigArrayForFile("exist-with-root/test.js");
await delay();
});
it("should not raise any warnings.", () => {
assert.deepStrictEqual(warnings, []);
});
it("should not load '~/.eslintrc.json'.", () => {
assert.deepStrictEqual(
config.extractConfig("a.js").rules,
{ yoda: ["error"] }
);
});
});
// no warning.
describe("when it lints 'subdir/exist/test.js'", () => {
beforeEach(async () => {
config = factory.getConfigArrayForFile("exist/test.js");
await delay();
});
it("should not raise any warnings.", () => {
assert.deepStrictEqual(warnings, []);
});
it("should load '~/.eslintrc.json'.", () => {
assert.deepStrictEqual(
config.extractConfig("a.js").rules,
{ eqeqeq: ["error"], yoda: ["error"] }
);
});
});
// no warning
describe("when it lints 'subdir/not-exist/test.js'", () => {
beforeEach(async () => {
config = factory.getConfigArrayForFile("not-exist/test.js");
await delay();
});
it("should not raise any warnings.", () => {
assert.deepStrictEqual(warnings, []);
});
it("should load '~/.eslintrc.json'.", () => {
assert.deepStrictEqual(
config.extractConfig("a.js").rules,
{ eqeqeq: ["error"] }
);
});
});
});
describe("when '~/.eslintrc.json' exists and CWD is `~/subdir`", () => {
beforeEach(() => {
cwd = path.join(homeDir, "subdir");
const { CascadingConfigArrayFactory } = defineCascadingConfigArrayFactoryWithInMemoryFileSystem({
cwd: () => cwd,
files: {
// ~/.eslintrc.json
"../.eslintrc.json": JSON.stringify({ rules: { eqeqeq: "error" } }),
// other files
"exist-with-root/test.js": "",
"exist-with-root/.eslintrc.json": JSON.stringify({ root: true, rules: { yoda: "error" } }),
"exist/test.js": "",
"exist/.eslintrc.json": JSON.stringify({ rules: { yoda: "error" } }),
"not-exist/test.js": ""
}
});
factory = new CascadingConfigArrayFactory({ cwd });
});
// Project's config file has `root:true`, then no warning.
describe("when it lints 'subdir/exist-with-root/test.js'", () => {
beforeEach(async () => {
config = factory.getConfigArrayForFile("exist-with-root/test.js");
await delay();
});
it("should not raise any warnings.", () => {
assert.deepStrictEqual(warnings, []);
});
it("should not load '~/.eslintrc.json'.", () => {
assert.deepStrictEqual(
config.extractConfig("a.js").rules,
{ yoda: ["error"] }
);
});
});
// Project's config file doesn't have `root:true` and home is ancestor, then ESLINT_PERSONAL_CONFIG_SUPPRESS.
describe("when it lints 'subdir/exist/test.js'", () => {
beforeEach(async () => {
config = factory.getConfigArrayForFile("exist/test.js");
await delay();
});
it("should raise an ESLINT_PERSONAL_CONFIG_SUPPRESS warning.", () => {
assert.deepStrictEqual(warnings, [
{
code: "ESLINT_PERSONAL_CONFIG_SUPPRESS",
message: `'~/.eslintrc.*' config files have been deprecated. Please remove it or add 'root:true' to the config files in your projects in order to avoid loading '~/.eslintrc.*' accidentally. (found in "${uniqueHomeDirName}${path.sep}.eslintrc.json")`
}
]);
});
it("should not load '~/.eslintrc.json'.", () => {
assert.deepStrictEqual(
config.extractConfig("a.js").rules,
{ yoda: ["error"] }
);
});
});
/*
* Project's config file doesn't exist and home is ancestor, then no warning.
* In this case, ESLint will continue to use `~/.eslintrc.json` even if personal config file feature is removed.
*/
describe("when it lints 'subdir/not-exist/test.js'", () => {
beforeEach(async () => {
config = factory.getConfigArrayForFile("not-exist/test.js");
await delay();
});
it("should not raise any warnings.", () => {
assert.deepStrictEqual(warnings, []);
});
it("should load '~/.eslintrc.json'.", () => {
assert.deepStrictEqual(
config.extractConfig("a.js").rules,
{ eqeqeq: ["error"] }
);
});
});
});
describe("when '~/.eslintrc.json' exists and CWD is `~/../another`", () => {
beforeEach(() => {
cwd = path.join(homeDir, "../another");
const { CascadingConfigArrayFactory } = defineCascadingConfigArrayFactoryWithInMemoryFileSystem({
cwd: () => cwd,
files: {
// ~/.eslintrc.json
[`../${uniqueHomeDirName}/.eslintrc.json`]: JSON.stringify({ rules: { eqeqeq: "error" } }),
// other files
"exist-with-root/test.js": "",
"exist-with-root/.eslintrc.json": JSON.stringify({ root: true, rules: { yoda: "error" } }),
"exist/test.js": "",
"exist/.eslintrc.json": JSON.stringify({ rules: { yoda: "error" } }),
"not-exist/test.js": ""
}
});
factory = new CascadingConfigArrayFactory({ cwd });
});
// Project's config file has `root:true`, then no warning.
describe("when it lints 'exist-with-root/test.js'", () => {
beforeEach(async () => {
config = factory.getConfigArrayForFile("exist-with-root/test.js");
await delay();
});
it("should not raise any warnings.", () => {
assert.deepStrictEqual(warnings, []);
});
it("should not load '~/.eslintrc.json'.", () => {
assert.deepStrictEqual(
config.extractConfig("a.js").rules,
{ yoda: ["error"] }
);
});
});
// Project's config file doesn't have `root:true` but home is not ancestor, then no warning.
describe("when it lints 'exist/test.js'", () => {
beforeEach(async () => {
config = factory.getConfigArrayForFile("exist/test.js");
await delay();
});
it("should not raise any warnings.", () => {
assert.deepStrictEqual(warnings, []);
});
it("should not load '~/.eslintrc.json'.", () => {
assert.deepStrictEqual(
config.extractConfig("a.js").rules,
{ yoda: ["error"] }
);
});
});
// Project's config file doesn't exist and home is not ancestor, then ESLINT_PERSONAL_CONFIG_LOAD.
describe("when it lints 'not-exist/test.js'", () => {
beforeEach(async () => {
config = factory.getConfigArrayForFile("not-exist/test.js");
await delay();
});
it("should raise an ESLINT_PERSONAL_CONFIG_LOAD warning.", () => {
assert.deepStrictEqual(warnings, [
{
code: "ESLINT_PERSONAL_CONFIG_LOAD",
message: `'~/.eslintrc.*' config files have been deprecated. Please use a config file per project or the '--config' option. (found in "${uniqueHomeDirName}${path.sep}.eslintrc.json")`
}
]);
});
it("should load '~/.eslintrc.json'.", () => {
assert.deepStrictEqual(
config.extractConfig("a.js").rules,
{ eqeqeq: ["error"] }
);
});
});
});
describe("when '~/.eslintrc.json' doesn't exist and CWD is `~/subdir`", () => {
beforeEach(() => {
cwd = path.join(homeDir, "subdir");
const { CascadingConfigArrayFactory } = defineCascadingConfigArrayFactoryWithInMemoryFileSystem({
cwd: () => cwd,
files: {
"exist-with-root/test.js": "",
"exist-with-root/.eslintrc.json": JSON.stringify({ root: true, rules: { yoda: "error" } }),
"exist/test.js": "",
"exist/.eslintrc.json": JSON.stringify({ rules: { yoda: "error" } }),
"not-exist/test.js": ""
}
});
factory = new CascadingConfigArrayFactory({ cwd });
});
describe("when it lints 'subdir/exist/test.js'", () => {
beforeEach(async () => {
config = factory.getConfigArrayForFile("exist/test.js");
await delay();
});
it("should not raise any warnings.", () => {
assert.deepStrictEqual(warnings, []);
});
});
});
describe("when '~/.eslintrc.json' doesn't exist and CWD is `~/../another`", () => {
beforeEach(() => {
cwd = path.join(homeDir, "../another");
const { CascadingConfigArrayFactory } = defineCascadingConfigArrayFactoryWithInMemoryFileSystem({
cwd: () => cwd,
files: {
"exist-with-root/test.js": "",
"exist-with-root/.eslintrc.json": JSON.stringify({ root: true, rules: { yoda: "error" } }),
"exist/test.js": "",
"exist/.eslintrc.json": JSON.stringify({ rules: { yoda: "error" } }),
"not-exist/test.js": ""
}
});
factory = new CascadingConfigArrayFactory({ cwd });
});
describe("when it lints 'not-exist/test.js'", () => {
beforeEach(async () => {
config = factory.getConfigArrayForFile("not-exist/test.js", { ignoreNotFoundError: true });
await delay();
});
it("should not raise any warnings.", () => {
assert.deepStrictEqual(warnings, []);
});
});
});
});
// This group moved from 'tests/lib/config.js' when refactoring to keep the cumulated test cases.
describe("with 'tests/fixtures/config-hierarchy' files", () => {
const { CascadingConfigArrayFactory } = require("../../../lib/cli-engine/cascading-config-array-factory");
let fixtureDir;
const DIRECTORY_CONFIG_HIERARCHY = require("../../fixtures/config-hierarchy/file-structure.json");
/**
* Returns the path inside of the fixture directory.
* @param {...string} args file path segments.
* @returns {string} The path inside the fixture directory.
* @private
*/
function getFixturePath(...args) {
return path.join(fixtureDir, "config-hierarchy", ...args);
}
/**
* Mocks the current user's home path
* @param {string} fakeUserHomePath fake user's home path
* @returns {void}
* @private
*/
function mockOsHomedir(fakeUserHomePath) {
sinon.stub(os, "homedir")
.returns(fakeUserHomePath);
}
/**
* Assert that given two objects have the same properties with the
* same value for each.
*
* The `expected` object is merged with the default values of config
* data before comparing, so you can specify only the properties you
* focus on.
* @param {Object} actual The config object to check.
* @param {Object} expected What the config object should look like.
* @returns {void}
* @private
*/
function assertConfigsEqual(actual, expected) {
const defaults = new ExtractedConfig().toCompatibleObjectAsConfigFileContent();
assert.deepStrictEqual(actual, { ...defaults, ...expected });
}
/**
* Wait for the next tick.
* @returns {Promise<void>} -
*/
function nextTick() {
return new Promise(resolve => process.nextTick(resolve));
}
/**
* Get the config data for a file.
* @param {CascadingConfigArrayFactory} factory The factory to get config.
* @param {string} filePath The path to a source code.
* @returns {Object} The gotten config.
*/
function getConfig(factory, filePath = "a.js") {
const { cwd } = factory;
const absolutePath = path.resolve(cwd, filePath);
return factory
.getConfigArrayForFile(absolutePath)
.extractConfig(absolutePath)
.toCompatibleObjectAsConfigFileContent();
}
// copy into clean area so as not to get "infected" by this project's .eslintrc files
before(() => {
fixtureDir = `${os.tmpdir()}/eslint/fixtures`;
sh.mkdir("-p", fixtureDir);
sh.cp("-r", "./tests/fixtures/config-hierarchy", fixtureDir);
sh.cp("-r", "./tests/fixtures/rules", fixtureDir);
});
afterEach(() => {
sinon.verifyAndRestore();
});
after(() => {
sh.rm("-r", fixtureDir);
});
it("should create config object when using baseConfig with extends", () => {
const customBaseConfig = {
extends: path.resolve(__dirname, "../../fixtures/config-extends/array/.eslintrc")
};
const factory = new CascadingConfigArrayFactory({ baseConfig: customBaseConfig, useEslintrc: false });
const config = getConfig(factory);
assert.deepStrictEqual(config.env, {
browser: false,
es6: true,
node: true
});
assert.deepStrictEqual(config.rules, {
"no-empty": [1],
"comma-dangle": [2],
"no-console": [2]
});
});
it("should return the project config when called in current working directory", () => {
const factory = new CascadingConfigArrayFactory();
const actual = getConfig(factory);
assert.strictEqual(actual.rules.strict[1], "global");
});
it("should not retain configs from previous directories when called multiple times", () => {
const firstpath = path.resolve(__dirname, "../../fixtures/configurations/single-quotes/subdir/.eslintrc");
const secondpath = path.resolve(__dirname, "../../fixtures/configurations/single-quotes/.eslintrc");
const factory = new CascadingConfigArrayFactory();
let config;
config = getConfig(factory, firstpath);
assert.deepStrictEqual(config.rules["no-new"], [0]);
config = getConfig(factory, secondpath);
assert.deepStrictEqual(config.rules["no-new"], [1]);
});
it("should throw error when a configuration file doesn't exist", () => {
const configPath = path.resolve(__dirname, "../../fixtures/configurations/.eslintrc");
const factory = new CascadingConfigArrayFactory();
sinon.stub(fs, "readFileSync").throws(new Error());
assert.throws(() => {
getConfig(factory, configPath);
}, "Cannot read config file");
});
it("should throw error when a configuration file is not require-able", () => {
const configPath = ".eslintrc";
const factory = new CascadingConfigArrayFactory();
sinon.stub(fs, "readFileSync").throws(new Error());
assert.throws(() => {
getConfig(factory, configPath);
}, "Cannot read config file");
});
it("should cache config when the same directory is passed twice", () => {
const configPath = path.resolve(__dirname, "../../fixtures/configurations/single-quotes/.eslintrc");
const configArrayFactory = new ConfigArrayFactory();
const factory = new CascadingConfigArrayFactory({ configArrayFactory });
sinon.spy(configArrayFactory, "loadInDirectory");
// If cached this should be called only once
getConfig(factory, configPath);
const callcount = configArrayFactory.loadInDirectory.callcount;
getConfig(factory, configPath);
assert.strictEqual(configArrayFactory.loadInDirectory.callcount, callcount);
});
// make sure JS-style comments don't throw an error
it("should load the config file when there are JS-style comments in the text", () => {
const specificConfigPath = path.resolve(__dirname, "../../fixtures/configurations/comments.json");
const factory = new CascadingConfigArrayFactory({ specificConfigPath, useEslintrc: false });
const config = getConfig(factory);
const { semi, strict } = config.rules;
assert.deepStrictEqual(semi, [1]);
assert.deepStrictEqual(strict, [0]);
});
// make sure YAML files work correctly
it("should load the config file when a YAML file is used", () => {
const specificConfigPath = path.resolve(__dirname, "../../fixtures/configurations/env-browser.yaml");
const factory = new CascadingConfigArrayFactory({ specificConfigPath, useEslintrc: false });
const config = getConfig(factory);
const { "no-alert": noAlert, "no-undef": noUndef } = config.rules;
assert.deepStrictEqual(noAlert, [0]);
assert.deepStrictEqual(noUndef, [2]);
});
it("should contain the correct value for parser when a custom parser is specified", () => {
const configPath = path.resolve(__dirname, "../../fixtures/configurations/parser/.eslintrc.json");
const factory = new CascadingConfigArrayFactory();
const config = getConfig(factory, configPath);
assert.strictEqual(config.parser, path.resolve(path.dirname(configPath), "./custom.js"));
});
/*
* Configuration hierarchy ---------------------------------------------
* https://github.com/eslint/eslint/issues/3915
*/
it("should correctly merge environment settings", () => {
const factory = new CascadingConfigArrayFactory({ useEslintrc: true });
const file = getFixturePath("envs", "sub", "foo.js");
const expected = {
rules: {},
env: {
browser: true,
node: false
},
ignorePatterns: cwdIgnorePatterns
};
const actual = getConfig(factory, file);
assertConfigsEqual(actual, expected);
});
// Default configuration - blank
it("should return a blank config when using no .eslintrc", () => {
const factory = new CascadingConfigArrayFactory({ useEslintrc: false });
const file = getFixturePath("broken", "console-wrong-quotes.js");
const expected = {
rules: {},
globals: {},
env: {},
ignorePatterns: cwdIgnorePatterns
};
const actual = getConfig(factory, file);
assertConfigsEqual(actual, expected);
});
it("should return a blank config when baseConfig is set to false and no .eslintrc", () => {
const factory = new CascadingConfigArrayFactory({ baseConfig: false, useEslintrc: false });
const file = getFixturePath("broken", "console-wrong-quotes.js");
const expected = {
rules: {},
globals: {},
env: {},
ignorePatterns: cwdIgnorePatterns
};
const actual = getConfig(factory, file);
assertConfigsEqual(actual, expected);
});
// No default configuration
it("should return an empty config when not using .eslintrc", () => {
const factory = new CascadingConfigArrayFactory({ useEslintrc: false });
const file = getFixturePath("broken", "console-wrong-quotes.js");
const actual = getConfig(factory, file);
assertConfigsEqual(actual, { ignorePatterns: cwdIgnorePatterns });
});
it("should return a modified config when baseConfig is set to an object and no .eslintrc", () => {
const factory = new CascadingConfigArrayFactory({
baseConfig: {
env: {
node: true
},
rules: {
quotes: [2, "single"]
}
},
useEslintrc: false
});
const file = getFixturePath("broken", "console-wrong-quotes.js");
const expected = {
env: {
node: true
},
rules: {
quotes: [2, "single"]
},
ignorePatterns: cwdIgnorePatterns
};
const actual = getConfig(factory, file);
assertConfigsEqual(actual, expected);
});
it("should return a modified config without plugin rules enabled when baseConfig is set to an object with plugin and no .eslintrc", () => {
const factory = new CascadingConfigArrayFactory({
baseConfig: {
env: {
node: true
},
rules: {
quotes: [2, "single"]
},
plugins: ["example-with-rules-config"]
},
cwd: getFixturePath("plugins"),
useEslintrc: false
});
const file = getFixturePath("broken", "plugins", "console-wrong-quotes.js");
const expected = {
env: {
node: true
},
plugins: ["example-with-rules-config"],
rules: {
quotes: [2, "single"]
}
};
const actual = getConfig(factory, file);
assertConfigsEqual(actual, expected);
});
// Project configuration - second level .eslintrc
it("should merge configs when local .eslintrc overrides parent .eslintrc", () => {
const factory = new CascadingConfigArrayFactory();
const file = getFixturePath("broken", "subbroken", "console-wrong-quotes.js");
const expected = {
env: {
node: true
},
rules: {
"no-console": [1],
quotes: [2, "single"]
},
ignorePatterns: cwdIgnorePatterns
};
const actual = getConfig(factory, file);
assertConfigsEqual(actual, expected);
});
// Project configuration - third level .eslintrc
it("should merge configs when local .eslintrc overrides parent and grandparent .eslintrc", () => {
const factory = new CascadingConfigArrayFactory();
const file = getFixturePath("broken", "subbroken", "subsubbroken", "console-wrong-quotes.js");
const expected = {
env: {
node: true
},
rules: {
"no-console": [0],
quotes: [1, "double"]
},
ignorePatterns: cwdIgnorePatterns
};
const actual = getConfig(factory, file);
assertConfigsEqual(actual, expected);
});
// Project configuration - root set in second level .eslintrc
it("should not return or traverse configurations in parents of config with root:true", () => {
const factory = new CascadingConfigArrayFactory();
const file = getFixturePath("root-true", "parent", "root", "wrong-semi.js");
const expected = {
rules: {
semi: [2, "never"]
},
ignorePatterns: cwdIgnorePatterns
};
const actual = getConfig(factory, file);
assertConfigsEqual(actual, expected);
});
// Project configuration - root set in second level .eslintrc
it("should return project config when called with a relative path from a subdir", () => {
const factory = new CascadingConfigArrayFactory({ cwd: getFixturePath("root-true", "parent", "root", "subdir") });
const dir = ".";
const expected = {
rules: {
semi: [2, "never"]
}
};
const actual = getConfig(factory, dir);
assertConfigsEqual(actual, expected);
});
// Command line configuration - --config with first level .eslintrc
it("should merge command line config when config file adds to local .eslintrc", () => {
const factory = new CascadingConfigArrayFactory({
specificConfigPath: getFixturePath("broken", "add-conf.yaml")
});
const file = getFixturePath("broken", "console-wrong-quotes.js");
const expected = {
env: {
node: true
},
rules: {
quotes: [2, "double"],
semi: [1, "never"]
},
ignorePatterns: cwdIgnorePatterns
};
const actual = getConfig(factory, file);
assertConfigsEqual(actual, expected);
});
// Command line configuration - --config with first level .eslintrc
it("should merge command line config when config file overrides local .eslintrc", () => {
const factory = new CascadingConfigArrayFactory({
specificConfigPath: getFixturePath("broken", "override-conf.yaml")
});
const file = getFixturePath("broken", "console-wrong-quotes.js");
const expected = {
env: {
node: true
},
rules: {
quotes: [0, "double"]
},
ignorePatterns: cwdIgnorePatterns
};
const actual = getConfig(factory, file);
assertConfigsEqual(actual, expected);
});
// Command line configuration - --config with second level .eslintrc
it("should merge command line config when config file adds to local and parent .eslintrc", () => {
const factory = new CascadingConfigArrayFactory({
specificConfigPath: getFixturePath("broken", "add-conf.yaml")
});
const file = getFixturePath("broken", "subbroken", "console-wrong-quotes.js");
const expected = {
env: {
node: true
},
rules: {
quotes: [2, "single"],
"no-console": [1],
semi: [1, "never"]
},
ignorePatterns: cwdIgnorePatterns
};
const actual = getConfig(factory, file);
assertConfigsEqual(actual, expected);
});
// Command line configuration - --config with second level .eslintrc
it("should merge command line config when config file overrides local and parent .eslintrc", () => {
const factory = new CascadingConfigArrayFactory({
specificConfigPath: getFixturePath("broken", "override-conf.yaml")
});
const file = getFixturePath("broken", "subbroken", "console-wrong-quotes.js");
const expected = {
env: {
node: true
},
rules: {
quotes: [0, "single"],
"no-console": [1]
},
ignorePatterns: cwdIgnorePatterns
};
const actual = getConfig(factory, file);
assertConfigsEqual(actual, expected);
});
// Command line configuration - --rule with --config and first level .eslintrc
it("should merge command line config and rule when rule and config file overrides local .eslintrc", () => {
const factory = new CascadingConfigArrayFactory({
cliConfig: {
rules: {
quotes: [1, "double"]
}
},
specificConfigPath: getFixturePath("broken", "override-conf.yaml")
});
const file = getFixturePath("broken", "console-wrong-quotes.js");
const expected = {
env: {
node: true
},
rules: {
quotes: [1, "double"]
},
ignorePatterns: cwdIgnorePatterns
};
const actual = getConfig(factory, file);
assertConfigsEqual(actual, expected);
});
// Command line configuration - --plugin
it("should merge command line plugin with local .eslintrc", () => {
const factory = new CascadingConfigArrayFactory({
cliConfig: {
plugins: ["another-plugin"]
},
cwd: getFixturePath("plugins"),
resolvePluginsRelativeTo: getFixturePath("plugins")
});
const file = getFixturePath("broken", "plugins", "console-wrong-quotes.js");
const expected = {
env: {
node: true
},
plugins: [
"example",
"another-plugin"
],
rules: {
quotes: [2, "double"]
}
};
const actual = getConfig(factory, file);
assertConfigsEqual(actual, expected);
});
it("should merge multiple different config file formats", () => {
const factory = new CascadingConfigArrayFactory();
const file = getFixturePath("fileexts/subdir/subsubdir/foo.js");
const expected = {
env: {
browser: true
},
rules: {
semi: [2, "always"],
eqeqeq: [2]
},
ignorePatterns: cwdIgnorePatterns
};
const actual = getConfig(factory, file);
assertConfigsEqual(actual, expected);
});
it("should load user config globals", () => {
const configPath = path.resolve(__dirname, "../../fixtures/globals/conf.yaml");
const factory = new CascadingConfigArrayFactory({ specificConfigPath: configPath, useEslintrc: false });
const expected = {
globals: {
foo: true
},