Skip to content
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
52 changes: 52 additions & 0 deletions src/atrules/atrules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,58 @@ test('analyzes @property', () => {
expect(actual).toEqual(expected)
})

test('analyzes @scope', () => {
// Examples from
// https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/At-rules/@scope#examples
// and
// https://drafts.csswg.org/css-cascade-6/#scoped-styles
const fixture = `
@scope (.article-body) to (figure) {}

@scope (.article-body) {}

@scope (.article-body) to (:scope > figure) {}

@scope (.article-body) to (.feature :scope figure) {}

@scope (.media-object) to (.content > *) {}

@scope ([data-scope='main-component']) to ([data-scope]) {}

@scope ([data-scope='main-component']) to ([data-scope] > *) {}

@scope (.parent-scope) {
@scope (:scope > .child-scope) to (:scope .limit) {}
}

@scope (.parent-scope > .child-scope) to (.parent-scope > .child-scope .limit) {}

/* No prelude */
@scope {}
`
const result = analyze(fixture)
const actual = result.atrules.scope
const expected = {
total: 10,
totalUnique: 10,
unique: {
'(.article-body) to (figure)': 1,
'(.article-body)': 1,
'(.article-body) to (:scope > figure)': 1,
'(.article-body) to (.feature :scope figure)': 1,
'(.media-object) to (.content > *)': 1,
"([data-scope='main-component']) to ([data-scope])": 1,
"([data-scope='main-component']) to ([data-scope] > *)": 1,
'(.parent-scope)': 1,
'(:scope > .child-scope) to (:scope .limit)': 1,
'(.parent-scope > .child-scope) to (.parent-scope > .child-scope .limit)': 1,
},
uniquenessRatio: 1,
}

expect(actual).toEqual(expected)
})

test('tracks nesting depth', () => {
const fixture = `
a {
Expand Down
6 changes: 6 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,12 @@ test('handles empty input gracefully', () => {
unique: {},
uniquenessRatio: 0,
},
scope: {
total: 0,
totalUnique: 0,
unique: {},
uniquenessRatio: 0,
},
complexity: {
min: 0,
max: 0,
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ function analyzeInternal<T extends boolean>(css: string, options: Options, useLo
let containers = new Collection(useLocations)
let containerNames = new Collection(useLocations)
let registeredProperties = new Collection(useLocations)
let scopes = new Collection(useLocations)
let atruleNesting = new AggregateCollection()
let uniqueAtruleNesting = new Collection(useLocations)

Expand Down Expand Up @@ -294,6 +295,8 @@ function analyzeInternal<T extends boolean>(css: string, options: Options, useLo
registeredProperties.p(node.prelude.text, toLoc(node))
} else if (normalized_name === 'charset') {
charsets.p(node.prelude.text.toLowerCase(), toLoc(node))
} else if (normalized_name === 'scope') {
scopes.p(node.prelude.text, toLoc(node))
}

atRuleComplexities.push(complexity)
Expand Down Expand Up @@ -858,6 +861,7 @@ function analyzeInternal<T extends boolean>(css: string, options: Options, useLo
}),
layer: layers.c(),
property: registeredProperties.c(),
scope: scopes.c(),
complexity: atRuleComplexity,
nesting: assign(
atruleNesting.aggregate(),
Expand Down