Skip to content

Commit

Permalink
New: support TypeScript at config initializer (fixes #11789)
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane committed Aug 27, 2019
1 parent 31e5428 commit c034d61
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
27 changes: 27 additions & 0 deletions lib/init/config-initializer.js
Expand Up @@ -119,6 +119,10 @@ function getModulesList(config, installESLint) {
);
}
}
const parser = config.parser || config.parserOptions.parser;
if (parser) {
modules[parser] = "latest";
}

if (installESLint === false) {
delete modules.eslint;
Expand Down Expand Up @@ -291,6 +295,20 @@ function processAnswers(answers) {
config.extends.push("plugin:vue/essential");
}

if (answers.typescript) {
if (answers.framework === "vue") {
config.parserOptions.parser = "@typescript-eslint/parser";
} else {
config.parser = "@typescript-eslint/parser";
}

if (Array.isArray(config.plugins)) {
config.plugins.push("@typescript-eslint");
} else {
config.plugins = ["@typescript-eslint"];
}
}

// setup rules based on problems/style enforcement preferences
if (answers.purpose === "problems") {
config.extends.unshift("eslint:recommended");
Expand All @@ -306,6 +324,9 @@ function processAnswers(answers) {
config = autoconfig.extendFromRecommended(config);
}
}
if (answers.typescript && config.extends.includes("eslint:recommended")) {
config.extends.push("plugin:@typescript-eslint/eslint-recommended");
}

// normalize extends
if (config.extends.length === 0) {
Expand Down Expand Up @@ -465,6 +486,12 @@ function promptUser() {
{ name: "None of these", value: "none" }
]
},
{
type: "confirm",
name: "typescript",
message: "Does your project use TypeScript?",
default: false
},
{
type: "checkbox",
name: "env",
Expand Down
39 changes: 39 additions & 0 deletions tests/lib/init/config-initializer.js
Expand Up @@ -168,6 +168,24 @@ describe("configInitializer", () => {
assert.deepStrictEqual(config.extends, ["eslint:recommended", "plugin:vue/essential"]);
});

it("should enable typescript parser and plugin", () => {
answers.typescript = true;
const config = init.processAnswers(answers);

assert.strictEqual(config.parser, "@typescript-eslint/parser");
assert.deepStrictEqual(config.plugins, ["@typescript-eslint"]);
assert.deepStrictEqual(config.extends, ["eslint:recommended", "plugin:@typescript-eslint/eslint-recommended"]);
});

it("should enable typescript parser and plugin with vue", () => {
answers.framework = "vue";
answers.typescript = true;
const config = init.processAnswers(answers);

assert.strictEqual(config.parserOptions.parser, "@typescript-eslint/parser");
assert.deepStrictEqual(config.plugins, ["vue", "@typescript-eslint"]);
});

it("should extend eslint:recommended", () => {
const config = init.processAnswers(answers);

Expand Down Expand Up @@ -317,6 +335,27 @@ describe("configInitializer", () => {
assert.include(modules, "eslint-plugin-vue@latest");
assert.include(modules, "eslint-config-standard@latest");
});

it("should support custom parser", () => {
const config = {
parser: "@typescript-eslint"
};
const modules = init.getModulesList(config);

assert.include(modules, "@typescript-eslint/parser@latest");
});

it("should support custom parser with Vue.js", () => {
const config = {
// We should declare the parser at `parserOptions` when using with `eslint-plugin-vue`.
parserOptions: {
parser: "@typescript-eslint"
}
};
const modules = init.getModulesList(config);

assert.include(modules, "@typescript-eslint/parser@latest");
});
});

describe("auto", () => {
Expand Down

0 comments on commit c034d61

Please sign in to comment.