Skip to content

Commit e398bd3

Browse files
author
Bart Veneman
committed
provide context/actuals for most guards
1 parent a3b4cea commit e398bd3

File tree

6 files changed

+48
-3
lines changed

6 files changed

+48
-3
lines changed

src/complexity.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const guards = [
1313
id: 'MoreThanMostCommonSelectorComplexity',
1414
score: 0,
1515
value: result.selectors.total === 0 ? 0 : selectorsAboveMode / result.selectors.total,
16+
actuals: result.selectors.complexity.items,
1617
}
1718

1819
if (selectorsAboveMode > result.selectors.total * 0.1) {
@@ -34,6 +35,7 @@ const guards = [
3435
id: 'MoreThanMostCommonSelectorSpecificity',
3536
score: 0,
3637
value: result.selectors.total === 0 ? 0 : selectorsAboveMode / result.selectors.total,
38+
actuals: result.selectors.specificity.items,
3739
}
3840

3941
if (selectorsAboveMode > result.selectors.total * 0.1) {
@@ -53,6 +55,7 @@ const guards = [
5355
id: 'MaxSelectorComplexity',
5456
score: 0,
5557
value: result.selectors.complexity.max,
58+
actuals: result.selectors.complexity.items,
5659
}
5760

5861
// Deduct 0.5 points per complexity over 5, up to 5 points
@@ -73,6 +76,7 @@ const guards = [
7376
id: 'AverageSelectorComplexity',
7477
score: 0,
7578
value: actual,
79+
actuals: result.selectors.complexity.items,
7680
}
7781

7882
// Deduct 2 points per selector over 2
@@ -91,6 +95,7 @@ const guards = [
9195
id: 'IdSelectorRatio',
9296
score: 0,
9397
value: actual,
98+
actuals: Object.keys(result.selectors.id.unique)
9499
}
95100

96101
if (actual > ALLOWED) {
@@ -108,6 +113,7 @@ const guards = [
108113
id: 'ImportantRatio',
109114
score: 0,
110115
value: actual,
116+
actuals: result.declarations.importants.total,
111117
}
112118

113119
if (actual > ALLOWED) {

src/complexity.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Complexity('should deduct points for a lot of Selectors more complex than most c
2525
id: 'MoreThanMostCommonSelectorComplexity',
2626
score: 5,
2727
value: 1 / 3,
28+
actuals: (new Array(1000).fill(1)).concat(new Array(500).fill(2)),
2829
}
2930
])
3031
assert.is(actual.complexity.score, 95)
@@ -51,6 +52,7 @@ Complexity('should deduct points for a lot of Selectors more complex than most c
5152
id: 'MoreThanMostCommonSelectorSpecificity',
5253
score: 2,
5354
value: 200 / 700,
55+
actuals: (new Array(500).fill([0, 0, 1])).concat(new Array(200).fill([0, 1, 0])),
5456
}
5557
])
5658
assert.is(actual.complexity.score, 98)
@@ -67,11 +69,13 @@ Complexity('deducts points for selectors with high complexity', () => {
6769
id: 'MaxSelectorComplexity',
6870
score: 3,
6971
value: 11,
72+
actuals: [11],
7073
},
7174
{
7275
id: 'AverageSelectorComplexity',
7376
score: 10,
7477
value: 11,
78+
actuals: [11],
7579
}
7680
])
7781
assert.is(actual.complexity.score, 87)
@@ -91,6 +95,7 @@ Complexity('deducts points for having a high ratio of ID selectors', () => {
9195
id: 'IdSelectorRatio',
9296
score: 2,
9397
value: 0.25,
98+
actuals: ['#a'],
9499
}
95100
])
96101
assert.is(actual.complexity.score, 98)
@@ -112,6 +117,7 @@ Complexity('deducts points for having a high ratio !importants', () => {
112117
id: 'ImportantRatio',
113118
score: 2,
114119
value: 0.25,
120+
actuals: 1,
115121
}
116122
])
117123
assert.is(actual.complexity.score, 98)

src/maintainability.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const guards = [
2626
id: 'AverageSelectorsPerRule',
2727
score: 0,
2828
value: actual,
29+
actuals: result.rules.selectors.items,
2930
}
3031

3132
// Deduct 5 points per selector over 2
@@ -45,12 +46,13 @@ const guards = [
4546
id: 'AverageDeclarationsPerRule',
4647
score: 0,
4748
value: result.rules.declarations.mean,
49+
actuals: result.rules.declarations.items,
4850
}
4951

5052
// Deduct 5 points per declaration over 5
5153
if (result.rules.declarations.mean > ALLOWED_DECLARATIONS_PER_RULESET) {
5254
const score = Math.floor((result.rules.declarations.mean - ALLOWED_DECLARATIONS_PER_RULESET) * 5)
53-
outcome.score = Math.min(15, 0)
55+
outcome.score = Math.min(15, score)
5456
}
5557

5658
return outcome
@@ -64,6 +66,7 @@ const guards = [
6466
id: 'MaxSelectorsPerRule',
6567
score: 0,
6668
value: result.rules.selectors.max,
69+
actuals: result.rules.selectors.items,
6770
}
6871

6972
// Deduct 0.5 points per selectors over 10
@@ -83,6 +86,7 @@ const guards = [
8386
id: 'MaxDeclarationsPerRule',
8487
score: 0,
8588
value: result.rules.declarations.max,
89+
actuals: result.rules.declarations.items,
8690
}
8791

8892
// Deduct 0.5 points per declarations over 10
@@ -106,6 +110,7 @@ const guards = [
106110
id: 'MoreThanMostCommonSelectorsPerRule',
107111
score: 0,
108112
value: result.rules.selectors.mode,
113+
actuals: result.rules.selectors.items,
109114
}
110115

111116
// if more than 10% of RuleSets has more Selectors than most common:
@@ -129,6 +134,7 @@ const guards = [
129134
id: 'MoreThanMostCommonDeclarationsPerRule',
130135
score: 0,
131136
value: result.rules.declarations.mode,
137+
actuals: result.rules.declarations.items,
132138
}
133139

134140
// if more than 10% of RuleSets has more Declarations than most common:

src/maintainability.test.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ Maintainability('deducts points for having too many Selectors per RuleSet', () =
5959
id: 'AverageSelectorsPerRule',
6060
score: 12,
6161
value: 4.5,
62+
actuals: [4, 5],
6263
}
6364
])
6465
})
@@ -90,11 +91,13 @@ Maintainability('deducts points for having too many Selectors in a RuleSet', ()
9091
id: 'AverageSelectorsPerRule',
9192
score: 15,
9293
value: 8,
94+
actuals: [15, 1],
9395
},
9496
{
9597
id: 'MaxSelectorsPerRule',
9698
score: 3,
9799
value: 15,
100+
actuals: [15, 1],
98101
},
99102
])
100103
})
@@ -114,12 +117,19 @@ Maintainability('deducts points for having too many Declarations in a single Rul
114117
`
115118
const actual = calculate(fixture)
116119

117-
assert.is(actual.maintainability.score, 93)
120+
assert.is(actual.maintainability.score, 78)
118121
assert.equal(actual.maintainability.violations, [
122+
{
123+
id: "AverageDeclarationsPerRule",
124+
score: 15,
125+
value: 12,
126+
actuals: [1, 23],
127+
},
119128
{
120129
id: 'MaxDeclarationsPerRule',
121130
score: 7, // Math.ceil((23 - 10) * 0.5) === Math.ceil(6.5)
122131
value: 23,
132+
actuals: [1, 23],
123133
},
124134
])
125135
})
@@ -145,6 +155,7 @@ Maintainability('deducts points for having RuleSets with more Selectors than wha
145155
id: 'MoreThanMostCommonSelectorsPerRule',
146156
score: 5,
147157
value: 1,
158+
actuals: (new Array(1000).fill(1)).concat(new Array(500).fill(2)),
148159
},
149160
])
150161
assert.is(actual.maintainability.score, 95)
@@ -171,6 +182,7 @@ Maintainability('deducts points for having RuleSets with more Selectors than wha
171182
id: 'MoreThanMostCommonDeclarationsPerRule',
172183
score: 5,
173184
value: 1,
185+
actuals: (new Array(1000).fill(1)).concat(new Array(500).fill(2)),
174186
},
175187
])
176188
assert.is(actual.maintainability.score, 95)

src/performance.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const guards = [
55
id: 'Imports',
66
score: result.atrules.import.total * 10,
77
value: result.atrules.import.total,
8+
actuals: Object.keys(result.atrules.import.unique),
89
}),
910

1011
// Should not contain empty rules
@@ -70,6 +71,7 @@ const guards = [
7071
id: 'TooMuchEmbeddedContent',
7172
score: Math.min(20, Math.floor(size.total / 250)),
7273
value: size.total,
74+
actuals: Object.keys(result.stylesheet.embeddedContent.unique),
7375
}
7476
},
7577
]

src/performance.test.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Performance('deducts points for having a single @import', () => {
2323
id: 'Imports',
2424
value: 1,
2525
score: 10,
26+
actuals: [`url('some-url')`],
2627
},
2728
])
2829
})
@@ -39,6 +40,7 @@ Performance('deducts points for having multiple @imports', () => {
3940
id: 'Imports',
4041
value: 2,
4142
score: 20,
43+
actuals: [`url('some-url')`, `url('another-url')`],
4244
},
4345
])
4446
})
@@ -55,6 +57,13 @@ Performance('deducts points for having a single empty rule', () => {
5557
test { color: green; }
5658
`)
5759
assert.is(actual.performance.score, 99)
60+
assert.equal(actual.performance.violations, [
61+
{
62+
id: 'EmptyRules',
63+
score: 1,
64+
value: 1,
65+
}
66+
])
5867
})
5968

6069
Performance('deducts points for having multiple empty rules', () => {
@@ -158,11 +167,14 @@ Performance('deducts points for having comments', () => {
158167
})
159168

160169
Performance('deducts points for having embedded content', () => {
170+
function generateEmbed(index) {
171+
return `data:image/svg+xml,%3Csvg%20id%3D%22Layer_${index}%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20195.6%20107.8%22%3E%3Cpath%20fill%3D%22%23B5B5B5%22%20class%3D%22st0%22%20d%3D%22M97.8%20107.8c-2.6%200-5.1-1-7.1-2.9L2.9%2017.1C-1%2013.2-1%206.8%202.9%202.9%206.8-1%2013.2-1%2017.1%202.9l80.7%2080.7%2080.7-80.7c3.9-3.9%2010.2-3.9%2014.1%200%203.9%203.9%203.9%2010.2%200%2014.1l-87.8%2087.8c-1.9%202-4.4%203-7%203z%22%2F%3E%3C%2Fsvg%3E`
172+
}
161173
const fixture = new Array(100)
162174
.fill('')
163175
.map((_, index) => `
164176
selector-${index} {
165-
background: url(data:image/svg+xml,%3Csvg%20id%3D%22Layer_${index}%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20195.6%20107.8%22%3E%3Cpath%20fill%3D%22%23B5B5B5%22%20class%3D%22st0%22%20d%3D%22M97.8%20107.8c-2.6%200-5.1-1-7.1-2.9L2.9%2017.1C-1%2013.2-1%206.8%202.9%202.9%206.8-1%2013.2-1%2017.1%202.9l80.7%2080.7%2080.7-80.7c3.9-3.9%2010.2-3.9%2014.1%200%203.9%203.9%203.9%2010.2%200%2014.1l-87.8%2087.8c-1.9%202-4.4%203-7%203z%22%2F%3E%3C%2Fsvg%3E);
177+
background: url(${generateEmbed(index)});
166178
}
167179
`)
168180
.join('')
@@ -174,6 +186,7 @@ Performance('deducts points for having embedded content', () => {
174186
id: 'TooMuchEmbeddedContent',
175187
score: 20,
176188
value: 45990,
189+
actuals: new Array(100).fill('').map((_, index) => generateEmbed(index)),
177190
},
178191
])
179192
})

0 commit comments

Comments
 (0)