Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: Require eslint:all and eslint:recommended as parameters. #103

Merged
merged 3 commits into from Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
Expand Up @@ -22,6 +22,7 @@ The primary class in this package is `FlatCompat`, which is a utility to transla

```js
import { FlatCompat } from "@eslint/eslintrc";
import js from "@eslint/js";
import path from "path";
import { fileURLToPath } from "url";

Expand All @@ -30,8 +31,10 @@ const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const compat = new FlatCompat({
baseDirectory: __dirname, // optional; default: process.cwd()
resolvePluginsRelativeTo: __dirname // optional
baseDirectory: __dirname, // optional; default: process.cwd()
resolvePluginsRelativeTo: __dirname, // optional
recommendedConfig: js.configs.recommended, // optional
allConfig: js.configs.all, // optional
});

export default [
Expand Down
33 changes: 19 additions & 14 deletions lib/flat-compat.js
Expand Up @@ -53,17 +53,6 @@ function translateESLintRC(eslintrcConfig, {
const languageOptionsKeysToCopy = ["globals", "parser", "parserOptions"];
const linterOptionsKeysToCopy = ["noInlineConfig", "reportUnusedDisableDirectives"];

// check for special settings for eslint:all and eslint:recommended:
if (eslintrcConfig.settings) {
if (eslintrcConfig.settings["eslint:all"] === true) {
return ["eslint:all"];
}

if (eslintrcConfig.settings["eslint:recommended"] === true) {
return ["eslint:recommended"];
}
}

// copy over simple translations
for (const key of keysToCopy) {
if (key in eslintrcConfig && typeof eslintrcConfig[key] !== "undefined") {
Expand Down Expand Up @@ -215,15 +204,31 @@ class FlatCompat {

constructor({
baseDirectory = process.cwd(),
resolvePluginsRelativeTo = baseDirectory
resolvePluginsRelativeTo = baseDirectory,
recommendedConfig,
allConfig
} = {}) {
this.baseDirectory = baseDirectory;
this.resolvePluginsRelativeTo = resolvePluginsRelativeTo;
this[cafactory] = new ConfigArrayFactory({
cwd: baseDirectory,
resolvePluginsRelativeTo,
getEslintAllConfig: () => ({ settings: { "eslint:all": true } }),
getEslintRecommendedConfig: () => ({ settings: { "eslint:recommended": true } })
getEslintAllConfig: () => {

if (!allConfig) {
throw new TypeError("Missing parameter 'allConfig' in FlatCompat constructor.");
}

return allConfig;
},
getEslintRecommendedConfig: () => {

if (!recommendedConfig) {
throw new TypeError("Missing parameter 'recommendedConfig' in FlatCompat constructor.");
}

return recommendedConfig;
}
});
}

Expand Down
82 changes: 66 additions & 16 deletions tests/lib/flat-compat.js
Expand Up @@ -62,7 +62,9 @@ describe("FlatCompat", () => {

beforeEach(() => {
compat = new FlatCompat({
baseDirectory
baseDirectory,
recommendedConfig: { settings: { "eslint:recommended": true } },
allConfig: { settings: { "eslint:all": true } }
});
});

Expand Down Expand Up @@ -239,7 +241,7 @@ describe("FlatCompat", () => {
});
});

it("should translate extends eslint:all into a string", () => {
it("should translate extends eslint:all into settings", () => {
const result = compat.config({
extends: "eslint:all",
rules: {
Expand All @@ -248,15 +250,15 @@ describe("FlatCompat", () => {
});

assert.strictEqual(result.length, 2);
assert.deepStrictEqual(result[0], "eslint:all");
assert.isTrue(result[0].settings["eslint:all"]);
assert.deepStrictEqual(result[1], {
rules: {
foo: "warn"
}
});
});

it("should translate extends [eslint:all] into a string", () => {
it("should translate extends [eslint:all] into settings", () => {
const result = compat.config({
extends: ["eslint:all"],
rules: {
Expand All @@ -265,15 +267,15 @@ describe("FlatCompat", () => {
});

assert.strictEqual(result.length, 2);
assert.deepStrictEqual(result[0], "eslint:all");
assert.isTrue(result[0].settings["eslint:all"]);
assert.deepStrictEqual(result[1], {
rules: {
foo: "warn"
}
});
});

it("should translate extends eslint:recommended into a string", () => {
it("should translate extends eslint:recommended into settings", () => {
const result = compat.config({
extends: "eslint:recommended",
rules: {
Expand All @@ -282,15 +284,15 @@ describe("FlatCompat", () => {
});

assert.strictEqual(result.length, 2);
assert.deepStrictEqual(result[0], "eslint:recommended");
assert.isTrue(result[0].settings["eslint:recommended"]);
assert.deepStrictEqual(result[1], {
rules: {
foo: "warn"
}
});
});

it("should translate extends [eslint:recommended] into a string", () => {
it("should translate extends [eslint:recommended] into settings", () => {
const result = compat.config({
extends: ["eslint:recommended"],
rules: {
Expand All @@ -299,7 +301,7 @@ describe("FlatCompat", () => {
});

assert.strictEqual(result.length, 2);
assert.deepStrictEqual(result[0], "eslint:recommended");
assert.isTrue(result[0].settings["eslint:recommended"]);
assert.deepStrictEqual(result[1], {
rules: {
foo: "warn"
Expand Down Expand Up @@ -346,7 +348,7 @@ describe("FlatCompat", () => {
}
}
});
assert.deepStrictEqual(result[1], "eslint:all");
assert.isTrue(result[1].settings["eslint:all"]);
assert.deepStrictEqual(result[2], {
languageOptions: {
globals: {
Expand All @@ -364,6 +366,26 @@ describe("FlatCompat", () => {
});
});

it("should throw an error when extending eslint:all without allConfig", () => {
const invalidCompat = new FlatCompat();

assert.throws(() => {
invalidCompat.config({
extends: "eslint:all"
});
}, /Missing parameter 'allConfig'/gu);
});

it("should throw an error when extending eslint:recommended without recommendedConfig", () => {
const invalidCompat = new FlatCompat();

assert.throws(() => {
invalidCompat.config({
extends: "eslint:recommended"
});
}, /Missing parameter 'recommendedConfig'/gu);
});

});

describe("overrides", () => {
Expand Down Expand Up @@ -850,7 +872,9 @@ describe("FlatCompat", () => {

beforeEach(() => {
compat = new FlatCompat({
baseDirectory: getFixturePath("config")
baseDirectory: getFixturePath("config"),
recommendedConfig: { settings: { "eslint:recommended": true } },
allConfig: { settings: { "eslint:all": true } }
});
});

Expand All @@ -867,18 +891,18 @@ describe("FlatCompat", () => {
});
});

it("should translate extends eslint:all into a string", () => {
it("should translate extends eslint:all into settings", () => {
const result = compat.extends("eslint:all");

assert.strictEqual(result.length, 1);
assert.deepStrictEqual(result[0], "eslint:all");
assert.isTrue(result[0].settings["eslint:all"]);
});

it("should translate extends eslint:recommended into a string", () => {
it("should translate extends eslint:recommended into settings", () => {
const result = compat.extends("eslint:recommended");

assert.strictEqual(result.length, 1);
assert.deepStrictEqual(result[0], "eslint:recommended");
assert.isTrue(result[0].settings["eslint:recommended"]);
});

it("should translate extends array with multiple configs into config objects", () => {
Expand All @@ -892,7 +916,7 @@ describe("FlatCompat", () => {
}
}
});
assert.deepStrictEqual(result[1], "eslint:all");
assert.isTrue(result[1].settings["eslint:all"]);
assert.deepStrictEqual(result[2], {
languageOptions: {
globals: {
Expand All @@ -905,6 +929,32 @@ describe("FlatCompat", () => {
});
});

it("should throw an error when extending eslint:all without allConfig", () => {
const invalidCompat = new FlatCompat();

assert.throws(() => {
invalidCompat.extends("eslint:all");
}, /Missing parameter 'allConfig'/gu);
});

it("should throw an error when extending eslint:recommended without recommendedConfig", () => {
const invalidCompat = new FlatCompat();

assert.throws(() => {
invalidCompat.extends("eslint:recommended");
}, /Missing parameter 'recommendedConfig'/gu);
});

it("should throw an error when extending eslint:recommended without recommendedConfig but with allConfig", () => {
const invalidCompat = new FlatCompat({
allConfig: {}
});

assert.throws(() => {
invalidCompat.extends("eslint:recommended");
}, /Missing parameter 'recommendedConfig'/gu);
});

});


Expand Down