@projectwallace/css-code-quality 2.0.0 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{css-code-quality.js → core.js} +6 -10
- package/dist/index.js +9 -0
- package/package.json +12 -15
- package/dist/css-code-quality.umd.cjs +0 -1
- package/src/complexity.js +0 -134
- package/src/core.js +0 -49
- package/src/index.js +0 -10
- package/src/maintainability.js +0 -159
- package/src/performance.js +0 -83
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { compareSpecificity as i
|
|
2
|
-
const
|
|
1
|
+
import { compareSpecificity as i } from "@projectwallace/css-analyzer";
|
|
2
|
+
const l = [
|
|
3
3
|
// Should not contain @import rules
|
|
4
4
|
/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result */
|
|
5
5
|
(e) => ({
|
|
@@ -64,7 +64,7 @@ const m = [
|
|
|
64
64
|
value: s.total
|
|
65
65
|
};
|
|
66
66
|
}
|
|
67
|
-
],
|
|
67
|
+
], m = [
|
|
68
68
|
// Source Lines of Code should be low
|
|
69
69
|
/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result */
|
|
70
70
|
(e) => {
|
|
@@ -172,7 +172,7 @@ const m = [
|
|
|
172
172
|
}
|
|
173
173
|
return t;
|
|
174
174
|
}
|
|
175
|
-
],
|
|
175
|
+
], u = [
|
|
176
176
|
// Complexity per Selector should not differ too much from the most common Complexity
|
|
177
177
|
/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result */
|
|
178
178
|
(e) => {
|
|
@@ -275,7 +275,7 @@ function n(e, s) {
|
|
|
275
275
|
};
|
|
276
276
|
}
|
|
277
277
|
function L(e) {
|
|
278
|
-
const s = n(e,
|
|
278
|
+
const s = n(e, l), o = n(e, m), t = n(e, u);
|
|
279
279
|
return {
|
|
280
280
|
violations: s.violations.concat(o.violations).concat(t.violations),
|
|
281
281
|
passes: s.passes.concat(o.passes).concat(t.passes),
|
|
@@ -284,10 +284,6 @@ function L(e) {
|
|
|
284
284
|
complexity: t
|
|
285
285
|
};
|
|
286
286
|
}
|
|
287
|
-
function d(e) {
|
|
288
|
-
const s = l(e);
|
|
289
|
-
return L(s);
|
|
290
|
-
}
|
|
291
287
|
export {
|
|
292
|
-
|
|
288
|
+
L as calculate
|
|
293
289
|
};
|
package/dist/index.js
ADDED
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@projectwallace/css-code-quality",
|
|
3
3
|
"description": "Calculate the Code Quality score of your CSS based on a range of different quality guards",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "3.0.0",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
|
-
"url": "git
|
|
7
|
+
"url": "git+https://github.com:projectwallace/css-code-quality.git"
|
|
8
8
|
},
|
|
9
9
|
"homepage": "https://github.com/projectwallace/css-code-quality",
|
|
10
10
|
"issues": "https://github.com/projectwallace/css-code-quality/issues",
|
|
@@ -23,28 +23,25 @@
|
|
|
23
23
|
"maintainability",
|
|
24
24
|
"score"
|
|
25
25
|
],
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
|
|
29
|
-
"!src/**/*.test.js"
|
|
30
|
-
],
|
|
26
|
+
"engines": {
|
|
27
|
+
"node": ">=16.0.0"
|
|
28
|
+
},
|
|
31
29
|
"type": "module",
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"module": "./dist/css-code-quality.js",
|
|
35
|
-
"unpkg": "./dist/css-code-quality.umd.cjs",
|
|
30
|
+
"main": "./dist/index.js",
|
|
31
|
+
"types": "./dist/index.d.ts",
|
|
36
32
|
"exports": {
|
|
37
33
|
".": {
|
|
38
34
|
"types": "./dist/index.d.ts",
|
|
39
|
-
"import": "./dist/
|
|
40
|
-
"require": "./dist/css-code-quality.umd.cjs"
|
|
35
|
+
"import": "./dist/index.js"
|
|
41
36
|
},
|
|
42
37
|
"./core": {
|
|
43
38
|
"types": "./dist/core.d.ts",
|
|
44
|
-
"import": "./
|
|
39
|
+
"import": "./dist/core.js"
|
|
45
40
|
}
|
|
46
41
|
},
|
|
47
|
-
"
|
|
42
|
+
"files": [
|
|
43
|
+
"dist"
|
|
44
|
+
],
|
|
48
45
|
"scripts": {
|
|
49
46
|
"test": "uvu",
|
|
50
47
|
"build": "vite build",
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
(function(a,r){typeof exports=="object"&&typeof module<"u"?r(exports,require("@projectwallace/css-analyzer")):typeof define=="function"&&define.amd?define(["exports","@projectwallace/css-analyzer"],r):(a=typeof globalThis<"u"?globalThis:a||self,r(a.cssCodeQuality={},a.cssAnalyzer))})(this,function(a,r){"use strict";const u=[e=>({id:"Imports",score:e.atrules.import.total*10,value:e.atrules.import.total,actuals:Object.keys(e.atrules.import.unique)}),e=>({id:"EmptyRules",score:e.rules.empty.total,value:e.rules.empty.total}),e=>{const s={id:"SelectorDuplications",score:0,value:1-e.selectors.uniquenessRatio};return e.selectors.uniquenessRatio<.66&&(s.score=Math.floor((1-e.selectors.uniquenessRatio)*10)),s},e=>{const s={id:"DeclarationDuplications",score:0,value:1-e.declarations.uniquenessRatio};return e.declarations.uniquenessRatio<.66&&(s.score=Math.floor((1-e.declarations.uniquenessRatio)*10)),s},e=>({id:"CssSize",score:e.stylesheet.size>2e5?5:0,value:e.stylesheet.size}),e=>{const{comments:s}=e.stylesheet;return{id:"TooMuchComments",score:Math.min(10,Math.floor(s.size/250)),value:s.size}},e=>{const{size:s}=e.stylesheet.embeddedContent;return{id:"TooMuchEmbeddedContent",score:Math.min(20,Math.floor(s.total/250)),value:s.total}}],m=[e=>{const s={id:"SourceLinesOfCode",score:0,value:e.stylesheet.sourceLinesOfCode};if(e.stylesheet.sourceLinesOfCode>1e4){const o=Math.floor((e.stylesheet.sourceLinesOfCode-1e4)/1e3);s.score=Math.min(15,o)}return s},e=>{const o=e.rules.selectors.mean,t={id:"AverageSelectorsPerRule",score:0,value:o,actuals:e.rules.selectors.items};if(o>2){const c=Math.floor((o-2)*5);t.score=Math.min(15,c)}return t},e=>{const o={id:"AverageDeclarationsPerRule",score:0,value:e.rules.declarations.mean,actuals:e.rules.declarations.items};if(e.rules.declarations.mean>5){const t=Math.floor((e.rules.declarations.mean-5)*5);o.score=Math.min(15,t)}return o},e=>{let o=e.rules.selectors.max||0;const t={id:"MaxSelectorsPerRule",score:0,value:o,actuals:e.rules.selectors.items};if(o>10){const c=Math.ceil((o-10)*.5);t.score=Math.min(c,15)}return t},e=>{const o=e.rules.declarations.max||0,t={id:"MaxDeclarationsPerRule",score:0,value:o,actuals:e.rules.declarations.items};if(o>10){const c=Math.ceil((o-10)*.5);t.score=Math.min(15,c)}return t},e=>{const s=e.rules.selectors.mode,o=e.rules.selectors.items.filter(c=>c>s).length,t={id:"MoreThanMostCommonSelectorsPerRule",score:0,value:e.rules.selectors.mode,actuals:e.rules.selectors.items};if(o>e.rules.total*.1){const c=Math.floor(o*.01);t.score=Math.min(15,c)}return t},e=>{const s=e.rules.selectors.mode,o=e.rules.declarations.items.filter(c=>c>s).length,t={id:"MoreThanMostCommonDeclarationsPerRule",score:0,value:e.rules.declarations.mode,actuals:e.rules.declarations.items};if(o>e.rules.total*.1){const c=Math.floor(o*.01);t.score=Math.min(15,c)}return t}],E=[e=>{const s=e.selectors.complexity.mode,o=e.selectors.complexity.items.filter(c=>c>s).length,t={id:"MoreThanMostCommonSelectorComplexity",score:0,value:e.selectors.total===0?0:o/e.selectors.total,actuals:e.selectors.complexity.items};if(o>e.selectors.total*.1){const c=Math.floor(o*.01);t.score=Math.min(10,c)}return t},e=>{const s=e.selectors.specificity.mode,t=e.selectors.specificity.items.filter(n=>r.compareSpecificity(n,s)<0).length,c={id:"MoreThanMostCommonSelectorSpecificity",score:0,value:e.selectors.total===0?0:t/e.selectors.total,actuals:e.selectors.specificity.items};if(t>e.selectors.total*.1){const n=Math.floor(t*.01);c.score=Math.min(10,n)}return c},e=>{const o=e.selectors.complexity.max||0,t={id:"MaxSelectorComplexity",score:0,value:e.selectors.complexity.max,actuals:e.selectors.complexity.items};if(o>5){const c=Math.ceil((o-5)*.5);t.score=Math.min(5,c)}return t},e=>{const o=e.selectors.complexity.mean,t={id:"AverageSelectorComplexity",score:0,value:o,actuals:e.selectors.complexity.items};if(o>2){const c=Math.ceil((o-2)*2);t.score=Math.min(10,c)}return t},e=>{const o=e.selectors.id.ratio,t={id:"IdSelectorRatio",score:0,value:o,actuals:Object.keys(e.selectors.id.unique)};if(o>.01){const c=Math.floor((o-.01)*10);t.score=Math.min(c,5)}return t},e=>{const o=e.declarations.importants.ratio,t={id:"ImportantRatio",score:0,value:o,actuals:e.declarations.importants.total};if(o>.01){const c=Math.floor((o-.01)*10);t.score=Math.min(c,5)}return t}];function l(e,s){let o=100,t=[],c=[];for(const n of s){const i=n(e);i.score>0?(o-=i.score,t.push(i)):c.push(i)}return{score:Math.max(o,0),violations:t,passes:c}}function d(e){const s=l(e,u),o=l(e,m),t=l(e,E);return{violations:s.violations.concat(o.violations).concat(t.violations),passes:s.passes.concat(o.passes).concat(t.passes),performance:s,maintainability:o,complexity:t}}function M(e){const s=r.analyze(e);return d(s)}a.calculate=M,Object.defineProperty(a,Symbol.toStringTag,{value:"Module"})});
|
package/src/complexity.js
DELETED
|
@@ -1,134 +0,0 @@
|
|
|
1
|
-
import { compareSpecificity } from '@projectwallace/css-analyzer'
|
|
2
|
-
|
|
3
|
-
export const guards = [
|
|
4
|
-
|
|
5
|
-
// Complexity per Selector should not differ too much from the most common Complexity
|
|
6
|
-
/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result */
|
|
7
|
-
result => {
|
|
8
|
-
const mode = result.selectors.complexity.mode
|
|
9
|
-
const selectorsAboveMode = result.selectors.complexity.items
|
|
10
|
-
.filter(c => c > mode)
|
|
11
|
-
.length
|
|
12
|
-
|
|
13
|
-
const outcome = {
|
|
14
|
-
id: 'MoreThanMostCommonSelectorComplexity',
|
|
15
|
-
score: 0,
|
|
16
|
-
value: result.selectors.total === 0 ? 0 : selectorsAboveMode / result.selectors.total,
|
|
17
|
-
actuals: result.selectors.complexity.items,
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
if (selectorsAboveMode > result.selectors.total * 0.1) {
|
|
21
|
-
const score = Math.floor(selectorsAboveMode * 0.01)
|
|
22
|
-
outcome.score = Math.min(10, score)
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
return outcome
|
|
26
|
-
},
|
|
27
|
-
|
|
28
|
-
// Specificity per Selector should not differ too much from the most common Specificity
|
|
29
|
-
/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result */
|
|
30
|
-
result => {
|
|
31
|
-
const mode = result.selectors.specificity.mode
|
|
32
|
-
/** @type {import('@projectwallace/css-analyzer').Specificity[]} */
|
|
33
|
-
// @ts-expect-error css-analyzer type is incorrect
|
|
34
|
-
const items = result.selectors.specificity.items
|
|
35
|
-
const selectorsAboveMode = items.filter(c => compareSpecificity(c, mode) < 0)
|
|
36
|
-
.length
|
|
37
|
-
|
|
38
|
-
const outcome = {
|
|
39
|
-
id: 'MoreThanMostCommonSelectorSpecificity',
|
|
40
|
-
score: 0,
|
|
41
|
-
value: result.selectors.total === 0 ? 0 : selectorsAboveMode / result.selectors.total,
|
|
42
|
-
actuals: result.selectors.specificity.items,
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
if (selectorsAboveMode > result.selectors.total * 0.1) {
|
|
46
|
-
const score = Math.floor(selectorsAboveMode * 0.01)
|
|
47
|
-
outcome.score = Math.min(10, score)
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
return outcome
|
|
51
|
-
},
|
|
52
|
-
|
|
53
|
-
// Maximum Selector Complexity should be low
|
|
54
|
-
/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result */
|
|
55
|
-
result => {
|
|
56
|
-
const MAX_SELECTOR_COMPLEXITY = 5
|
|
57
|
-
const actual = result.selectors.complexity.max || 0
|
|
58
|
-
|
|
59
|
-
const outcome = {
|
|
60
|
-
id: 'MaxSelectorComplexity',
|
|
61
|
-
score: 0,
|
|
62
|
-
value: result.selectors.complexity.max,
|
|
63
|
-
actuals: result.selectors.complexity.items,
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
// Deduct 0.5 points per complexity over 5, up to 5 points
|
|
67
|
-
if (actual > MAX_SELECTOR_COMPLEXITY) {
|
|
68
|
-
const score = Math.ceil((actual - MAX_SELECTOR_COMPLEXITY) * 0.5)
|
|
69
|
-
outcome.score = Math.min(5, score)
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
return outcome
|
|
73
|
-
},
|
|
74
|
-
|
|
75
|
-
// Average Selector Complexity should be low
|
|
76
|
-
/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result */
|
|
77
|
-
result => {
|
|
78
|
-
const ALLOWED_COMPLEXITY = 2
|
|
79
|
-
const actual = result.selectors.complexity.mean
|
|
80
|
-
|
|
81
|
-
const outcome = {
|
|
82
|
-
id: 'AverageSelectorComplexity',
|
|
83
|
-
score: 0,
|
|
84
|
-
value: actual,
|
|
85
|
-
actuals: result.selectors.complexity.items,
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
// Deduct 2 points per selector over 2
|
|
89
|
-
if (actual > ALLOWED_COMPLEXITY) {
|
|
90
|
-
const score = Math.ceil((actual - ALLOWED_COMPLEXITY) * 2)
|
|
91
|
-
outcome.score = Math.min(10, score)
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
return outcome
|
|
95
|
-
},
|
|
96
|
-
|
|
97
|
-
/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result */
|
|
98
|
-
result => {
|
|
99
|
-
const ALLOWED = 0.01
|
|
100
|
-
const actual = result.selectors.id.ratio
|
|
101
|
-
const outcome = {
|
|
102
|
-
id: 'IdSelectorRatio',
|
|
103
|
-
score: 0,
|
|
104
|
-
value: actual,
|
|
105
|
-
actuals: Object.keys(result.selectors.id.unique)
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
if (actual > ALLOWED) {
|
|
109
|
-
const score = Math.floor((actual - ALLOWED) * 10)
|
|
110
|
-
outcome.score = Math.min(score, 5)
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
return outcome
|
|
114
|
-
},
|
|
115
|
-
|
|
116
|
-
/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result */
|
|
117
|
-
result => {
|
|
118
|
-
const ALLOWED = 0.01
|
|
119
|
-
const actual = result.declarations.importants.ratio
|
|
120
|
-
const outcome = {
|
|
121
|
-
id: 'ImportantRatio',
|
|
122
|
-
score: 0,
|
|
123
|
-
value: actual,
|
|
124
|
-
actuals: result.declarations.importants.total,
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
if (actual > ALLOWED) {
|
|
128
|
-
const score = Math.floor((actual - ALLOWED) * 10)
|
|
129
|
-
outcome.score = Math.min(score, 5)
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
return outcome
|
|
133
|
-
},
|
|
134
|
-
]
|
package/src/core.js
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { guards as performanceGuards } from './performance.js'
|
|
2
|
-
import { guards as maintainabilityGuards } from './maintainability.js'
|
|
3
|
-
import { guards as complexityGuards } from './complexity.js'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result
|
|
7
|
-
* @param {performanceGuards | maintainabilityGuards | complexityGuards} guards
|
|
8
|
-
*/
|
|
9
|
-
function calculateScore(result, guards) {
|
|
10
|
-
let score = 100
|
|
11
|
-
let violations = []
|
|
12
|
-
let passes = []
|
|
13
|
-
|
|
14
|
-
for (const guard of guards) {
|
|
15
|
-
const outcome = guard(result)
|
|
16
|
-
|
|
17
|
-
if (outcome.score > 0) {
|
|
18
|
-
score -= outcome.score
|
|
19
|
-
violations.push(outcome)
|
|
20
|
-
} else {
|
|
21
|
-
passes.push(outcome)
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
return {
|
|
26
|
-
score: Math.max(score, 0),
|
|
27
|
-
violations,
|
|
28
|
-
passes,
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} analysis */
|
|
33
|
-
export function calculate(analysis) {
|
|
34
|
-
const performance = calculateScore(analysis, performanceGuards)
|
|
35
|
-
const maintainability = calculateScore(analysis, maintainabilityGuards)
|
|
36
|
-
const complexity = calculateScore(analysis, complexityGuards)
|
|
37
|
-
|
|
38
|
-
return {
|
|
39
|
-
violations: performance.violations
|
|
40
|
-
.concat(maintainability.violations)
|
|
41
|
-
.concat(complexity.violations),
|
|
42
|
-
passes: performance.passes
|
|
43
|
-
.concat(maintainability.passes)
|
|
44
|
-
.concat(complexity.passes),
|
|
45
|
-
performance,
|
|
46
|
-
maintainability,
|
|
47
|
-
complexity,
|
|
48
|
-
}
|
|
49
|
-
}
|
package/src/index.js
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { analyze } from '@projectwallace/css-analyzer'
|
|
2
|
-
import { calculate as calculateFromAnalysis } from './core.js'
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* @param {string} css
|
|
6
|
-
*/
|
|
7
|
-
export function calculate(css) {
|
|
8
|
-
const analysis = analyze(css)
|
|
9
|
-
return calculateFromAnalysis(analysis)
|
|
10
|
-
}
|
package/src/maintainability.js
DELETED
|
@@ -1,159 +0,0 @@
|
|
|
1
|
-
export const guards = [
|
|
2
|
-
|
|
3
|
-
// Source Lines of Code should be low
|
|
4
|
-
/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result */
|
|
5
|
-
result => {
|
|
6
|
-
const outcome = {
|
|
7
|
-
id: 'SourceLinesOfCode',
|
|
8
|
-
score: 0,
|
|
9
|
-
value: result.stylesheet.sourceLinesOfCode,
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
if (result.stylesheet.sourceLinesOfCode > 10000) {
|
|
13
|
-
// deduct 1 point per 1000 lines of code over 10,000
|
|
14
|
-
const score = Math.floor((result.stylesheet.sourceLinesOfCode - 10000) / 1000)
|
|
15
|
-
outcome.score = Math.min(15, score)
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
return outcome
|
|
19
|
-
},
|
|
20
|
-
|
|
21
|
-
// Average count of Selectors per RuleSet should be low
|
|
22
|
-
/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result */
|
|
23
|
-
result => {
|
|
24
|
-
const ALLOWED_SELECTORS_PER_RULESET = 2
|
|
25
|
-
const actual = result.rules.selectors.mean
|
|
26
|
-
|
|
27
|
-
const outcome = {
|
|
28
|
-
id: 'AverageSelectorsPerRule',
|
|
29
|
-
score: 0,
|
|
30
|
-
value: actual,
|
|
31
|
-
actuals: result.rules.selectors.items,
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
// Deduct 5 points per selector over 2
|
|
35
|
-
if (actual > ALLOWED_SELECTORS_PER_RULESET) {
|
|
36
|
-
const score = Math.floor((actual - ALLOWED_SELECTORS_PER_RULESET) * 5)
|
|
37
|
-
outcome.score = Math.min(15, score)
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
return outcome
|
|
41
|
-
},
|
|
42
|
-
|
|
43
|
-
// Average count of Declarations per RuleSet should be low
|
|
44
|
-
/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result */
|
|
45
|
-
result => {
|
|
46
|
-
const ALLOWED_DECLARATIONS_PER_RULESET = 5
|
|
47
|
-
|
|
48
|
-
const outcome = {
|
|
49
|
-
id: 'AverageDeclarationsPerRule',
|
|
50
|
-
score: 0,
|
|
51
|
-
value: result.rules.declarations.mean,
|
|
52
|
-
actuals: result.rules.declarations.items,
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
// Deduct 5 points per declaration over 5
|
|
56
|
-
if (result.rules.declarations.mean > ALLOWED_DECLARATIONS_PER_RULESET) {
|
|
57
|
-
const score = Math.floor((result.rules.declarations.mean - ALLOWED_DECLARATIONS_PER_RULESET) * 5)
|
|
58
|
-
outcome.score = Math.min(15, score)
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
return outcome
|
|
62
|
-
},
|
|
63
|
-
|
|
64
|
-
// Max number of Selectors per Rule should be low
|
|
65
|
-
/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result */
|
|
66
|
-
result => {
|
|
67
|
-
const MAX_SELECTORS_PER_RULESET = 10
|
|
68
|
-
let max = result.rules.selectors.max || 0
|
|
69
|
-
|
|
70
|
-
const outcome = {
|
|
71
|
-
id: 'MaxSelectorsPerRule',
|
|
72
|
-
score: 0,
|
|
73
|
-
value: max,
|
|
74
|
-
actuals: result.rules.selectors.items,
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
// Deduct 0.5 points per selectors over 10
|
|
78
|
-
if (max > MAX_SELECTORS_PER_RULESET) {
|
|
79
|
-
const score = Math.ceil((max - MAX_SELECTORS_PER_RULESET) * 0.5)
|
|
80
|
-
outcome.score = Math.min(score, 15)
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
return outcome
|
|
84
|
-
},
|
|
85
|
-
|
|
86
|
-
// Max number of Declarations per Rule should be low
|
|
87
|
-
/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result */
|
|
88
|
-
result => {
|
|
89
|
-
const MAX_DECLARATIONS_PER_RULESET = 10
|
|
90
|
-
const max = result.rules.declarations.max || 0
|
|
91
|
-
|
|
92
|
-
const outcome = {
|
|
93
|
-
id: 'MaxDeclarationsPerRule',
|
|
94
|
-
score: 0,
|
|
95
|
-
value: max,
|
|
96
|
-
actuals: result.rules.declarations.items,
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
// Deduct 0.5 points per declarations over 10
|
|
100
|
-
if (max > MAX_DECLARATIONS_PER_RULESET) {
|
|
101
|
-
const score = Math.ceil((max - MAX_DECLARATIONS_PER_RULESET) * 0.5)
|
|
102
|
-
outcome.score = Math.min(15, score)
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
return outcome
|
|
106
|
-
},
|
|
107
|
-
|
|
108
|
-
// Number of Selectors per RuleSet should not differ too much from the most common amount of
|
|
109
|
-
// Selectors per RuleSet
|
|
110
|
-
/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result */
|
|
111
|
-
result => {
|
|
112
|
-
const mode = result.rules.selectors.mode
|
|
113
|
-
const rulesHavingMoreThanMode = result.rules.selectors.items
|
|
114
|
-
.filter(item => item > mode)
|
|
115
|
-
.length
|
|
116
|
-
|
|
117
|
-
const outcome = {
|
|
118
|
-
id: 'MoreThanMostCommonSelectorsPerRule',
|
|
119
|
-
score: 0,
|
|
120
|
-
value: result.rules.selectors.mode,
|
|
121
|
-
actuals: result.rules.selectors.items,
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
// if more than 10% of RuleSets has more Selectors than most common:
|
|
125
|
-
if (rulesHavingMoreThanMode > result.rules.total * 0.1) {
|
|
126
|
-
// then deduct 0.01 for ever applicable RuleSet
|
|
127
|
-
const score = Math.floor(rulesHavingMoreThanMode * 0.01)
|
|
128
|
-
// with a maximum of 10 points
|
|
129
|
-
outcome.score = Math.min(15, score)
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
return outcome
|
|
133
|
-
},
|
|
134
|
-
|
|
135
|
-
// Number of Declarations per RuleSet should not differ too much from the most common amount of
|
|
136
|
-
// Declarations per RuleSet
|
|
137
|
-
/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result */
|
|
138
|
-
result => {
|
|
139
|
-
const mode = result.rules.selectors.mode
|
|
140
|
-
const rulesHavingMoreThanMode = result.rules.declarations.items.filter(item => item > mode).length
|
|
141
|
-
|
|
142
|
-
const outcome = {
|
|
143
|
-
id: 'MoreThanMostCommonDeclarationsPerRule',
|
|
144
|
-
score: 0,
|
|
145
|
-
value: result.rules.declarations.mode,
|
|
146
|
-
actuals: result.rules.declarations.items,
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
// if more than 10% of RuleSets has more Declarations than most common:
|
|
150
|
-
if (rulesHavingMoreThanMode > result.rules.total * 0.1) {
|
|
151
|
-
// then deduct 0.01 for ever applicable RuleSet
|
|
152
|
-
const score = Math.floor(rulesHavingMoreThanMode * 0.01)
|
|
153
|
-
// with a maximum of 10 points
|
|
154
|
-
outcome.score = Math.min(15, score)
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
return outcome
|
|
158
|
-
},
|
|
159
|
-
]
|
package/src/performance.js
DELETED
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
export const guards = [
|
|
2
|
-
|
|
3
|
-
// Should not contain @import rules
|
|
4
|
-
/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result */
|
|
5
|
-
result => ({
|
|
6
|
-
id: 'Imports',
|
|
7
|
-
score: result.atrules.import.total * 10,
|
|
8
|
-
value: result.atrules.import.total,
|
|
9
|
-
actuals: Object.keys(result.atrules.import.unique),
|
|
10
|
-
}),
|
|
11
|
-
|
|
12
|
-
// Should not contain empty rules
|
|
13
|
-
/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result */
|
|
14
|
-
result => ({
|
|
15
|
-
id: 'EmptyRules',
|
|
16
|
-
score: result.rules.empty.total,
|
|
17
|
-
value: result.rules.empty.total,
|
|
18
|
-
}),
|
|
19
|
-
|
|
20
|
-
// Too many selectors appear multiple times
|
|
21
|
-
/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result */
|
|
22
|
-
result => {
|
|
23
|
-
const outcome = {
|
|
24
|
-
id: 'SelectorDuplications',
|
|
25
|
-
score: 0,
|
|
26
|
-
value: 1 - result.selectors.uniquenessRatio,
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
if (result.selectors.uniquenessRatio < 0.66) {
|
|
30
|
-
outcome.score = Math.floor((1 - result.selectors.uniquenessRatio) * 10)
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
return outcome
|
|
34
|
-
},
|
|
35
|
-
|
|
36
|
-
// Too many declarations appear multiple times
|
|
37
|
-
/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result */
|
|
38
|
-
result => {
|
|
39
|
-
const outcome = {
|
|
40
|
-
id: 'DeclarationDuplications',
|
|
41
|
-
score: 0,
|
|
42
|
-
value: 1 - result.declarations.uniquenessRatio,
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
if (result.declarations.uniquenessRatio < 0.66) {
|
|
46
|
-
outcome.score = Math.floor((1 - result.declarations.uniquenessRatio) * 10)
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
return outcome
|
|
50
|
-
},
|
|
51
|
-
|
|
52
|
-
// The total amount of CSS should not be too high
|
|
53
|
-
/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result */
|
|
54
|
-
result => ({
|
|
55
|
-
id: 'CssSize',
|
|
56
|
-
score: result.stylesheet.size > 200_000 ? 5 : 0,
|
|
57
|
-
value: result.stylesheet.size,
|
|
58
|
-
}),
|
|
59
|
-
|
|
60
|
-
// Should not contain (too much) comments
|
|
61
|
-
// Deduct 1 point for every 250 bytes
|
|
62
|
-
/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result */
|
|
63
|
-
result => {
|
|
64
|
-
const { comments } = result.stylesheet
|
|
65
|
-
return {
|
|
66
|
-
id: 'TooMuchComments',
|
|
67
|
-
score: Math.min(10, Math.floor(comments.size / 250)),
|
|
68
|
-
value: comments.size,
|
|
69
|
-
}
|
|
70
|
-
},
|
|
71
|
-
|
|
72
|
-
// Should not contain too much embedded content
|
|
73
|
-
// Deduct 1 point for every 250 bytes
|
|
74
|
-
/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result */
|
|
75
|
-
(result) => {
|
|
76
|
-
const { size } = result.stylesheet.embeddedContent
|
|
77
|
-
return {
|
|
78
|
-
id: 'TooMuchEmbeddedContent',
|
|
79
|
-
score: Math.min(20, Math.floor(size.total / 250)),
|
|
80
|
-
value: size.total,
|
|
81
|
-
}
|
|
82
|
-
},
|
|
83
|
-
]
|