stylelint: Fix `function-no-unknown` false negatives for functions in at-rule preludes

What minimal example or steps are needed to reproduce the bug?

@container scroll-state() {}
@container style() {}

What minimal configuration is needed to reproduce the bug?

{
  "rules": {
    "function-no-unknown": true
  }
}

How did you run Stylelint?

https://stylelint.io/demo

What did you expect to happen?

Unexpected unknown function "scroll-state" Unexpected unknown function "style"

Do you have a proposal to fix the bug?

Support

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Comments: 18 (18 by maintainers)

Most upvoted comments

In another way, we may add container-query-no-invalid like media-query-no-invalid.

I think it is very hard to avoid overlap here with other rules, but maybe this is fine?

I would also expect function-no-unknown to check at-rules, while at the same time I like more specific and specialized rules like media-query-no-invalid and declaration-property-value-no-unknown. Both kinds of rules have value and can coexist imho.

I think that a specialized rule will always give better feedback because it should be able to determine if a function exists and if it is used correctly in a given context.

While function-no-unknown can only give superficial feedback about if a function is known. We could have some conditions to only make some functions valid in some places but it has to be more specific than only looking at the at-rule name.

Some more complex cases:

  • @import supports(<everything here, including selector()>) layer(...) scope(:is(foo)) (width: calc(...))
  • @container (width: calc(...)) (only math functions)
  • @container style(--foo: rgb(...)) (any value related functions)
  • …

Thanks for the info. Since some functions are already supported in at-rules, adding at-rule support to function-no-unknown may be possible.

The implementation might be as follows:

// .../function-no-unknown/index.mjs
import valueParser from 'postcss-value-parser';

root.walkAtRules(/^(container|supports)$/i, (atRule) => {
	const valueNodes = valueParser(getAtRuleParams(atRule)).nodes;

	switch (atRule.name.toLowerCase()) {
		case "container":
			// Check unknown functions in `@container` rule...
		case "supports":
			// Check unknown functions in `@supports` rule...
	}
});

Are there other points we missed? If not, we could go forward.

I wonder if there are other cases of functions in at-rules. 🤔

There are 3 more AFAIK:

  • font-format()
  • font-tech()
  • selector()

see https://developer.mozilla.org/en-US/docs/Web/CSS/@supports#function_syntax

I’d like to wait and see how the experimental spec goes for a while.

style() is not really experimental at this point. cf https://caniuse.com/css-container-queries-style

I agree for scroll-state() though.