Skip to content

breaking: update css-analyzer to v6, drops actuals for TooMuchEmbeddedContent #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 58 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"check": "tsc --noEmit"
},
"dependencies": {
"@projectwallace/css-analyzer": "^5.14.0"
"@projectwallace/css-analyzer": "^6.0.0"
},
"devDependencies": {
"typescript": "^5.4.5",
Expand Down
8 changes: 5 additions & 3 deletions src/complexity.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ export const guards = [
/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result */
result => {
const mode = result.selectors.specificity.mode
const selectorsAboveMode = result.selectors.specificity.items
.filter(c => compareSpecificity(c, mode) < 0)
/** @type {import('@projectwallace/css-analyzer').Specificity[]} */
// @ts-expect-error css-analyzer type is incorrect
const items = result.selectors.specificity.items
const selectorsAboveMode = items.filter(c => compareSpecificity(c, mode) < 0)
.length

const outcome = {
Expand All @@ -52,7 +54,7 @@ export const guards = [
/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result */
result => {
const MAX_SELECTOR_COMPLEXITY = 5
const actual = result.selectors.complexity.max
const actual = result.selectors.complexity.max || 0

const outcome = {
id: 'MaxSelectorComplexity',
Expand Down
4 changes: 1 addition & 3 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ function calculateScore(result, guards) {
}
}

/**
* @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} analysis
*/
/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} analysis */
export function calculate(analysis) {
const performance = calculateScore(analysis, performanceGuards)
const maintainability = calculateScore(analysis, maintainabilityGuards)
Expand Down
2 changes: 0 additions & 2 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ Index('smoke test', () => {
"id": "TooMuchEmbeddedContent",
"score": 0,
"value": 0,
"actuals": []
},
{
"id": "SourceLinesOfCode",
Expand Down Expand Up @@ -235,7 +234,6 @@ Index('smoke test', () => {
"id": "TooMuchEmbeddedContent",
"score": 0,
"value": 0,
"actuals": []
}
]
},
Expand Down
14 changes: 8 additions & 6 deletions src/maintainability.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,18 @@ export const guards = [
/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result */
result => {
const MAX_SELECTORS_PER_RULESET = 10
let max = result.rules.selectors.max || 0

const outcome = {
id: 'MaxSelectorsPerRule',
score: 0,
value: result.rules.selectors.max,
value: max,
actuals: result.rules.selectors.items,
}

// Deduct 0.5 points per selectors over 10
if (result.rules.selectors.max > MAX_SELECTORS_PER_RULESET) {
const score = Math.ceil((result.rules.selectors.max - MAX_SELECTORS_PER_RULESET) * 0.5)
if (max > MAX_SELECTORS_PER_RULESET) {
const score = Math.ceil((max - MAX_SELECTORS_PER_RULESET) * 0.5)
outcome.score = Math.min(score, 15)
}

Expand All @@ -86,17 +87,18 @@ export const guards = [
/** @param {ReturnType<import('@projectwallace/css-analyzer').analyze>} result */
result => {
const MAX_DECLARATIONS_PER_RULESET = 10
const max = result.rules.declarations.max || 0

const outcome = {
id: 'MaxDeclarationsPerRule',
score: 0,
value: result.rules.declarations.max,
value: max,
actuals: result.rules.declarations.items,
}

// Deduct 0.5 points per declarations over 10
if (result.rules.declarations.max > MAX_DECLARATIONS_PER_RULESET) {
const score = Math.ceil((result.rules.declarations.max - MAX_DECLARATIONS_PER_RULESET) * 0.5)
if (max > MAX_DECLARATIONS_PER_RULESET) {
const score = Math.ceil((max - MAX_DECLARATIONS_PER_RULESET) * 0.5)
outcome.score = Math.min(15, score)
}

Expand Down
9 changes: 4 additions & 5 deletions src/performance.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ export const guards = [
const outcome = {
id: 'DeclarationDuplications',
score: 0,
value: 1 - result.declarations.unique.ratio,
value: 1 - result.declarations.uniquenessRatio,
}

if (result.declarations.unique.ratio < 0.66) {
outcome.score = Math.floor((1 - result.declarations.unique.ratio) * 10)
if (result.declarations.uniquenessRatio < 0.66) {
outcome.score = Math.floor((1 - result.declarations.uniquenessRatio) * 10)
}

return outcome
Expand Down Expand Up @@ -72,13 +72,12 @@ 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 => {
(result) => {
const { size } = result.stylesheet.embeddedContent
return {
id: 'TooMuchEmbeddedContent',
score: Math.min(20, Math.floor(size.total / 250)),
value: size.total,
actuals: Object.keys(result.stylesheet.embeddedContent.unique),
}
},
]
1 change: 0 additions & 1 deletion src/performance.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ Performance('deducts points for having embedded content', () => {
id: 'TooMuchEmbeddedContent',
score: 20,
value: 45990,
actuals: Array.from({ length: 100 }).fill('').map((_, index) => generateEmbed(index)),
},
])
})
Expand Down