Skip to content

Commit

Permalink
fix: use eslint-config-airbnb-base by default (#92)
Browse files Browse the repository at this point in the history
eslint-congif-airbnb is only used for react.
fixes #87
  • Loading branch information
aladdin-add committed Apr 10, 2024
1 parent 136df22 commit 627306a
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 4 deletions.
16 changes: 12 additions & 4 deletions lib/config-generator.js
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"
? { 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
@@ -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
@@ -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
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

0 comments on commit 627306a

Please sign in to comment.