Skip to content

Commit e679290

Browse files
authored
Add type checking (#25)
closes #21
1 parent 95b35df commit e679290

File tree

5 files changed

+24
-4
lines changed

5 files changed

+24
-4
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
"types": "./dist/index.d.ts",
4242
"scripts": {
4343
"test": "uvu",
44-
"build": "vite build"
44+
"build": "vite build",
45+
"check": "tsc --noEmit"
4546
},
4647
"dependencies": {
4748
"@projectwallace/css-analyzer": "^5.14.0"

src/complexity.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { compareSpecificity } from '@projectwallace/css-analyzer'
33
export const guards = [
44

55
// Complexity per Selector should not differ too much from the most common Complexity
6+
/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result */
67
result => {
78
const mode = result.selectors.complexity.mode
89
const selectorsAboveMode = result.selectors.complexity.items
@@ -25,6 +26,7 @@ export const guards = [
2526
},
2627

2728
// Specificity per Selector should not differ too much from the most common Specificity
29+
/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result */
2830
result => {
2931
const mode = result.selectors.specificity.mode
3032
const selectorsAboveMode = result.selectors.specificity.items
@@ -47,6 +49,7 @@ export const guards = [
4749
},
4850

4951
// Maximum Selector Complexity should be low
52+
/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result */
5053
result => {
5154
const MAX_SELECTOR_COMPLEXITY = 5
5255
const actual = result.selectors.complexity.max
@@ -68,6 +71,7 @@ export const guards = [
6871
},
6972

7073
// Average Selector Complexity should be low
74+
/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result */
7175
result => {
7276
const ALLOWED_COMPLEXITY = 2
7377
const actual = result.selectors.complexity.mean
@@ -88,6 +92,7 @@ export const guards = [
8892
return outcome
8993
},
9094

95+
/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result */
9196
result => {
9297
const ALLOWED = 0.01
9398
const actual = result.selectors.id.ratio
@@ -106,6 +111,7 @@ export const guards = [
106111
return outcome
107112
},
108113

114+
/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result */
109115
result => {
110116
const ALLOWED = 0.01
111117
const actual = result.declarations.importants.ratio

src/core.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@ import { guards as complexityGuards } from './complexity.js'
44

55
/**
66
* @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result
7-
* @param {Array<function(ReturnType<import('@projectwallace/css-analyzer').analyze>): {score: number, value: number, id: string, actuals?: unknown}>} guards
7+
* @param {performanceGuards | maintainabilityGuards | complexityGuards} guards
88
*/
99
function calculateScore(result, guards) {
1010
let score = 100
1111
let violations = []
1212
let passes = []
1313

1414
for (const guard of guards) {
15-
/** @type {{score: number, value: number, id: string}} */
1615
const outcome = guard(result)
1716

1817
if (outcome.score > 0) {

src/maintainability.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export const guards = [
22

3-
// Source Lines of Code should be low'
3+
// Source Lines of Code should be low
4+
/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result */
45
result => {
56
const outcome = {
67
id: 'SourceLinesOfCode',
@@ -18,6 +19,7 @@ export const guards = [
1819
},
1920

2021
// Average count of Selectors per RuleSet should be low
22+
/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result */
2123
result => {
2224
const ALLOWED_SELECTORS_PER_RULESET = 2
2325
const actual = result.rules.selectors.mean
@@ -39,6 +41,7 @@ export const guards = [
3941
},
4042

4143
// Average count of Declarations per RuleSet should be low
44+
/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result */
4245
result => {
4346
const ALLOWED_DECLARATIONS_PER_RULESET = 5
4447

@@ -59,6 +62,7 @@ export const guards = [
5962
},
6063

6164
// Max number of Selectors per Rule should be low
65+
/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result */
6266
result => {
6367
const MAX_SELECTORS_PER_RULESET = 10
6468

@@ -79,6 +83,7 @@ export const guards = [
7983
},
8084

8185
// Max number of Declarations per Rule should be low
86+
/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result */
8287
result => {
8388
const MAX_DECLARATIONS_PER_RULESET = 10
8489

@@ -100,6 +105,7 @@ export const guards = [
100105

101106
// Number of Selectors per RuleSet should not differ too much from the most common amount of
102107
// Selectors per RuleSet
108+
/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result */
103109
result => {
104110
const mode = result.rules.selectors.mode
105111
const rulesHavingMoreThanMode = result.rules.selectors.items
@@ -126,6 +132,7 @@ export const guards = [
126132

127133
// Number of Declarations per RuleSet should not differ too much from the most common amount of
128134
// Declarations per RuleSet
135+
/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result */
129136
result => {
130137
const mode = result.rules.selectors.mode
131138
const rulesHavingMoreThanMode = result.rules.declarations.items.filter(item => item > mode).length

src/performance.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export const guards = [
22

33
// Should not contain @import rules
4+
/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result */
45
result => ({
56
id: 'Imports',
67
score: result.atrules.import.total * 10,
@@ -9,13 +10,15 @@ export const guards = [
910
}),
1011

1112
// Should not contain empty rules
13+
/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result */
1214
result => ({
1315
id: 'EmptyRules',
1416
score: result.rules.empty.total,
1517
value: result.rules.empty.total,
1618
}),
1719

1820
// Too many selectors appear multiple times
21+
/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result */
1922
result => {
2023
const outcome = {
2124
id: 'SelectorDuplications',
@@ -31,6 +34,7 @@ export const guards = [
3134
},
3235

3336
// Too many declarations appear multiple times
37+
/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result */
3438
result => {
3539
const outcome = {
3640
id: 'DeclarationDuplications',
@@ -46,6 +50,7 @@ export const guards = [
4650
},
4751

4852
// The total amount of CSS should not be too high
53+
/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result */
4954
result => ({
5055
id: 'CssSize',
5156
score: result.stylesheet.size > 200_000 ? 5 : 0,
@@ -54,6 +59,7 @@ export const guards = [
5459

5560
// Should not contain (too much) comments
5661
// Deduct 1 point for every 250 bytes
62+
/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result */
5763
result => {
5864
const { comments } = result.stylesheet
5965
return {
@@ -65,6 +71,7 @@ export const guards = [
6571

6672
// Should not contain too much embedded content
6773
// Deduct 1 point for every 250 bytes
74+
/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result */
6875
result => {
6976
const { size } = result.stylesheet.embeddedContent
7077
return {

0 commit comments

Comments
 (0)