Skip to content

Commit

Permalink
fix: use xo-typescript for ts files
Browse files Browse the repository at this point in the history
  • Loading branch information
aladdin-add committed Jul 25, 2022
1 parent 0721b85 commit 211a8f1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 30 deletions.
68 changes: 38 additions & 30 deletions lib/init/config-initializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -146,7 +154,8 @@ function processAnswers(answers) {
rules: {},
env: {},
parserOptions: {},
extends: []
extends: [],
overrides: []
};

config.parserOptions.ecmaVersion = "latest";
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions tests/init/config-initializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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" };

Expand Down

0 comments on commit 211a8f1

Please sign in to comment.