Skip to content

Commit

Permalink
Fix: init error in extending recommended config (fixes #12707) (#12738)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
yeonjuan committed Apr 6, 2020
1 parent 5dfd4eb commit bfa811c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
6 changes: 3 additions & 3 deletions lib/init/autoconfig.js
Expand Up @@ -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);
Expand All @@ -333,7 +333,7 @@ function extendFromRecommended(config) {
delete newConfig.rules[ruleId];
}
});
newConfig.extends = RECOMMENDED_CONFIG_NAME;
newConfig.extends.unshift(RECOMMENDED_CONFIG_NAME);
return newConfig;
}

Expand Down
42 changes: 41 additions & 1 deletion tests/lib/init/autoconfig.js
Expand Up @@ -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() });

Expand Down Expand Up @@ -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);
});
});
});

0 comments on commit bfa811c

Please sign in to comment.