Skip to content

Commit

Permalink
replace obsolete variable to keep track of selector complexities (#234)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartveneman authored Apr 11, 2022
1 parent 8adfde5 commit 932774a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
4 changes: 4 additions & 0 deletions src/aggregate-collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ class AggregateCollection {
this._items.push(item)
}

size() {
return this._items.length
}

aggregate() {
if (this._items.length === 0) {
return {
Expand Down
29 changes: 13 additions & 16 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,9 @@ const analyze = (css) => {
let specificityA = new AggregateCollection()
let specificityB = new AggregateCollection()
let specificityC = new AggregateCollection()
const complexityAggregator = new AggregateCollection()
const selectorComplexities = new AggregateCollection()
/** @type [number,number,number][] */
const specificities = []
/** @type number[] */
const complexities = []
const ids = new CountableCollection()
const a11y = new CountableCollection()

Expand Down Expand Up @@ -233,7 +231,7 @@ const analyze = (css) => {
}

uniqueSelectors.push(selector)
complexityAggregator.add(complexity)
selectorComplexities.add(complexity)

if (maxSpecificity === undefined) {
maxSpecificity = specificity
Expand All @@ -256,7 +254,6 @@ const analyze = (css) => {
}

specificities.push(specificity)
complexities.push(complexity)

// Avoid deeper walking of selectors to not mess with
// our specificity calculations in case of a selector
Expand Down Expand Up @@ -415,11 +412,11 @@ const analyze = (css) => {

const totalUniqueDeclarations = uniqueDeclarations.count()

const totalSelectors = complexities.length
const aggregatesA = specificityA.aggregate()
const aggregatesB = specificityB.aggregate()
const aggregatesC = specificityC.aggregate()
const complexityCount = new CountableCollection(complexities).count()
const totalSelectors = selectorComplexities.size()
const specificitiesA = specificityA.aggregate()
const specificitiesB = specificityB.aggregate()
const specificitiesC = specificityC.aggregate()
const complexityCount = new CountableCollection(selectorComplexities.toArray()).count()
const totalUniqueSelectors = uniqueSelectors.count()

return {
Expand Down Expand Up @@ -481,16 +478,16 @@ const analyze = (css) => {
specificity: {
min: minSpecificity === undefined ? [0, 0, 0] : minSpecificity,
max: maxSpecificity === undefined ? [0, 0, 0] : maxSpecificity,
sum: [aggregatesA.sum, aggregatesB.sum, aggregatesC.sum],
mean: [aggregatesA.mean, aggregatesB.mean, aggregatesC.mean],
mode: [aggregatesA.mode, aggregatesB.mode, aggregatesC.mode],
median: [aggregatesA.median, aggregatesB.median, aggregatesC.median],
sum: [specificitiesA.sum, specificitiesB.sum, specificitiesC.sum],
mean: [specificitiesA.mean, specificitiesB.mean, specificitiesC.mean],
mode: [specificitiesA.mode, specificitiesB.mode, specificitiesC.mode],
median: [specificitiesA.median, specificitiesB.median, specificitiesC.median],
items: specificities
},
complexity: Object.assign(
complexityAggregator.aggregate(),
selectorComplexities.aggregate(),
complexityCount, {
items: complexities
items: selectorComplexities.toArray(),
}),
id: Object.assign(
ids.count(), {
Expand Down

0 comments on commit 932774a

Please sign in to comment.