From 211a8f19f72dc09ce2f91f609258415be8bd3e2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=AF=E7=84=B6?= Date: Mon, 25 Jul 2022 11:38:56 +0800 Subject: [PATCH] fix: use xo-typescript for ts files --- lib/init/config-initializer.js | 68 ++++++++++++++++++-------------- tests/init/config-initializer.js | 11 ++++++ 2 files changed, 49 insertions(+), 30 deletions(-) diff --git a/lib/init/config-initializer.js b/lib/init/config-initializer.js index 9dcd314d..8219ca69 100644 --- a/lib/init/config-initializer.js +++ b/lib/init/config-initializer.js @@ -97,21 +97,29 @@ function getModulesList(config, installESLint) { modules[moduleName] = "latest"; } } - if (config.extends) { - const extendList = Array.isArray(config.extends) ? config.extends : [config.extends]; - for (const extend of extendList) { - if (extend.startsWith("eslint:") || extend.startsWith("plugin:")) { - continue; - } - const moduleName = naming.normalizePackageName(extend, "eslint-config"); + const extendList = []; + const overrides = config.overrides || []; - modules[moduleName] = "latest"; - Object.assign( - modules, - getPeerDependencies(`${moduleName}@latest`) - ); + for (const item of [config, ...overrides]) { + if (typeof item.extends === "string") { + extendList.push(item.extends); + } else if (Array.isArray(item.extends)) { + extendList.push(...item.extends); + } + } + + for (const extend of extendList) { + if (extend.startsWith("eslint:") || extend.startsWith("plugin:")) { + continue; } + const moduleName = naming.normalizePackageName(extend, "eslint-config"); + + modules[moduleName] = "latest"; + Object.assign( + modules, + getPeerDependencies(`${moduleName}@latest`) + ); } const parser = config.parser || (config.parserOptions && config.parserOptions.parser); @@ -146,7 +154,8 @@ function processAnswers(answers) { rules: {}, env: {}, parserOptions: {}, - extends: [] + extends: [], + overrides: [] }; config.parserOptions.ecmaVersion = "latest"; @@ -184,6 +193,22 @@ function processAnswers(answers) { } } + // set config.extends based the selected guide + if (answers.source === "guide") { + if (answers.styleguide === "airbnb" && answers.framework !== "react") { + config.extends.push("airbnb-base"); + } else if (answers.styleguide === "xo-typescript") { + config.extends.push("xo"); + config.overrides.push({ + files: ["*.ts", "*.tsx"], + extends: ["xo-typescript"] + }); + } else { + config.extends.push(answers.styleguide); + } + } + + // setup rules based on problems/style enforcement preferences if (answers.purpose === "problems") { config.extends.unshift("eslint:recommended"); @@ -490,25 +515,8 @@ function promptUser() { if (earlyAnswers.installESLint === false && !semver.satisfies(earlyAnswers.localESLintVersion, earlyAnswers.requiredESLintVersionRange)) { info(`Note: it might not work since ESLint's version is mismatched with the ${earlyAnswers.styleguide} config.`); } - if (earlyAnswers.styleguide === "airbnb" && earlyAnswers.framework !== "react") { - earlyAnswers.styleguide = "airbnb-base"; - } - if (earlyAnswers.styleguide === "xo-typescript") { - earlyAnswers.styleguide = ["xo", "xo-typescript"]; - } - const config = processAnswers(earlyAnswers); - const styleguide = Array.isArray(earlyAnswers.styleguide) ? earlyAnswers.styleguide : [earlyAnswers.styleguide]; - - if (Array.isArray(config.extends)) { - config.extends.push(...styleguide); - } else if (config.extends) { - config.extends = [config.extends, ...styleguide]; - } else { - config.extends = [...styleguide]; - } - const modules = getModulesList(config); return askInstallModules(modules) diff --git a/tests/init/config-initializer.js b/tests/init/config-initializer.js index 2cf086ed..4329ded3 100644 --- a/tests/init/config-initializer.js +++ b/tests/init/config-initializer.js @@ -272,6 +272,17 @@ describe("configInitializer", () => { assert.include(modules, "eslint-config-xo@latest"); }); + it("should support the xo-typescript style guide", () => { + const config = { extends: "xo", overrides: [{ files: ["*.ts"], extends: ["xo-typescript"] }] }; + const modules = init.getModulesList(config); + + assert.deepStrictEqual(config.extends, "xo"); + assert.deepStrictEqual(config.overrides[0].extends[0], "xo-typescript"); + assert.strictEqual(config.installedESLint, true); + assert.include(modules, "eslint-config-xo@latest"); + assert.include(modules, "eslint-config-xo-typescript@latest"); + }); + it("should install required sharable config", () => { const config = { extends: "google" };