Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
96 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,30 @@ | ||
module.exports = (rules, declarations) => { | ||
const average = ((rules, declarations) => { | ||
if (declarations.total === 0) { | ||
return 0 | ||
module.exports = rules => { | ||
const totalRules = rules.length | ||
const totalDeclarations = | ||
totalRules === 0 | ||
? 0 | ||
: rules.reduce((acc, curr) => acc + curr.declarations.length, 0) | ||
const rulesSortedByDeclarationCount = rules.sort( | ||
(a, b) => b.declarations.length - a.declarations.length | ||
) | ||
|
||
if (totalRules === 0 || totalDeclarations === 0) { | ||
return { | ||
average: 0, | ||
min: { | ||
count: 0, | ||
value: null | ||
} | ||
} | ||
} | ||
|
||
return declarations.total / rules.total | ||
})(rules, declarations) | ||
const [ruleWithMostDeclarations] = rulesSortedByDeclarationCount | ||
|
||
return { | ||
average | ||
average: totalDeclarations / totalRules, | ||
min: { | ||
count: ruleWithMostDeclarations.declarations.length, | ||
value: ruleWithMostDeclarations | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,61 @@ | ||
const test = require('ava') | ||
const analyze = require('../../../src/analyzer/stylesheets/cohesion') | ||
|
||
test('it calculates cohesion based on the ratio of total declarations and total rules', t => { | ||
const {average: actual} = analyze({total: 2}, {total: 4}) | ||
t.is(actual, 2) | ||
test('it calculates average cohesion based on the ratio of total declarations and total rules', t => { | ||
const actual = analyze([ | ||
{ | ||
declarations: [ | ||
{property: 'a', value: 'a'}, | ||
{property: 'b', value: 'b'}, | ||
{property: 'c', value: 'c'}, | ||
{property: 'd', value: 'd'} | ||
] | ||
}, | ||
{ | ||
declarations: [{property: 'a', value: 'a'}, {property: 'b', value: 'b'}] | ||
} | ||
]) | ||
t.is(actual.average, 3) | ||
}) | ||
|
||
test('it calculates cohesion correctly if there are no rules and/or declarations', t => { | ||
const {average: actual} = analyze({total: 0}, {total: 0}) | ||
t.is(actual, 0) | ||
test('it calculates average cohesion correctly if there are no rules and/or declarations', t => { | ||
const actual = analyze([]) | ||
t.is(actual.average, 0) | ||
}) | ||
|
||
test('it calculates lowest cohesion as the rule with the most declarations', t => { | ||
const ruleWithManyDeclarations = { | ||
selectors: ['test'], | ||
declarations: [ | ||
{property: 'a', value: 'a'}, | ||
{property: 'b', value: 'b'}, | ||
{property: 'c', value: 'c'}, | ||
{property: 'd', value: 'd'} | ||
] | ||
} | ||
const fixture = [ | ||
ruleWithManyDeclarations, | ||
{ | ||
declarations: [{property: 'a', value: 'a'}] | ||
} | ||
] | ||
const actual = analyze(fixture) | ||
|
||
t.is(actual.min.count, 4) | ||
t.deepEqual(actual.min.value, ruleWithManyDeclarations) | ||
}) | ||
|
||
test('it calculates lowest cohesion correctly if there are no declarations', t => { | ||
const actualWithNoRules = analyze([]) | ||
t.is(actualWithNoRules.min.count, 0) | ||
t.is(actualWithNoRules.min.value, null) | ||
|
||
const actualWithNoDeclarations = analyze([ | ||
{ | ||
selectors: ['a'], | ||
declarations: [] | ||
} | ||
]) | ||
t.is(actualWithNoDeclarations.min.count, 0) | ||
t.is(actualWithNoDeclarations.min.value, null) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters