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

fix: use eslint-config-airbnb-base by default #92

Merged
merged 1 commit into from
Apr 10, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions lib/config-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import * as log from "./utils/logging.js";

// TODO: need to specify the package version - they may export flat configs in the future.
const jsStyleGuides = [
{ message: "Airbnb: https://github.com/airbnb/javascript", name: "airbnb", value: { packageName: "eslint-config-airbnb", type: "eslintrc" } },
{ message: "Airbnb: https://github.com/airbnb/javascript", name: "airbnb", value: { packageName: "eslint-config-airbnb-base", type: "eslintrc" } },
{ message: "Standard: https://github.com/standard/standard", name: "standard", value: { packageName: "eslint-config-standard", type: "eslintrc" } },
{ message: "XO: https://github.com/xojs/eslint-config-xo", name: "xo", value: { packageName: "eslint-config-xo", type: "eslintrc" } }
];
Expand Down Expand Up @@ -112,6 +112,7 @@ export class ConfigGenerator {
Object.assign(this.answers, answers);

if (answers.purpose === "style") {

const choices = this.answers.language === "javascript" ? jsStyleGuides : tsStyleGuides;
const styleguideAnswer = await enquirer.prompt({
type: "select",
Expand All @@ -133,6 +134,15 @@ export class ConfigGenerator {
const isESMModule = isPackageTypeModule(this.packageJsonPath);

this.result.configFilename = isESMModule ? "eslint.config.js" : "eslint.config.mjs";
this.answers.styleguide = typeof this.answers.styleguide === "string"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In which cases this.answers.styleguide can be a string value?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More for the future: none of the style guides in the list now support flat configs, so you have to use:

"styleguide": { "packageName": "eslint-config-airbnb", "type": "eslintrc"}

But when a certain support is available it can be written as:

 "styleguide": "eslint-config-airbnb"

? { packageName: this.answers.styleguide, type: "flat" }
: this.answers.styleguide;

// replaced `eslint-config-airbnb-base` with `eslint-config-airbnb`(for react supports)
if (this.answers.styleguide?.packageName === "eslint-config-airbnb-base" && this.answers.framework === "react") {
this.answers.styleguide.packageName = "eslint-config-airbnb";
this.answers.framework = "none";
}

let importContent = "";
const helperContent = `import path from "path";
Expand Down Expand Up @@ -172,9 +182,7 @@ const compat = new FlatCompat({baseDirectory: __dirname, recommendedConfig: plug
importContent += "import pluginJs from \"@eslint/js\";\n";
exportContent += " pluginJs.configs.recommended,\n";
} else if (this.answers.purpose === "style") {
const styleguide = typeof this.answers.styleguide === "string"
? { packageName: this.answers.styleguide, type: "flat" }
: this.answers.styleguide;
const styleguide = this.answers.styleguide;

this.result.devDependencies.push(styleguide.packageName);

Expand Down
28 changes: 28 additions & 0 deletions tests/__snapshots__/style-esm-none-airbnb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"configContent": "import globals from "globals";

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

// mimic CommonJS variables -- not needed if using CommonJS
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const compat = new FlatCompat({baseDirectory: __dirname, recommendedConfig: pluginJs.configs.recommended});

export default [
{languageOptions: { globals: globals.browser }},
...compat.extends("airbnb-base"),
];",
"configFilename": "eslint.config.mjs",
"devDependencies": [
"eslint",
"globals",
"eslint-config-airbnb-base",
"eslint@^7.32.0 || ^8.2.0",
"eslint-plugin-import@^2.25.2",
"@eslint/eslintrc",
"@eslint/js",
],
}
31 changes: 31 additions & 0 deletions tests/__snapshots__/style-esm-react-airbnb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"configContent": "import globals from "globals";

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

// mimic CommonJS variables -- not needed if using CommonJS
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const compat = new FlatCompat({baseDirectory: __dirname, recommendedConfig: pluginJs.configs.recommended});

export default [
{languageOptions: { globals: globals.browser }},
...compat.extends("airbnb"),
];",
"configFilename": "eslint.config.mjs",
"devDependencies": [
"eslint",
"globals",
"eslint-config-airbnb",
"eslint@^7.32.0 || ^8.2.0",
"eslint-plugin-import@^2.25.3",
"eslint-plugin-jsx-a11y@^6.5.1",
"eslint-plugin-react@^7.28.0",
"eslint-plugin-react-hooks@^4.3.0",
"@eslint/eslintrc",
"@eslint/js",
],
}
22 changes: 22 additions & 0 deletions tests/config-snapshots.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,28 @@ describe("generate config for cjs projects", () => {
env: ["browser"],
styleguide: { packageName: "eslint-config-xo-typescript", type: "eslintrc" }
}
},
{
name: "style-esm-react-airbnb",
answers: {
purpose: "style",
moduleType: "esm",
framework: "react",
language: "javascript",
env: ["browser"],
styleguide: { packageName: "eslint-config-airbnb-base", type: "eslintrc" }
}
},
{
name: "style-esm-none-airbnb",
answers: {
purpose: "style",
moduleType: "esm",
framework: "none",
language: "javascript",
env: ["browser"],
styleguide: { packageName: "eslint-config-airbnb-base", type: "eslintrc" }
}
}];

inputs.forEach(item => {
Expand Down
Loading