Skip to content

feat: make nesting available as countableCollection as well #469

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 1 commit into from
Jun 17, 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
9 changes: 8 additions & 1 deletion src/atrules/atrules.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,14 @@ AtRules('tracks nesting depth', () => {
mode: 1,
range: 1,
sum: 2,
items: [1, 0, 1]
items: [1, 0, 1],
total: 3,
totalUnique: 2,
unique: {
0: 1,
1: 2,
},
uniquenessRatio: 2 / 3,
}
assert.equal(actual, expected)
})
Expand Down
10 changes: 9 additions & 1 deletion src/declarations/declarations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,15 @@ Declarations('tracks nesting depth', () => {
mode: 0,
range: 2,
sum: 4,
items: [0, 0, 1, 0, 1, 2]
items: [0, 0, 1, 0, 1, 2],
total: 6,
totalUnique: 3,
unique: {
0: 3,
1: 2,
2: 1,
},
uniquenessRatio: 3 / 6,
}
assert.equal(actual, expected)
})
Expand Down
20 changes: 16 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ export function analyze(css, options = {}) {
let containerNames = new Collection(useLocations)
let registeredProperties = new Collection(useLocations)
let atruleNesting = new AggregateCollection()
let uniqueAtruleNesting = new Collection(useLocations)

// Rules
let totalRules = 0
Expand All @@ -140,6 +141,7 @@ export function analyze(css, options = {}) {
let uniqueSelectorsPerRule = new Collection(useLocations)
let uniqueDeclarationsPerRule = new Collection(useLocations)
let ruleNesting = new AggregateCollection()
let uniqueRuleNesting = new Collection(useLocations)

// Selectors
let keyframeSelectors = new Collection(useLocations)
Expand All @@ -162,6 +164,7 @@ export function analyze(css, options = {}) {
let pseudoClasses = new Collection(useLocations)
let combinators = new Collection(useLocations)
let selectorNesting = new AggregateCollection()
let uniqueSelectorNesting = new Collection(useLocations)

// Declarations
let uniqueDeclarations = new Set()
Expand All @@ -171,6 +174,7 @@ export function analyze(css, options = {}) {
let importantsInKeyframes = 0
let importantCustomProperties = new Collection(useLocations)
let declarationNesting = new AggregateCollection()
let uniqueDeclarationNesting = new Collection(useLocations)

// Properties
let properties = new Collection(useLocations)
Expand Down Expand Up @@ -207,6 +211,7 @@ export function analyze(css, options = {}) {
case Atrule: {
totalAtRules++
atruleNesting.push(nestingDepth)
uniqueAtruleNesting.p(nestingDepth, node.loc)

let atRuleName = node.name

Expand Down Expand Up @@ -318,6 +323,7 @@ export function analyze(css, options = {}) {
declarationsPerRule.push(numDeclarations)
uniqueDeclarationsPerRule.p(numDeclarations, block.loc)
ruleNesting.push(nestingDepth)
uniqueRuleNesting.p(nestingDepth, node.loc)

totalRules++

Expand Down Expand Up @@ -355,6 +361,7 @@ export function analyze(css, options = {}) {
selectorComplexities.push(complexity)
uniqueSelectorComplexities.p(complexity, node.loc)
selectorNesting.push(nestingDepth - 1)
uniqueSelectorNesting.p(nestingDepth - 1, node.loc)

// #region specificity
let specificity = calculateForAST(node).toArray()
Expand Down Expand Up @@ -713,6 +720,7 @@ export function analyze(css, options = {}) {

uniqueDeclarations.add(stringifyNode(node))
declarationNesting.push(nestingDepth - 1)
uniqueDeclarationNesting.p(nestingDepth - 1, node.loc)

if (node.important === true) {
importantDeclarations++
Expand Down Expand Up @@ -856,7 +864,8 @@ export function analyze(css, options = {}) {
atruleNesting.aggregate(),
{
items: atruleNesting.toArray(),
}
},
uniqueAtruleNesting.c(),
),
},
rules: {
Expand All @@ -876,7 +885,8 @@ export function analyze(css, options = {}) {
ruleNesting.aggregate(),
{
items: ruleNesting.toArray(),
}
},
uniqueRuleNesting.c(),
),
selectors: assign(
selectorsPerRule.aggregate(),
Expand Down Expand Up @@ -925,7 +935,8 @@ export function analyze(css, options = {}) {
selectorNesting.aggregate(),
{
items: selectorNesting.toArray(),
}
},
uniqueSelectorNesting.c(),
),
id: assign(
ids.c(), {
Expand Down Expand Up @@ -962,7 +973,8 @@ export function analyze(css, options = {}) {
declarationNesting.aggregate(),
{
items: declarationNesting.toArray(),
}
},
uniqueDeclarationNesting.c(),
),
},
properties: assign(
Expand Down
16 changes: 16 additions & 0 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ Api("handles empty input gracefully", () => {
range: 0,
sum: 0,
items: [],
total: 0,
totalUnique: 0,
unique: {},
uniquenessRatio: 0,
}
},
rules: {
Expand Down Expand Up @@ -218,6 +222,10 @@ Api("handles empty input gracefully", () => {
range: 0,
sum: 0,
items: [],
total: 0,
totalUnique: 0,
unique: {},
uniquenessRatio: 0,
},
selectors: {
min: 0,
Expand Down Expand Up @@ -283,6 +291,10 @@ Api("handles empty input gracefully", () => {
range: 0,
sum: 0,
items: [],
total: 0,
totalUnique: 0,
unique: {},
uniquenessRatio: 0,
},
id: {
total: 0,
Expand Down Expand Up @@ -352,6 +364,10 @@ Api("handles empty input gracefully", () => {
range: 0,
sum: 0,
items: [],
total: 0,
totalUnique: 0,
unique: {},
uniquenessRatio: 0,
}
},
properties: {
Expand Down
14 changes: 13 additions & 1 deletion src/rules/rules.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ Rules('should handle CSS without rules', () => {
range: 0,
sum: 0,
items: [],
total: 0,
totalUnique: 0,
unique: {},
uniquenessRatio: 0,
}
}
assert.equal(actual.rules, expected)
Expand Down Expand Up @@ -441,7 +445,15 @@ Rules('tracks nesting depth', () => {
mode: 0,
range: 2,
sum: 3,
items: [0, 0, 1, 2]
items: [0, 0, 1, 2],
total: 4,
totalUnique: 3,
unique: {
0: 2,
1: 1,
2: 1,
},
uniquenessRatio: 3 / 4,
}
assert.equal(actual, expected)
})
Expand Down
20 changes: 19 additions & 1 deletion src/selectors/selectors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ Selectors('handles CSS without selectors', () => {
range: 0,
sum: 0,
items: [],
total: 0,
totalUnique: 0,
unique: {},
uniquenessRatio: 0,
},
}
assert.equal(actual, expected)
Expand Down Expand Up @@ -368,6 +372,12 @@ Selectors('handles emoji selectors', () => {
range: 0,
sum: 0,
items: [0],
total: 1,
totalUnique: 1,
unique: {
0: 1,
},
uniquenessRatio: 1 / 1,
},
}
assert.equal(actual, expected)
Expand Down Expand Up @@ -529,7 +539,15 @@ Selectors('tracks nesting depth', () => {
mode: 0,
range: 2,
sum: 3,
items: [0, 0, 1, 2]
items: [0, 0, 1, 2],
total: 4,
totalUnique: 3,
unique: {
0: 2,
1: 1,
2: 1,
},
uniquenessRatio: 3 / 4,
}
assert.equal(actual, expected)
})
Expand Down