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
12 changes: 12 additions & 0 deletions src/defined-used.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ describe('Custom properties', () => {
expect(result.used).toEqual(['--color'])
})

test('tracks VAR() references as used regardless of case', () => {
let css = `.foo { color: VAR(--color); }`
let result = analyze(css).properties.custom
expect(result.used).toEqual(['--color'])
})

test('identifies unused custom properties', () => {
let css = `:root { --color: red; --size: 16px; } .foo { color: var(--color); }`
let result = analyze(css).properties.custom
Expand Down Expand Up @@ -384,6 +390,12 @@ describe('Anchor names', () => {
expect(result.used).toContain('--my-anchor')
})

test('tracks ANCHOR() and ANCHOR-SIZE() functions as used regardless of case', () => {
let css = `.positioned { position: absolute; top: ANCHOR(--my-anchor bottom); width: ANCHOR-SIZE(--my-anchor width); }`
let result = analyze(css).properties.anchorNames
expect(result.used).toContain('--my-anchor')
})

test('identifies unused anchor names', () => {
let css = `
.anchor { anchor-name: --tooltip; }
Expand Down
21 changes: 10 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
type CSSNode,
is_custom,
SKIP,
str_equals,
str_starts_with,
walk,
parse,
Expand Down Expand Up @@ -730,15 +731,15 @@ function analyzeInternal<T extends boolean>(css: string, options: Options, useLo
}
} else if (normalizedProperty === 'anchor-name') {
for (let child of value.children) {
if (is_identifier(child) && str_starts_with(child.text, '--')) {
if (is_identifier(child) && is_custom(child.text)) {
anchorNamesTracking.define(child.text)
}
}
} else if (normalizedProperty === 'position-anchor') {
if (
value.first_child &&
is_identifier(value.first_child) &&
str_starts_with(value.first_child.text, '--')
is_custom(value.first_child.text)
) {
anchorNamesTracking.use(value.first_child.text)
}
Expand Down Expand Up @@ -832,21 +833,19 @@ function analyzeInternal<T extends boolean>(css: string, options: Options, useLo
let firstFuncChild = valueNode.first_child

// Track var(--custom-property) usage
if (
funcName === 'var' &&
firstFuncChild &&
is_identifier(firstFuncChild) &&
str_starts_with(firstFuncChild.text, '--')
) {
customPropsTracking.use(firstFuncChild.text)
if (str_equals('var', funcName) && firstFuncChild && is_identifier(firstFuncChild)) {
let name = firstFuncChild.text
if (is_custom(name)) {
customPropsTracking.use(name)
}
}

// Track anchor(--name) and anchor-size(--name) usage
if (
(funcName === 'anchor' || funcName === 'anchor-size') &&
(str_equals('anchor', funcName) || str_equals('anchor-size', funcName)) &&
firstFuncChild &&
is_identifier(firstFuncChild) &&
str_starts_with(firstFuncChild.text, '--')
is_custom(firstFuncChild.text)
) {
anchorNamesTracking.use(firstFuncChild.text)
}
Expand Down
2 changes: 0 additions & 2 deletions src/string-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { str_equals, str_starts_with } from '@projectwallace/css-parser'

export function unquote(str: string): string {
return str.replaceAll(/(?:^['"])|(?:['"]$)/g, '')
}
Expand Down
6 changes: 3 additions & 3 deletions src/values/browserhacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
is_identifier,
is_function,
is_url,
str_equals,
walk,
} from '@projectwallace/css-parser'
import { endsWith, unquote } from '../string-utils.js'
Expand Down Expand Up @@ -48,10 +49,9 @@ export function isValueBrowserhack(node: Value, on_hack: (hack: string) => void)
// alpha(), expression(), and behavior .htc hacks
walk(node, function (child) {
if (is_function(child)) {
const name = child.name.toLowerCase()
if (name === 'alpha') {
if (str_equals('alpha', child.name)) {
on_hack('alpha()')
} else if (name === 'expression') {
} else if (str_equals('expression', child.name)) {
on_hack('expression()')
}
}
Expand Down
Loading