Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(resolve-extends): extends field should be resolved from left to right #2070

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions @commitlint/resolve-extends/src/index.test.ts
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
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