New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add declaration-property-value-no-unknown
#6376
Comments
@caneta Thanks for using the template. Unfortunately, Stylelint doesn't support the validation of property-and-value pairs. So I recommend using the $ cat package.json
{
"devDependencies": {
"stylelint": "^14.13.0",
"stylelint-csstree-validator": "^2.0.0"
},
"stylelint": {
"plugins": [
"stylelint-csstree-validator"
],
"rules": {
"csstree/validator": true
}
}
}
$ cat test.css
body {
padding: auto;
}
$ npx stylelint *.css
test.css
2:12 ✖ Invalid value for "padding" csstree/validator
1 problem (1 error, 0 warnings) See also our document about validators. |
This question pops up often, and it feels like an appropriate time to revisit it. As mentioned in our doc that @ybiquitous linked to, there are three types of tools:
Stylelint has historically overlapped with both validators and formatters. We intend to remove our formatting rules so that we don't overlap with formatters. We need a clear vision for how we approach validation too. We split our rules into two groups that: The latter group is made up of 5 subgroups that:
These all apply to valid CSS. They are powerful rules, mostly unique to Stylelint and aligned with our goal of being a linter. The group that avoids errors is made up of two subgroups that check for:
The former group overlaps with validators whereas the latter does not. Like the rules that enforce (non-stylistic) conventions, the ones in the latter subgroup are mostly unique to Stylelint. Additionally, we catch and surfaces broad syntax errors, e.g. unclosed blocks. The complete list of our validating rules is:
Of which, the following overlap with CSSTree: Now that we have the lay of the land, let's decide on a clear vision for validation. I think we have three options:
For the first two, we'd need to improve our documentation to better explain linters and validators, and how Stylelint can be used with a validator like CSSTree. There are pros and cons to all three approaches, for example:
Based on how often people open property/value pair validation issues in our tracker, I feel users are expecting this feature. Especially as some CSS-in-JS libraries like vanilla-extract now support this (via the CSSType library). What do people think? It's a big topic and one that may refine the direction of Stylelint. So let's leave this issue open as a discussion for a while so that we can stew on it. |
Thank you, I have used CSSTree indeed and everything worked as expected. |
@jeddy3 Thanks for the understandable classification of the built-in rules. It might be wonderful if we could reflect this grouping on our documentation somehow. In addition, thanks for providing several future policies for validation rules. I believe many people would like to receive the benefits of validation rules out of the box. So, now I'm inclined to the third option:
although there may be some problems like a larger bundle size or higher maintenance costs. |
SGTM.
I agree. It feels like the pros outweigh the cons, though. Unless anyone knows of an alternative, we can use the CSSTree within the rule for now. We can refactor the implementation if a more appropriate library comes along (or if we end up rolling our own). I believe the CSSTree parser returns everything we need, including what we need for accurate positions (i.e. import { lexer } from "css-tree";
lexer.matchProperty("padding", "10px auto"); {
matched: null,
iterations: 24,
error: SyntaxError [SyntaxMatchError]: Mismatch
at matchSyntax (file:///Users/jeddy3/Projects/temp/node_modules/css-tree/lib/lexer/Lexer.js:82:17)
at Lexer.matchProperty (file:///Users/jeddy3/Projects/temp/node_modules/css-tree/lib/lexer/Lexer.js:324:16)
at file:///Users/jeddy3/Projects/temp/csstree.mjs:7:21
at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
at async Promise.all (index 0)
at async ESMLoader.import (node:internal/modules/esm/loader:533:24)
at async loadESM (node:internal/process/esm_loader:91:5)
at async handleMainPromise (node:internal/modules/run_main:65:12) {
message: 'Mismatch\n' +
' syntax: [ <length> | <percentage> ]{1,4}\n' +
' value: 10px auto\n' +
' -------------^',
rawMessage: 'Mismatch',
syntax: '[ <length> | <percentage> ]{1,4}',
css: '10px auto',
mismatchOffset: 5,
mismatchLength: 4,
offset: 5,
line: 1,
column: 6,
loc: { source: '<unknown>', start: [Object], end: [Object] }
},
getTrace: [Function: getTrace],
isKeyword: [Function: isKeyword],
isProperty: [Function: isProperty],
isType: [Function: isType]
} We'll want to limit the scope of the rule compared to the plugin, e.g. the rule should only check standard CSS for unknown values within properties. We should silently catch all other errors the parser throws, including the error for unknown properties so that the rule doesn't overlap with our existing We may also want to limit our
I've labelled the issue as ready to implement. Please consider contributing if anyone fancies giving this ago. There are steps on how to add a new rule in the Developer guide. |
declaration-property-value-no-unknown
What steps are needed to reproduce the bug?
What Stylelint configuration is needed to reproduce the bug?
How did you run Stylelint?
From VSCode
Which version of Stylelint are you using?
14.13.0
What did you expect to happen?
The editor should tell me that 'auto' is not a permitted value for property 'padding'.
What actually happened?
Nothing, the error is not reported.
Does the bug relate to non-standard syntax?
Nope, tryed in a plain CSS
Proposal to fix the bug
No response
The text was updated successfully, but these errors were encountered: