8000 fix: explicit match in array elements of `files` (#218) · eslint/rewrite@e84cbd7 · GitHub
[go: up one dir, main page]

8000
Skip to content

Commit e84cbd7

Browse files
authored
fix: explicit match in array elements of files (#218)
1 parent b8bf819 commit e84cbd7

File tree

2 files changed

+93
-7
lines changed

2 files changed

+93
-7
lines changed

packages/config-array/src/config-array.js

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,18 +1010,42 @@ export class ConfigArray extends Array {
10101010
* a file with a specific extensions such as *.js.
10111011
*/
10121012

1013-
const universalFiles = config.files.filter(pattern =>
1014-
universalPattern.test(pattern),
1015-
);
1013+
const nonUniversalFiles = [];
1014+
const universalFiles = config.files.filter(element => {
1015+
if (Array.isArray(element)) {
1016+
/*
1017+
* filePath matches an element that is an array only if it matches
1018+
* all patterns in it (AND operation). Therefore, if there is at least
1019+
* one non-universal pattern in the array, and filePath matches the array,
1020+
* then we know for sure that filePath matches at least one non-universal
1021+
* pattern, so we can consider the entire array to be non-universal.
1022+
* In other words, all patterns in the array need to be universal
1023+
* for it to be considered universal.
1024+
*/
1025+
if (
1026+
element.every(pattern => universalPattern.test(pattern))
1027+
) {
1028+
return true;
1029+
}
1030+
1031+
nonUniversalFiles.push(element);
1032+
return false;
1033+
}
1034+
1035+
// element is a string
1036+
1037+
if (universalPattern.test(element)) {
1038+
return true;
1039+
}
1040+
1041+
nonUniversalFiles.push(element);
1042+
return false;
1043+
});
10161044

10171045
// universal patterns were found so we need to check the config twice
10181046
if (universalFiles.length) {
10191047
debug("Universal files patterns found. Checking carefully.");
10201048

1021-
const nonUniversalFiles = config.files.filter(
1022-
pattern => !universalPattern.test(pattern),
1023-
);
1024-
10251049
// check that the config matches without the non-universal files first
10261050
if (
10271051
nonUniversalFiles.length &&

packages/config-array/tests/config-array.test.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,34 @@ describe("ConfigArray", () => {
883883
assert.strictEqual(config2, undefined);
884884
});
885885

886+
it("should match any filename with a config object that has `[]` in files", () => {
887+
configs = new ConfigArray(
888+
[
889+
{
890+
files: [[]],
891+
defs: {
892+
"test-def": "test-value",
893+
},
894+
},
895+
{
896+
files: ["**/*.js"], // `[]` is not an explicit match, so we need to add an explicit match
897+
},
898+
],
899+
{
900+
basePath,
901+
schema,
902+
},
903+
);
904+
905+
configs.normalizeSync();
906+
907+
assert.deepStrictEqual(configs.getConfig("foo/a.js"), {
908+
defs: {
909+
"test-def": "test-value",
910+
},
911+
});
912+
});
913+
886914
it("should calculate correct config when passed JS filename that matches a async function config", () => {
887915
const configsToTest = createConfigArray();
888916
configsToTest.push(context =>
@@ -1833,6 +1861,40 @@ describe("ConfigArray", () => {
18331861
);
18341862
});
18351863

1864+
it('should return "matched" when there is at least one non-universal match, "unconfigured" otherwise', () => {
1865+
[
1866+
[["**/*.js", "foo/**"]],
1867+
[["foo/**", "**/*.js"]],
1868+
[["**/*.js", "foo/**"], "bar/**"],
1869+
[["foo/**", "**/*.js"], "bar/**"],
1870+
[["bar/**"], "foo/*.js"],
1871+
[[], "foo/*.js"],
1872+
].forEach(files => {
1873+
configs = new ConfigArray(
1874+
[
1875+
{
1876+
files,
1877+
},
1878+
],
1879+
{
1880+
basePath,
1881+
},
1882+
);
1883+
1884+
configs.normalizeSync();
1885+
1886+
assert.strictEqual(
1887+
configs.getConfigStatus("foo/a.js"),
1888+
"matched",
1889+
);
1890+
1891+
assert.strictEqual(
1892+
configs.getConfigStatus("bar/a.js"),
1893+
"unconfigured",
1894+
);
1895+
});
1896+
});
1897+
18361898
it('should return "matched" when file has the same name as a directory that is ignored by a pattern that ends with `/`', () => {
18371899
configs = new ConfigArray(
18381900
[

0 commit comments

Comments
 (0)
0