Skip to content

Commit

Permalink
feature: putout: ignore: add ability to merge negated (kaelzhang/node…
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Nov 22, 2023
1 parent 7a4f7e5 commit 4a83bb8
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
30 changes: 29 additions & 1 deletion packages/putout/lib/merge.js
@@ -1,11 +1,39 @@
'use strict';

const deepmerge = require('deepmerge');
const arrayUnion = (...a) => [...new Set(a.flat())];

const isString = (a) => typeof a === 'string';
const arrayUnion = (...a) => {
const flatten = a.flat();
return mergeIgnore(Array.from(new Set(flatten)));
};

const arrayMerge = (a, b) => arrayUnion(b, a);

module.exports = (...args) => {
return deepmerge.all(args, {
arrayMerge,
});
};

function mergeIgnore(list) {
const negatives = [];

for (const current of list) {
if (isString(current) && current.startsWith('!'))
negatives.push(current.slice(1));
}

for (const current of negatives) {
const index = list.indexOf(current);

if (index >= 0) {
list.splice(index, 1);

const negIndex = list.indexOf(`!${current}`);
list.splice(negIndex, 1);
}
}

return list;
}
36 changes: 36 additions & 0 deletions packages/putout/lib/merge.spec.js
Expand Up @@ -22,3 +22,39 @@ test('putout: merge', (t) => {
t.deepEqual(result, expected);
t.end();
});

test('putout: merge: ignore', (t) => {
const defaultConfig = {
plugins: ['remove-unused-variables'],
};

const result = merge(defaultConfig, {
ignore: [
'**/coverage',
'!**/coverage',
],
});

const expected = {
plugins: ['remove-unused-variables'],
ignore: [],
};

t.deepEqual(result, expected);
t.end();
});

test('putout: merge: rules', (t) => {
const options = {
rules: {
'filesystem/remove-files': ['on', {
names: ['*.md'],
}],
},
};

const result = merge({}, options);

t.deepEqual(result, options);
t.end();
});

0 comments on commit 4a83bb8

Please sign in to comment.