Back to Biomejs

noMissingVarFunction

src/content/docs/linter/rules/no-missing-var-function.mdx

latest7.8 KB
Original Source

import { Tabs, TabItem } from '@astrojs/starlight/components';

<Tabs> <TabItem label="CSS" icon="seti:css"> ## Summary - Rule available since: `v1.9.2` - Diagnostic Category: [`lint/correctness/noMissingVarFunction`](/reference/diagnostics#diagnostic-category) - This rule is **recommended**, meaning it is enabled by default. - This rule doesn't have a fix. - The default severity of this rule is [**error**](/reference/diagnostics#error). - Sources: - Same as [`custom-property-no-missing-var-function`](https://github.com/stylelint/stylelint/blob/main/lib/rules/custom-property-no-missing-var-function/README.md)

How to configure

json
{
	"linter": {
		"rules": {
			"correctness": {
				"noMissingVarFunction": "error"
			}
		}
	}
}

Description

Disallow missing var function for css variables.

This rule has the following limitations:

  • It only reports custom properties that are defined and accessible within the same source.
  • It does not check properties that can contain author-defined identifiers.
  • It ignores the following properties:
    • animation
    • animation-name
    • counter-increment
    • counter-reset
    • counter-set
    • grid-column
    • grid-column-end
    • grid-column-start
    • grid-row
    • grid-row-end
    • grid-row-start
    • list-style
    • list-style-type
    • transition
    • transition-property
    • view-transition-name
    • will-change

Examples

Invalid

css
a {
  --foo: red;
  color: --foo;
}
<pre class="language-text"><code class="language-text">code-block.css:3:10 <a href="https://biomejs.dev/linter/rules/no-missing-var-function">lint/correctness/noMissingVarFunction</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Tomato;">✖</span></strong> <span style="color: Tomato;">CSS variables '</span><span style="color: Tomato;"><strong>--foo</strong></span><span style="color: Tomato;">' is used without the 'var()' function</span> <strong>1 │ </strong>a &#123; <strong>2 │ </strong> --foo: red; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>3 │ </strong> color: --foo; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>4 │ </strong>&#125; <strong>5 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">CSS variables should be used with the 'var()' function to ensure proper fallback behavior and browser compatibility.</span> </code></pre>
css
.parent {
  --foo: red;
  .child {
    color: --foo;
  }
}
<pre class="language-text"><code class="language-text">code-block.css:4:12 <a href="https://biomejs.dev/linter/rules/no-missing-var-function">lint/correctness/noMissingVarFunction</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Tomato;">✖</span></strong> <span style="color: Tomato;">CSS variables '</span><span style="color: Tomato;"><strong>--foo</strong></span><span style="color: Tomato;">' is used without the 'var()' function</span> <strong>2 │ </strong> --foo: red; <strong>3 │ </strong> .child &#123; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>4 │ </strong> color: --foo; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>5 │ </strong> &#125; <strong>6 │ </strong>&#125; <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">CSS variables should be used with the 'var()' function to ensure proper fallback behavior and browser compatibility.</span> </code></pre>
css
@property --bar {}

a {
  color: --bar;
}
<pre class="language-text"><code class="language-text">code-block.css:4:10 <a href="https://biomejs.dev/linter/rules/no-missing-var-function">lint/correctness/noMissingVarFunction</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Tomato;">✖</span></strong> <span style="color: Tomato;">CSS variables '</span><span style="color: Tomato;"><strong>--bar</strong></span><span style="color: Tomato;">' is used without the 'var()' function</span> <strong>3 │ </strong>a &#123; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>4 │ </strong> color: --bar; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>5 │ </strong>&#125; <strong>6 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">CSS variables should be used with the 'var()' function to ensure proper fallback behavior and browser compatibility.</span> </code></pre>
css
:root {
  --baz: 0;
}

a {
  --foo: --baz;
}
<pre class="language-text"><code class="language-text">code-block.css:6:10 <a href="https://biomejs.dev/linter/rules/no-missing-var-function">lint/correctness/noMissingVarFunction</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Tomato;">✖</span></strong> <span style="color: Tomato;">CSS variables '</span><span style="color: Tomato;"><strong>--baz</strong></span><span style="color: Tomato;">' is used without the 'var()' function</span> <strong>5 │ </strong>a &#123; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>6 │ </strong> --foo: --baz; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>7 │ </strong>&#125; <strong>8 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">CSS variables should be used with the 'var()' function to ensure proper fallback behavior and browser compatibility.</span> </code></pre>

Valid

css
p {
  color: var(--foo);
}
css
p {
  --foo: red;
  color: var(--foo);
}
css
p {
  color: --foo;
}
css
*:root {
--global: red;
}

a {
    color: var(--global);
}
css
@property --global-value {}
a {
  color: var(--global-value);
}
css
a {
  view-transition-name: --bbb;
}
</TabItem> </Tabs>