Skip to content

Commit

Permalink
Update: Behavior of --init (fixes #11105)
Browse files Browse the repository at this point in the history
  • Loading branch information
nzakas committed Feb 13, 2019
1 parent aaceefb commit eee3b16
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 42 deletions.
35 changes: 23 additions & 12 deletions lib/config/config-initializer.js
Expand Up @@ -3,6 +3,7 @@
* @author Ilya Volodin
*/


"use strict";

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -264,8 +265,10 @@ function processAnswers(answers) {
});

// add in library information
if (answers.framwork === "react") {
config.parserOptions.ecmaFeatures.jsx = true;
if (answers.framework === "react") {
config.parserOptions.ecmaFeatures = {
jsx: true
};
config.plugins = ["react"];
} else if (answers.framework === "vue") {
config.plugins = ["vue"];
Expand Down Expand Up @@ -473,7 +476,7 @@ function promptUser() {
type: "list",
name: "source",
message: "How would you like to define a style for your project?",
default: "prompt",
default: "guide",
choices: [
{ name: "Use a popular style guide", value: "guide" },
{ name: "Answer questions about your style", value: "prompt" },
Expand Down Expand Up @@ -516,10 +519,7 @@ function promptUser() {
name: "format",
message: "What format do you want your config file to be in?",
default: "JavaScript",
choices: ["JavaScript", "YAML", "JSON"],
when(answers) {
return ((answers.source === "guide" && answers.packageJsonExists) || answers.source === "auto");
}
choices: ["JavaScript", "YAML", "JSON"]
},
{
type: "confirm",
Expand All @@ -538,6 +538,15 @@ function promptUser() {
}
]).then(earlyAnswers => {

// early exit if no style guide is necessary
if (earlyAnswers.purpose !== "style") {
const config = processAnswers(earlyAnswers);
const modules = getModulesList(config);

return askInstallModules(modules, earlyAnswers.packageJsonExists)
.then(() => writeFile(config, earlyAnswers.format));
}

// early exit if you are using a style guide
if (earlyAnswers.source === "guide") {
if (!earlyAnswers.packageJsonExists) {
Expand All @@ -551,14 +560,16 @@ function promptUser() {
earlyAnswers.styleguide = "airbnb-base";
}

const config = getConfigForStyleGuide(earlyAnswers.styleguide);
const config = ConfigOps.merge(processAnswers(earlyAnswers), getConfigForStyleGuide(earlyAnswers.styleguide));
const modules = getModulesList(config);

return askInstallModules(modules, earlyAnswers.packageJsonExists)
.then(() => writeFile(config, earlyAnswers.format));

} else if (earlyAnswers.source === "auto") {
const combinedAnswers = Object.assign({}, earlyAnswers, secondAnswers);

}

if (earlyAnswers.source === "auto") {
const combinedAnswers = Object.assign({}, earlyAnswers);
const config = processAnswers(combinedAnswers);
const modules = getModulesList(config);

Expand Down Expand Up @@ -602,7 +613,7 @@ function promptUser() {
choices: ["JavaScript", "YAML", "JSON"]
}
]).then(answers => {
const totalAnswers = Object.assign({}, earlyAnswers, secondAnswers, answers);
const totalAnswers = Object.assign({}, earlyAnswers, answers);

const config = processAnswers(totalAnswers);
const modules = getModulesList(config);
Expand Down
4 changes: 4 additions & 0 deletions package.json
Expand Up @@ -54,7 +54,11 @@
"ignore": "^4.0.6",
"import-fresh": "^3.0.0",
"imurmurhash": "^0.1.4",
<<<<<<< HEAD
"inquirer": "^6.2.2",
=======
"inquirer": "^6.2.1",
>>>>>>> Update: Behavior of --init (fixes #11105)
"js-yaml": "^3.12.0",
"json-stable-stringify-without-jsonify": "^1.0.1",
"levn": "^0.3.0",
Expand Down
38 changes: 8 additions & 30 deletions tests/lib/config/config-initializer.js
Expand Up @@ -117,20 +117,17 @@ describe("configInitializer", () => {

beforeEach(() => {
answers = {
purpose: "style",
source: "prompt",
extendDefault: true,
indent: 2,
quotes: "single",
linebreak: "unix",
semi: true,
ecmaVersion: 2015,
modules: true,
moduleType: "esm",
es6Globals: true,
env: ["browser"],
jsx: false,
react: false,
format: "JSON",
commonjs: false
format: "JSON"
};
});

Expand All @@ -142,7 +139,7 @@ describe("configInitializer", () => {
assert.deepStrictEqual(config.rules["linebreak-style"], ["error", "unix"]);
assert.deepStrictEqual(config.rules.semi, ["error", "always"]);
assert.strictEqual(config.env.es6, true);
assert.strictEqual(config.parserOptions.ecmaVersion, 2015);
assert.strictEqual(config.parserOptions.ecmaVersion, 2018);
assert.strictEqual(config.parserOptions.sourceType, "module");
assert.strictEqual(config.env.browser, true);
assert.strictEqual(config.extends, "eslint:recommended");
Expand All @@ -155,31 +152,15 @@ describe("configInitializer", () => {
assert.deepStrictEqual(config.rules.semi, ["error", "never"]);
});

it("should enable jsx flag", () => {
answers.jsx = true;
const config = init.processAnswers(answers);

assert.strictEqual(config.parserOptions.ecmaFeatures.jsx, true);
});

it("should enable react plugin", () => {
answers.jsx = true;
answers.react = true;
answers.framework = "react";
const config = init.processAnswers(answers);

assert.strictEqual(config.parserOptions.ecmaFeatures.jsx, true);
assert.strictEqual(config.parserOptions.ecmaVersion, 2018);
assert.deepStrictEqual(config.plugins, ["react"]);
});

it("should not enable es6", () => {
answers.ecmaVersion = 5;
const config = init.processAnswers(answers);

assert.strictEqual(config.parserOptions.ecmaVersion, 5);
assert.isUndefined(config.env.es6);
});

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

Expand All @@ -193,7 +174,7 @@ describe("configInitializer", () => {
});

it("should use commonjs when set", () => {
answers.commonjs = true;
answers.moduleType = "commonjs";
const config = init.processAnswers(answers);

assert.isTrue(config.env.commonjs);
Expand Down Expand Up @@ -338,14 +319,11 @@ describe("configInitializer", () => {
].join(" ");

answers = {
purpose: "style",
source: "auto",
patterns,
ecmaVersion: 5,
env: ["browser"],
jsx: false,
react: false,
format: "JSON",
commonjs: false
format: "JSON"
};

sandbox = sinon.sandbox.create();
Expand Down

0 comments on commit eee3b16

Please sign in to comment.