Skip to content

Commit 4c0b70b

Browse files
g-planekaicataldo
authored andcommitted
New: support TypeScript at config initializer (fixes #11789) (#12172)
* New: support TypeScript at config initializer (fixes #11789) * fix lint * tiny fixes
1 parent 94e39d9 commit 4c0b70b

2 files changed

Lines changed: 69 additions & 0 deletions

File tree

lib/init/config-initializer.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ function getModulesList(config, installESLint) {
120120
}
121121
}
122122

123+
const parser = config.parser || (config.parserOptions && config.parserOptions.parser);
124+
125+
if (parser) {
126+
modules[parser] = "latest";
127+
}
128+
123129
if (installESLint === false) {
124130
delete modules.eslint;
125131
} else {
@@ -291,6 +297,20 @@ function processAnswers(answers) {
291297
config.extends.push("plugin:vue/essential");
292298
}
293299

300+
if (answers.typescript) {
301+
if (answers.framework === "vue") {
302+
config.parserOptions.parser = "@typescript-eslint/parser";
303+
} else {
304+
config.parser = "@typescript-eslint/parser";
305+
}
306+
307+
if (Array.isArray(config.plugins)) {
308+
config.plugins.push("@typescript-eslint");
309+
} else {
310+
config.plugins = ["@typescript-eslint"];
311+
}
312+
}
313+
294314
// setup rules based on problems/style enforcement preferences
295315
if (answers.purpose === "problems") {
296316
config.extends.unshift("eslint:recommended");
@@ -306,6 +326,9 @@ function processAnswers(answers) {
306326
config = autoconfig.extendFromRecommended(config);
307327
}
308328
}
329+
if (answers.typescript && config.extends.includes("eslint:recommended")) {
330+
config.extends.push("plugin:@typescript-eslint/eslint-recommended");
331+
}
309332

310333
// normalize extends
311334
if (config.extends.length === 0) {
@@ -465,6 +488,12 @@ function promptUser() {
465488
{ name: "None of these", value: "none" }
466489
]
467490
},
491+
{
492+
type: "confirm",
493+
name: "typescript",
494+
message: "Does your project use TypeScript?",
495+
default: false
496+
},
468497
{
469498
type: "checkbox",
470499
name: "env",

tests/lib/init/config-initializer.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,24 @@ describe("configInitializer", () => {
168168
assert.deepStrictEqual(config.extends, ["eslint:recommended", "plugin:vue/essential"]);
169169
});
170170

171+
it("should enable typescript parser and plugin", () => {
172+
answers.typescript = true;
173+
const config = init.processAnswers(answers);
174+
175+
assert.strictEqual(config.parser, "@typescript-eslint/parser");
176+
assert.deepStrictEqual(config.plugins, ["@typescript-eslint"]);
177+
assert.deepStrictEqual(config.extends, ["eslint:recommended", "plugin:@typescript-eslint/eslint-recommended"]);
178+
});
179+
180+
it("should enable typescript parser and plugin with vue", () => {
181+
answers.framework = "vue";
182+
answers.typescript = true;
183+
const config = init.processAnswers(answers);
184+
185+
assert.strictEqual(config.parserOptions.parser, "@typescript-eslint/parser");
186+
assert.deepStrictEqual(config.plugins, ["vue", "@typescript-eslint"]);
187+
});
188+
171189
it("should extend eslint:recommended", () => {
172190
const config = init.processAnswers(answers);
173191

@@ -317,6 +335,28 @@ describe("configInitializer", () => {
317335
assert.include(modules, "eslint-plugin-vue@latest");
318336
assert.include(modules, "eslint-config-standard@latest");
319337
});
338+
339+
it("should support custom parser", () => {
340+
const config = {
341+
parser: "@typescript-eslint/parser"
342+
};
343+
const modules = init.getModulesList(config);
344+
345+
assert.include(modules, "@typescript-eslint/parser@latest");
346+
});
347+
348+
it("should support custom parser with Vue.js", () => {
349+
const config = {
350+
351+
// We should declare the parser at `parserOptions` when using with `eslint-plugin-vue`.
352+
parserOptions: {
353+
parser: "@typescript-eslint/parser"
354+
}
355+
};
356+
const modules = init.getModulesList(config);
357+
358+
assert.include(modules, "@typescript-eslint/parser@latest");
359+
});
320360
});
321361

322362
describe("auto", () => {

0 commit comments

Comments
 (0)