Skip to content

Commit

Permalink
fix: Ensure environments in overrides respect files patterns
Browse files Browse the repository at this point in the history
Fixes #125
  • Loading branch information
nzakas committed Aug 4, 2023
1 parent 6cfa046 commit a0dfd44
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 3 deletions.
10 changes: 8 additions & 2 deletions lib/flat-compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,20 @@ function translateESLintRC(eslintrcConfig, {
if (environments.has(envName)) {

// built-in environments should be defined first
configs.unshift(...translateESLintRC(environments.get(envName), {
configs.unshift(...translateESLintRC({
criteria: eslintrcConfig.criteria,
...environments.get(envName)
}, {
resolveConfigRelativeTo,
resolvePluginsRelativeTo
}));
} else if (pluginEnvironments.has(envName)) {

// if the environment comes from a plugin, it should come after the plugin config
configs.push(...translateESLintRC(pluginEnvironments.get(envName), {
configs.push(...translateESLintRC({
criteria: eslintrcConfig.criteria,
...pluginEnvironments.get(envName)
}, {
resolveConfigRelativeTo,
resolvePluginsRelativeTo
}));
Expand Down
87 changes: 86 additions & 1 deletion tests/lib/flat-compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,92 @@ describe("FlatCompat", () => {
});
});

it("should translate builtin env with files", () => {
const result = compat.config({
rules: {
foo: "error"
},
overrides: [
{
files: "*.jsx",
env: {
amd: true
}
}
]
});

assert.strictEqual(result.length, 3);

assert.deepStrictEqual(result[0], {
rules: {
foo: "error"
}
});

assert.deepStrictEqual(result[1].languageOptions, {
...environments.get("amd")
});
assert.typeOf(result[1].files[0], "function");
assert.isTrue(result[1].files[0]("/usr/eslint/foo.jsx"));
assert.isFalse(result[1].files[0]("/usr/eslint/foo.js"));

assert.typeOf(result[2].files[0], "function");
assert.isTrue(result[2].files[0]("/usr/eslint/foo.jsx"));
assert.isFalse(result[2].files[0]("/usr/eslint/foo.js"));
});

it("should translate plugin env with files", () => {
const result = compat.config({
rules: {
foo: "error"
},
overrides: [
{
files: "*.jsx",
plugins: ["fixture3"],
env: {
"fixture3/a": true,
"fixture3/b": true
}
}
]
});

assert.strictEqual(result.length, 4);

assert.deepStrictEqual(result[0], {
rules: {
foo: "error"
}
});

assert.deepStrictEqual(result[1].languageOptions, {
globals: {
foo: true
}
});
assert.typeOf(result[1].files[0], "function");
assert.isTrue(result[1].files[0]("/usr/eslint/foo.jsx"));
assert.isFalse(result[1].files[0]("/usr/eslint/foo.js"));

assert.deepStrictEqual(result[2].languageOptions, {
globals: {
bar: false
}
});
assert.typeOf(result[2].files[0], "function");
assert.isTrue(result[2].files[0]("/usr/eslint/foo.jsx"));
assert.isFalse(result[2].files[0]("/usr/eslint/foo.js"));

assert.deepStrictEqual(result[3].plugins, {
fixture3: pluginFixture3
});
assert.typeOf(result[3].files[0], "function");
assert.isTrue(result[3].files[0]("/usr/eslint/foo.jsx"));
assert.isFalse(result[3].files[0]("/usr/eslint/foo.js"));
});

});

describe("extends", () => {
Expand Down Expand Up @@ -957,7 +1043,6 @@ describe("FlatCompat", () => {

});


describe("plugins()", () => {

let compat;
Expand Down

0 comments on commit a0dfd44

Please sign in to comment.