From 8487f633cbe9cb64212ec81c6d27c455898ba02d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=AF=E7=84=B6?= Date: Wed, 10 Apr 2024 19:53:59 +0800 Subject: [PATCH] fix: use eslint-config-airbnb-base by default eslint-congif-airbnb is only used for react. fixes #87 --- lib/config-generator.js | 16 ++++++++--- tests/__snapshots__/style-esm-none-airbnb | 28 +++++++++++++++++++ tests/__snapshots__/style-esm-react-airbnb | 31 ++++++++++++++++++++++ tests/config-snapshots.spec.js | 22 +++++++++++++++ 4 files changed, 93 insertions(+), 4 deletions(-) create mode 100644 tests/__snapshots__/style-esm-none-airbnb create mode 100644 tests/__snapshots__/style-esm-react-airbnb diff --git a/lib/config-generator.js b/lib/config-generator.js index bdbdc676..170b053b 100644 --- a/lib/config-generator.js +++ b/lib/config-generator.js @@ -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" } } ]; @@ -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", @@ -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"; @@ -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); diff --git a/tests/__snapshots__/style-esm-none-airbnb b/tests/__snapshots__/style-esm-none-airbnb new file mode 100644 index 00000000..e6dfcfd9 --- /dev/null +++ b/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", + ], +} \ No newline at end of file diff --git a/tests/__snapshots__/style-esm-react-airbnb b/tests/__snapshots__/style-esm-react-airbnb new file mode 100644 index 00000000..97153d53 --- /dev/null +++ b/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", + ], +} \ No newline at end of file diff --git a/tests/config-snapshots.spec.js b/tests/config-snapshots.spec.js index f5f74bd5..3a7d8237 100644 --- a/tests/config-snapshots.spec.js +++ b/tests/config-snapshots.spec.js @@ -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 => {