Skip to content

Commit

Permalink
feat(diff): Four rules sections are now mutually exclusive
Browse files Browse the repository at this point in the history
BREAKING CHANGE: sharedRules key in diff object was changed to 
matchingRules, and no longer includes the rules in the ruleDifferences
array.
  • Loading branch information
scottnonnenberg committed Jun 20, 2016
1 parent e7dd85f commit c2350af
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 38 deletions.
33 changes: 19 additions & 14 deletions src/get_differences.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

/* eslint-disable security/detect-object-injection */
/* eslint-disable security/detect-object-injection, max-statements */

var _ = require('lodash');
var equal = require('deep-equal');
Expand All @@ -21,6 +21,22 @@ function compareRules(leftConfig, rightConfig) {
var rightClean = _.omit(rightConfig, ['rules', 'plugins', 'extends']);

var sharedRules = _.intersection(leftRules, rightRules);
var ruleDifferences = _.chain(sharedRules)
.map(function(rule) {
return {
rule: rule,
left: leftConfig.rules[rule],
right: rightConfig.rules[rule],
};
})
.reject(function(item) {
return equal(item.left, item.right);
})
.value();
var ruleDifferenceNames = _.map(ruleDifferences, function(item) {
return item.rule;
});
var matchingRules = _.difference(sharedRules, ruleDifferenceNames);

return {
pluginsMissingFromLeft: _.difference(rightPlugins, leftPlugins),
Expand All @@ -32,20 +48,9 @@ function compareRules(leftConfig, rightConfig) {
extendsMissingFromRight: _.difference(leftExtends, rightExtends),

rulesMissingFromLeft: _.difference(rightRules, leftRules),
sharedRules: sharedRules,
matchingRules: matchingRules,
rulesMissingFromRight: _.difference(leftRules, rightRules),
ruleDifferences: _.chain(sharedRules)
.map(function(rule) {
return {
rule: rule,
left: leftConfig.rules[rule],
right: rightConfig.rules[rule],
};
})
.reject(function(item) {
return equal(item.left, item.right);
})
.value(),
ruleDifferences: ruleDifferences,

differences: deepDiff(leftClean, rightClean) || [],
};
Expand Down
11 changes: 4 additions & 7 deletions src/get_score.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,18 @@ var MAX = 100;
module.exports = function(differences) {
var totals = {
rulesMissingFromLeft: getLength(differences.rulesMissingFromLeft),
sharedRules: getLength(differences.sharedRules),
matchingRules: getLength(differences.matchingRules),
rulesMissingFromRight: getLength(differences.rulesMissingFromRight),
ruleDifferences: getLength(differences.ruleDifferences),
};

var totalRules = totals.rulesMissingFromLeft + totals.sharedRules
+ totals.rulesMissingFromRight;
var unmatched = totals.ruleDifferences + totals.rulesMissingFromLeft
+ totals.rulesMissingFromRight;

var totalRules = totals.rulesMissingFromLeft + totals.matchingRules
+ totals.rulesMissingFromRight + totals.ruleDifferences;
if (totalRules === 0) {
return MAX;
}

return Math.round((totalRules - unmatched) / totalRules * MAX);
return Math.round(totals.matchingRules / totalRules * MAX);
};

function getLength(array) {
Expand Down
4 changes: 2 additions & 2 deletions src/render_differences.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ function printExtendsDifferences(result) {


function printRuleDifferences(result) {
return 'Rules shared:'
+ joinOrNone(_.map(result.sharedRules, bold))
return 'Rules matching:'
+ joinOrNone(_.map(result.matchingRules, bold))
+ SEPARATOR
+ 'Rules missing from left:'
+ joinOrNone(_.map(result.rulesMissingFromLeft, bold))
Expand Down
14 changes: 8 additions & 6 deletions test/unit/test_get_differences.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('unit/getDifferences', function() {
expect(actual).to.have.property('extendsMissingFromRight').that.deep.equal([]);

expect(actual).to.have.property('rulesMissingFromLeft').that.deep.equal([]);
expect(actual).to.have.property('sharedRules').that.deep.equal([]);
expect(actual).to.have.property('matchingRules').that.deep.equal([]);
expect(actual).to.have.property('rulesMissingFromRight').that.deep.equal([]);
expect(actual).to.have.property('ruleDifferences').that.deep.equal([]);

Expand All @@ -42,6 +42,7 @@ describe('unit/getDifferences', function() {
three: ['error', {
setting: 1,
}],
four: 'error',
},
};
var right = {
Expand All @@ -51,6 +52,7 @@ describe('unit/getDifferences', function() {
setting: 2,
}],
four: 'error',
five: 'error',
},
};

Expand All @@ -65,9 +67,9 @@ describe('unit/getDifferences', function() {
expect(actual).to.have.property('extendsMissingFromRight').that.deep.equal([]);

expect(actual).to.have.property('rulesMissingFromLeft')
.that.deep.equal(['four']);
expect(actual).to.have.property('sharedRules')
.that.deep.equal(['two', 'three']);
.that.deep.equal(['five']);
expect(actual).to.have.property('matchingRules')
.that.deep.equal(['two', 'four']);
expect(actual).to.have.property('rulesMissingFromRight')
.that.deep.equal(['one']);
expect(actual).to.have.property('ruleDifferences')
Expand Down Expand Up @@ -107,7 +109,7 @@ describe('unit/getDifferences', function() {
expect(actual).to.have.property('extendsMissingFromRight').that.deep.equal([]);

expect(actual).to.have.property('rulesMissingFromLeft').that.deep.equal([]);
expect(actual).to.have.property('sharedRules').that.deep.equal([]);
expect(actual).to.have.property('matchingRules').that.deep.equal([]);
expect(actual).to.have.property('rulesMissingFromRight').that.deep.equal([]);
expect(actual).to.have.property('ruleDifferences').that.deep.equal([]);

Expand Down Expand Up @@ -142,7 +144,7 @@ describe('unit/getDifferences', function() {
expect(actual).to.have.property('extendsMissingFromRight').that.deep.equal([]);

expect(actual).to.have.property('rulesMissingFromLeft').that.deep.equal([]);
expect(actual).to.have.property('sharedRules').that.deep.equal([]);
expect(actual).to.have.property('matchingRules').that.deep.equal([]);
expect(actual).to.have.property('rulesMissingFromRight').that.deep.equal([]);
expect(actual).to.have.property('ruleDifferences').that.deep.equal([]);

Expand Down
14 changes: 7 additions & 7 deletions test/unit/test_get_score.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('unit/getScore', function() {
it('returns 100 for no differences', function() {
var differences = {
rulesMissingFromLeft: [],
sharedRules: [],
matchingRules: [],
rulesMissingFromRight: [],
ruleDifferences: [],
};
Expand All @@ -28,11 +28,11 @@ describe('unit/getScore', function() {

it('returns all differences', function() {
var differences = {
rulesMissingFromLeft: ['one', 'two'],
sharedRules: ['three', 'four'],
rulesMissingFromRight: ['five', 'six'],
rulesMissingFromLeft: ['one', 'two', 'three'],
matchingRules: ['four', 'five'],
rulesMissingFromRight: ['six', 'seven'],
ruleDifferences: [{
rule: 'five',
rule: 'eight',
left: ['error', {
setting: 1,
}],
Expand All @@ -41,13 +41,13 @@ describe('unit/getScore', function() {
};
var actual = getScore(differences);

expect(actual).to.equal(17);
expect(actual).to.equal(25);
});

it('returns minimal differences', function() {
var differences = {
rulesMissingFromLeft: [],
sharedRules: ['one'],
matchingRules: ['one'],
rulesMissingFromRight: [],
ruleDifferences: [],
};
Expand Down
4 changes: 2 additions & 2 deletions test/unit/test_render_differences.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ describe('unit/renderDifferences', function() {
+ ' _three\n'
+ ' _four\n'
+ '\n'
+ 'Rules shared: None\n'
+ 'Rules matching: None\n'
+ '\n'
+ 'Rules missing from left: 2\n'
+ ' +one\n'
Expand Down Expand Up @@ -144,7 +144,7 @@ describe('unit/renderDifferences', function() {
+ '\n'
+ 'Extends missing from right: None\n'
+ '\n'
+ 'Rules shared: None\n'
+ 'Rules matching: None\n'
+ '\n'
+ 'Rules missing from left: None\n'
+ '\n'
Expand Down

0 comments on commit c2350af

Please sign in to comment.