Skip to content

Commit

Permalink
fix(resolve-extends): extends field should be resolved from left to…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
ikatyang authored Sep 13, 2020
1 parent 9e5d9bf commit c0a86f5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
29 changes: 29 additions & 0 deletions @commitlint/resolve-extends/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'};

Expand Down
4 changes: 2 additions & 2 deletions @commitlint/resolve-extends/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -78,7 +78,7 @@ function loadExtends(
config.parserPreset = parserPreset;
}

return [...configs, c, ...loadExtends(c, ctx)];
return [...configs, ...loadExtends(c, ctx), c];
}, []);
}

Expand Down

0 comments on commit c0a86f5

Please sign in to comment.