-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: no-invalid-properties rule (#11)
* feat: no-invalid-property-values rule * Fix README format * Fix typings errors * Fix tests * feat: no-invalid-properties rule * Update docs * Ignore descriptor declarations * Update tests/rules/no-invalid-properties.test.js Co-authored-by: Milos Djermanovic <[email protected]> * Update tests/rules/no-invalid-properties.test.js Co-authored-by: Milos Djermanovic <[email protected]> * Update docs/rules/no-invalid-properties.md Co-authored-by: Milos Djermanovic <[email protected]> --------- Co-authored-by: Milos Djermanovic <[email protected]>
- Loading branch information
1 parent
4c0761b
commit 9b80bdd
Showing
12 changed files
with
1,353 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# no-invalid-properties | ||
|
||
Disallow invalid properties. | ||
|
||
## Background | ||
|
||
CSS has a defined set of known properties that are expected to have specific values. While CSS may parse correctly, that doesn't mean that the properties are correctly matched with their values. For example, the following is syntactically valid CSS code: | ||
|
||
```css | ||
a { | ||
display: black; | ||
} | ||
``` | ||
|
||
The property `display` doesn't accept a color for its value, so while this code will parse without error, it's still invalid CSS. | ||
|
||
Similarly, as long as a property name is syntactically valid, it will parse without error even if it's not a known property. For example: | ||
|
||
```css | ||
a { | ||
ccolor: black; | ||
} | ||
``` | ||
|
||
Here, `ccolor` is a syntactically valid identifier even though it will be ignored by browsers. Such errors are often caused by typos. | ||
|
||
## Rule Details | ||
|
||
This rule warns when it finds a CSS property that doesn't exist or a value that doesn't match with the property name in the CSS specification (custom properties such as `--my-color` are ignored). The property data is provided via the [CSSTree](https://github.com/csstree/csstree) project. | ||
|
||
Examples of incorrect code: | ||
|
||
```css | ||
a { | ||
display: black; | ||
ccolor: black; | ||
} | ||
|
||
body { | ||
overflow: 100%; | ||
bg: red; | ||
} | ||
``` | ||
|
||
## When Not to Use It | ||
|
||
If you aren't concerned with invalid properties, then you can safely disable this rule. | ||
|
||
## Prior Art | ||
|
||
- [`declaration-property-value-no-unknown`](https://stylelint.io/user-guide/rules/declaration-property-value-no-unknown/) | ||
|
||
- [`property-no-unknown`](https://stylelint.io/user-guide/rules/property-no-unknown) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/** | ||
* @fileoverview Rule to prevent invalid properties in CSS. | ||
* @author Nicholas C. Zakas | ||
*/ | ||
|
||
//----------------------------------------------------------------------------- | ||
// Imports | ||
//----------------------------------------------------------------------------- | ||
|
||
import { lexer } from "css-tree"; | ||
|
||
//----------------------------------------------------------------------------- | ||
// Type Definitions | ||
//----------------------------------------------------------------------------- | ||
|
||
/** @typedef {import("css-tree").SyntaxMatchError} SyntaxMatchError */ | ||
|
||
//----------------------------------------------------------------------------- | ||
// Helpers | ||
//----------------------------------------------------------------------------- | ||
|
||
/** | ||
* Determines if an error is a syntax match error. | ||
* @param {Object} error The error object from the CSS parser. | ||
* @returns {error is SyntaxMatchError} True if the error is a syntax match error, false if not. | ||
*/ | ||
function isSyntaxMatchError(error) { | ||
return typeof error.css === "string"; | ||
} | ||
|
||
//----------------------------------------------------------------------------- | ||
// Rule Definition | ||
//----------------------------------------------------------------------------- | ||
|
||
export default { | ||
meta: { | ||
type: "problem", | ||
|
||
docs: { | ||
description: "Disallow invalid properties.", | ||
recommended: true, | ||
}, | ||
|
||
messages: { | ||
invalidPropertyValue: | ||
"Invalid value '{{value}}' for property '{{property}}'. Expected {{expected}}.", | ||
unknownProperty: "Unknown property '{{property}}' found.", | ||
}, | ||
}, | ||
|
||
create(context) { | ||
return { | ||
"Rule > Block > Declaration"(node) { | ||
// don't validate custom properties | ||
if (node.property.startsWith("--")) { | ||
return; | ||
} | ||
|
||
const { error } = lexer.matchDeclaration(node); | ||
|
||
if (error) { | ||
// validation failure | ||
if (isSyntaxMatchError(error)) { | ||
context.report({ | ||
loc: error.loc, | ||
messageId: "invalidPropertyValue", | ||
data: { | ||
property: node.property, | ||
value: error.css, | ||
expected: error.syntax, | ||
}, | ||
}); | ||
return; | ||
} | ||
|
||
// unknown property | ||
context.report({ | ||
loc: { | ||
start: node.loc.start, | ||
end: { | ||
line: node.loc.start.line, | ||
column: | ||
node.loc.start.column + | ||
node.property.length, | ||
}, | ||
}, | ||
messageId: "unknownProperty", | ||
data: { | ||
property: node.property, | ||
}, | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.