Skip to content

Commit

Permalink
New: support TypeScript at config initializer (fixes #11789) (#12172)
Browse files Browse the repository at this point in the history
* New: support TypeScript at config initializer (fixes #11789)

* fix lint

* tiny fixes
  • Loading branch information
g-plane authored and kaicataldo committed Aug 30, 2019
1 parent 94e39d9 commit 4c0b70b
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
29 changes: 29 additions & 0 deletions lib/init/config-initializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ function getModulesList(config, installESLint) {
}
}

const parser = config.parser || (config.parserOptions && config.parserOptions.parser);

if (parser) {
modules[parser] = "latest";
}

if (installESLint === false) {
delete modules.eslint;
} else {
Expand Down Expand Up @@ -291,6 +297,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 +326,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 +488,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
40 changes: 40 additions & 0 deletions tests/lib/init/config-initializer.js
Original file line number Diff line number Diff line change
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,28 @@ 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/parser"
};
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/parser"
}
};
const modules = init.getModulesList(config);

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

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

0 comments on commit 4c0b70b

Please sign in to comment.