From bfa811ceee801fe8ba212a5c879e13743146e909 Mon Sep 17 00:00:00 2001 From: YeonJuan Date: Tue, 7 Apr 2020 03:50:08 +0900 Subject: [PATCH] Fix: init error in extending recommended config (fixes #12707) (#12738) * Fix: error in extending recommended config (fixes #12707) * change to push * add test cases * add test case * fix typo, add test case * change to put eslint:recommended at the beginning. --- lib/init/autoconfig.js | 6 +++--- tests/lib/init/autoconfig.js | 42 +++++++++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/lib/init/autoconfig.js b/lib/init/autoconfig.js index 64be3d2a84f..c47c242dbd5 100644 --- a/lib/init/autoconfig.js +++ b/lib/init/autoconfig.js @@ -316,10 +316,10 @@ class Registry { /** * Extract rule configuration into eslint:recommended where possible. * - * This will return a new config with `"extends": "eslint:recommended"` and + * This will return a new config with `["extends": [ ..., "eslint:recommended"]` and * only the rules which have configurations different from the recommended config. * @param {Object} config config object - * @returns {Object} config object using `"extends": "eslint:recommended"` + * @returns {Object} config object using `"extends": ["eslint:recommended"]` */ function extendFromRecommended(config) { const newConfig = Object.assign({}, config); @@ -333,7 +333,7 @@ function extendFromRecommended(config) { delete newConfig.rules[ruleId]; } }); - newConfig.extends = RECOMMENDED_CONFIG_NAME; + newConfig.extends.unshift(RECOMMENDED_CONFIG_NAME); return newConfig; } diff --git a/tests/lib/init/autoconfig.js b/tests/lib/init/autoconfig.js index 3ce766a1283..1fa2b0476de 100644 --- a/tests/lib/init/autoconfig.js +++ b/tests/lib/init/autoconfig.js @@ -12,7 +12,8 @@ const assert = require("chai").assert, autoconfig = require("../../../lib/init/autoconfig"), sourceCodeUtils = require("../../../lib/init/source-code-utils"), - baseDefaultOptions = require("../../../conf/default-cli-options"); + baseDefaultOptions = require("../../../conf/default-cli-options"), + recommendedConfig = require("../../conf/eslint-recommended"); const defaultOptions = Object.assign({}, baseDefaultOptions, { cwd: process.cwd() }); @@ -328,4 +329,43 @@ describe("autoconfig", () => { }); }); }); + + describe("extendFromRecommended()", () => { + it("should return a configuration which has `extends` key with Array type value", () => { + const oldConfig = { extends: [], rules: {} }; + const newConfig = autoconfig.extendFromRecommended(oldConfig); + + assert.exists(newConfig.extends); + assert.isArray(newConfig.extends); + }); + + it("should return a configuration which has array property `extends`", () => { + const oldConfig = { extends: [], rules: {} }; + const newConfig = autoconfig.extendFromRecommended(oldConfig); + + assert.include(newConfig.extends, "eslint:recommended"); + }); + + it("should return a configuration which preserves the previous extending configurations", () => { + const oldConfig = { extends: ["previous:configuration1", "previous:configuration2"], rules: {} }; + const newConfig = autoconfig.extendFromRecommended(oldConfig); + + assert.includeMembers(newConfig.extends, oldConfig.extends); + }); + + it("should return a configuration which has `eslint:recommended` at the first of `extends`", () => { + const oldConfig = { extends: ["previous:configuration1", "previous:configuration2"], rules: {} }; + const newConfig = autoconfig.extendFromRecommended(oldConfig); + const [firstExtendInNewConfig] = newConfig.extends; + + assert.strictEqual(firstExtendInNewConfig, "eslint:recommended"); + }); + + it("should return a configuration which not includes rules configured in `eslint:recommended`", () => { + const oldConfig = { extends: [], rules: { ...recommendedConfig.rules } }; + const newConfig = autoconfig.extendFromRecommended(oldConfig); + + assert.notInclude(newConfig.rules, oldConfig.rules); + }); + }); });