Skip to content

Commit

Permalink
Analyze @property atrules #339 (#341)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartveneman authored Sep 6, 2023
1 parent 2eb0ee5 commit 23b1a6e
Show file tree
Hide file tree
Showing 14 changed files with 145 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/__fixtures__/bol-com-20190617.json
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,12 @@
"totalUnique": 0,
"unique": {},
"uniquenessRatio": 0
},
"property": {
"total": 0,
"totalUnique": 0,
"unique": {},
"uniquenessRatio": 0
}
},
"rules": {
Expand Down
6 changes: 6 additions & 0 deletions src/__fixtures__/bootstrap-5.0.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@
"totalUnique": 0,
"unique": {},
"uniquenessRatio": 0
},
"property": {
"total": 0,
"totalUnique": 0,
"unique": {},
"uniquenessRatio": 0
}
},
"rules": {
Expand Down
6 changes: 6 additions & 0 deletions src/__fixtures__/cnn-20220403.json
Original file line number Diff line number Diff line change
Expand Up @@ -1075,6 +1075,12 @@
"totalUnique": 0,
"unique": {},
"uniquenessRatio": 0
},
"property": {
"total": 0,
"totalUnique": 0,
"unique": {},
"uniquenessRatio": 0
}
},
"rules": {
Expand Down
6 changes: 6 additions & 0 deletions src/__fixtures__/css-tricks-20190319.json
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@
"totalUnique": 0,
"unique": {},
"uniquenessRatio": 0
},
"property": {
"total": 0,
"totalUnique": 0,
"unique": {},
"uniquenessRatio": 0
}
},
"rules": {
Expand Down
6 changes: 6 additions & 0 deletions src/__fixtures__/facebook-20190319.json
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@
"totalUnique": 0,
"unique": {},
"uniquenessRatio": 0
},
"property": {
"total": 0,
"totalUnique": 0,
"unique": {},
"uniquenessRatio": 0
}
},
"rules": {
Expand Down
6 changes: 6 additions & 0 deletions src/__fixtures__/gazelle-20210905.json
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,12 @@
"totalUnique": 0,
"unique": {},
"uniquenessRatio": 0
},
"property": {
"total": 0,
"totalUnique": 0,
"unique": {},
"uniquenessRatio": 0
}
},
"rules": {
Expand Down
6 changes: 6 additions & 0 deletions src/__fixtures__/github-20210501.json
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@
"totalUnique": 0,
"unique": {},
"uniquenessRatio": 0
},
"property": {
"total": 0,
"totalUnique": 0,
"unique": {},
"uniquenessRatio": 0
}
},
"rules": {
Expand Down
6 changes: 6 additions & 0 deletions src/__fixtures__/indiatimes-20230219.json
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@
"totalUnique": 0,
"unique": {},
"uniquenessRatio": 0
},
"property": {
"total": 0,
"totalUnique": 0,
"unique": {},
"uniquenessRatio": 0
}
},
"rules": {
Expand Down
6 changes: 6 additions & 0 deletions src/__fixtures__/lego-20190617.json
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,12 @@
"totalUnique": 0,
"unique": {},
"uniquenessRatio": 0
},
"property": {
"total": 0,
"totalUnique": 0,
"unique": {},
"uniquenessRatio": 0
}
},
"rules": {
Expand Down
6 changes: 6 additions & 0 deletions src/__fixtures__/smashing-magazine-20190319.json
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,12 @@
"totalUnique": 0,
"unique": {},
"uniquenessRatio": 0
},
"property": {
"total": 0,
"totalUnique": 0,
"unique": {},
"uniquenessRatio": 0
}
},
"rules": {
Expand Down
6 changes: 6 additions & 0 deletions src/__fixtures__/trello-20190617.json
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,12 @@
"totalUnique": 0,
"unique": {},
"uniquenessRatio": 0
},
"property": {
"total": 0,
"totalUnique": 0,
"unique": {},
"uniquenessRatio": 0
}
},
"rules": {
Expand Down
65 changes: 65 additions & 0 deletions src/atrules/atrules.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -544,4 +544,69 @@ AtRules('analyzes container queries', () => {
assert.equal(actual, expected)
})

AtRules('analyzes @property', () => {
const fixture = `
/* Examples from https://nerdy.dev/cant-break-this-design-system */
@property --focal-size {
syntax: '<length-percentage>';
initial-value: 100%;
inherits: false;
}

@property --hue {
syntax: '<angle>';
initial-value: .5turn;
inherits: false;
}

@property --surface {
syntax: '<color>';
initial-value: #333;
inherits: true;
}

@property --surface-over {
syntax: '<color>';
initial-value: #444;
inherits: true;
}

@property --surface-under {
syntax: '<color>';
initial-value: #222;
inherits: true;
}
/* end nerdy.dev examples */

@media all {
@layer test {
@property --test-dupe {
syntax: '<color>';
inherits: false;
}
@property --test-dupe {
syntax: '<color>';
inherits: false;
}
}
}
`
const actual = analyze(fixture).atrules.property
const expected = {
total: 7,
totalUnique: 6,
unique: {
'--focal-size': 1,
'--hue': 1,
'--surface': 1,
'--surface-over': 1,
'--surface-under': 1,
'--test-dupe': 2,
},
uniquenessRatio: 6 / 7
}

assert.equal(actual, expected)
})

AtRules.run()
8 changes: 8 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export function analyze(css, options = {}) {
let keyframes = new Collection({ useLocations })
let prefixedKeyframes = new Collection({ useLocations })
let containers = new Collection({ useLocations })
let registeredProperties = new Collection({ useLocations })

// Rules
let totalRules = 0
Expand Down Expand Up @@ -218,6 +219,12 @@ export function analyze(css, options = {}) {
prelude
.split(',')
.forEach(name => layers.push(name.trim(), node.prelude.loc))
break
}
if (atRuleName === 'property') {
let prelude = stringifyNode(node.prelude)
registeredProperties.push(prelude, node.prelude.loc)
break
}
break
}
Expand Down Expand Up @@ -628,6 +635,7 @@ export function analyze(css, options = {}) {
}),
container: containers.count(),
layer: layers.count(),
property: registeredProperties.count(),
},
rules: {
total: totalRules,
Expand Down
6 changes: 6 additions & 0 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ Api('handles empty input gracefully', () => {
"totalUnique": 0,
"unique": {},
"uniquenessRatio": 0
},
"property": {
"total": 0,
"totalUnique": 0,
"unique": {},
"uniquenessRatio": 0
}
},
"rules": {
Expand Down

0 comments on commit 23b1a6e

Please sign in to comment.