Skip to content

Commit

Permalink
Add type checking (#25)
Browse files Browse the repository at this point in the history
closes #21
  • Loading branch information
bartveneman authored May 31, 2024
1 parent 95b35df commit e679290
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 4 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"types": "./dist/index.d.ts",
"scripts": {
"test": "uvu",
"build": "vite build"
"build": "vite build",
"check": "tsc --noEmit"
},
"dependencies": {
"@projectwallace/css-analyzer": "^5.14.0"
Expand Down
6 changes: 6 additions & 0 deletions src/complexity.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { compareSpecificity } from '@projectwallace/css-analyzer'
export const guards = [

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

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

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

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

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

/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result */
result => {
const ALLOWED = 0.01
const actual = result.declarations.importants.ratio
Expand Down
3 changes: 1 addition & 2 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import { guards as complexityGuards } from './complexity.js'

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

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

if (outcome.score > 0) {
Expand Down
9 changes: 8 additions & 1 deletion src/maintainability.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export const guards = [

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

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

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

Expand All @@ -59,6 +62,7 @@ export const guards = [
},

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

Expand All @@ -79,6 +83,7 @@ export const guards = [
},

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

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

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

// Number of Declarations per RuleSet should not differ too much from the most common amount of
// Declarations per RuleSet
/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result */
result => {
const mode = result.rules.selectors.mode
const rulesHavingMoreThanMode = result.rules.declarations.items.filter(item => item > mode).length
Expand Down
7 changes: 7 additions & 0 deletions src/performance.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export const guards = [

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

// Should not contain empty rules
/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result */
result => ({
id: 'EmptyRules',
score: result.rules.empty.total,
value: result.rules.empty.total,
}),

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

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

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

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

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

0 comments on commit e679290

Please sign in to comment.