From c0a86f5b5ed6ef071acef4baf38e7fc549fbec37 Mon Sep 17 00:00:00 2001 From: Ika Date: Sun, 13 Sep 2020 17:08:16 +0800 Subject: [PATCH] fix(resolve-extends): `extends` field should be resolved from left to right (#2070) * test: add failing test * fix(resolve-extends): `extends` field should be resolved from left to right BREAKING CHANGE: The order of the `extends` resolution is changed from right-to-left to left-to-right --- @commitlint/resolve-extends/src/index.test.ts | 29 +++++++++++++++++++ @commitlint/resolve-extends/src/index.ts | 4 +-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/@commitlint/resolve-extends/src/index.test.ts b/@commitlint/resolve-extends/src/index.test.ts index b6d06bb5e6..b386d001c4 100644 --- a/@commitlint/resolve-extends/src/index.test.ts +++ b/@commitlint/resolve-extends/src/index.test.ts @@ -243,6 +243,35 @@ test('propagates contents recursively with overlap', () => { expect(actual).toEqual(expected); }); +test('extends rules from left to right with overlap', () => { + const input = {extends: ['left', 'right']}; + + const require = (id: string) => { + switch (id) { + case 'left': + return {rules: {a: true}}; + case 'right': + return {rules: {a: false, b: true}}; + default: + return {}; + } + }; + + const ctx = {resolve: id, require: jest.fn(require)} as ResolveExtendsContext; + + const actual = resolveExtends(input, ctx); + + const expected = { + extends: ['left', 'right'], + rules: { + a: false, + b: true, + }, + }; + + expect(actual).toEqual(expected); +}); + test('extending contents should take precedence', () => { const input = {extends: ['extender-name'], zero: 'root'}; diff --git a/@commitlint/resolve-extends/src/index.ts b/@commitlint/resolve-extends/src/index.ts index 7fb9ec3426..f8c786c18b 100644 --- a/@commitlint/resolve-extends/src/index.ts +++ b/@commitlint/resolve-extends/src/index.ts @@ -32,7 +32,7 @@ export default function resolveExtends( context: ResolveExtendsContext = {} ) { const {extends: e} = config; - const extended = loadExtends(config, context).reduceRight( + const extended = loadExtends(config, context).reduce( (r, {extends: _, ...c}) => mergeWith(r, c, (objValue, srcValue) => { if (Array.isArray(objValue)) { @@ -78,7 +78,7 @@ function loadExtends( config.parserPreset = parserPreset; } - return [...configs, c, ...loadExtends(c, ctx)]; + return [...configs, ...loadExtends(c, ctx), c]; }, []); }