Skip to content
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 annotation-no-unknown #6155

Merged
merged 8 commits into from
Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/user-guide/rules/list.md
Expand Up @@ -91,6 +91,10 @@ Within each cateogory, the rules are grouped by the [_thing_](http://apps.workfl
- [`no-invalid-double-slash-comments`](../../../lib/rules/no-invalid-double-slash-comments/README.md): Disallow double-slash comments (`//...`) which are not supported by CSS.
- [`no-invalid-position-at-import-rule`](../../../lib/rules/no-invalid-position-at-import-rule/README.md): Disallow invalid position `@import` rules within a stylesheet.

### Annotation
mattxwang marked this conversation as resolved.
Show resolved Hide resolved

- [`annotation-no-unknown`](../../../lib/rules/annotation-no-unknown/README.md): Disallow unknown annotations.

## Enforce conventions

### Alpha-value
Expand Down
2 changes: 2 additions & 0 deletions lib/reference/keywordSets.js
Expand Up @@ -65,6 +65,8 @@ keywordSets.lengthUnits = new Set([

keywordSets.units = uniteSets(keywordSets.nonLengthUnits, keywordSets.lengthUnits);

keywordSets.annotations = new Set(['!important']);

keywordSets.camelCaseFunctionNames = new Set([
'translateX',
'translateY',
Expand Down
74 changes: 74 additions & 0 deletions lib/rules/annotation-no-unknown/README.md
@@ -0,0 +1,74 @@
# annotation-no-unknown

Disallow unknown annotations.

<!-- prettier-ignore -->
```css
a { color: green !imprtant; }
/** ↑
* This annotation */
```

This rule considers solely the `!important` annotation per the [CSS Cascading and Inheritance spec](https://www.w3.org/TR/css-cascade-3/#importance).
mattxwang marked this conversation as resolved.
Show resolved Hide resolved

## Options

### `true`

The following patterns is considered a problem:
mattxwang marked this conversation as resolved.
Show resolved Hide resolved

<!-- prettier-ignore -->
```css
a {
color: green !imprtant;
}
```

The following patterns are _not_ considered problems:
mattxwang marked this conversation as resolved.
Show resolved Hide resolved

<!-- prettier-ignore -->
```css
a {
color: green !important;
}
```

<!-- prettier-ignore -->
```css
a {
color: green !IMPORTANT;
}
```

<!-- prettier-ignore -->
```css
a {
color: green !ImPoRtAnT;
}
```
mattxwang marked this conversation as resolved.
Show resolved Hide resolved

## Optional secondary options

### `ignoreAnnotations: ["/regex/", /regex/, "string"]`

Given:

```json
["/^!my-/", "!custom"]
mattxwang marked this conversation as resolved.
Show resolved Hide resolved
```

The following patterns are _not_ considered problems:

<!-- prettier-ignore -->
```css
a {
color: green !custom;
}
```

<!-- prettier-ignore -->
```css
a {
color: green !my-custom-annotation;
}
```
58 changes: 58 additions & 0 deletions lib/rules/annotation-no-unknown/__tests__/index.js
@@ -0,0 +1,58 @@
'use strict';

const { messages, ruleName } = require('..');

testRule({
ruleName,
config: [
true,
{
ignoreAnnotations: ['!custom', '/^!my-/'],
},
],

accept: [
{
code: 'a { color: green !important; }',
},
{
code: 'a { color: green !IMPORTANT; }',
},
{
code: 'a { color: green !ImPoRtAnT; }',
},
{
code: 'a { color: green !custom; }',
},
{
code: 'a { color: green !my-custom-annotation; }',
},
],

reject: [
{
code: 'a { color: green !imprtant }',
message: messages.rejected('!imprtant'),
line: 1,
column: 18,
endLine: 1,
endColumn: 27,
},
{
code: 'a { color: green !IMPRTANT }',
message: messages.rejected('!IMPRTANT'),
line: 1,
column: 18,
endLine: 1,
endColumn: 27,
},
{
code: 'a { color: green !ImPrTaNt }',
message: messages.rejected('!ImPrTaNt'),
line: 1,
column: 18,
endLine: 1,
endColumn: 27,
},
],
});
88 changes: 88 additions & 0 deletions lib/rules/annotation-no-unknown/index.js
@@ -0,0 +1,88 @@
'use strict';

const valueParser = require('postcss-value-parser');
const getDeclarationValue = require('../../utils/getDeclarationValue');

const keywordSets = require('../../reference/keywordSets');
const optionsMatches = require('../../utils/optionsMatches');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const validateOptions = require('../../utils/validateOptions');
const { isRegExp, isString } = require('../../utils/validateTypes');

const ruleName = 'annotation-no-unknown';

const messages = ruleMessages(ruleName, {
rejected: (annotation) => `Unexpected unknown annotation "${annotation}"`,
});

const meta = {
url: 'https://stylelint.io/user-guide/rules/list/annotation-no-unknown',
};

/** @type {import('stylelint').Rule} */
const rule = (primary, secondaryOptions) => {
return (root, result) => {
const validOptions = validateOptions(
result,
ruleName,
{ actual: primary },
{
actual: secondaryOptions,
possible: {
ignoreAnnotations: [isString, isRegExp],
},
optional: true,
},
);

if (!validOptions) {
return;
}

root.walkDecls(checkStatement);

/**
* @param {import('postcss').Declaration} decl
*/
function checkStatement(decl) {
mattxwang marked this conversation as resolved.
Show resolved Hide resolved
if (!decl.value.includes('!')) return;
mattxwang marked this conversation as resolved.
Show resolved Hide resolved

const parsedValue = valueParser(getDeclarationValue(decl));

parsedValue.walk((node) => {
if (!isAnnotation(node)) return;

const value = node.value;

if (optionsMatches(secondaryOptions, 'ignoreAnnotations', value)) {
return;
}

if (keywordSets.annotations.has(value.toLowerCase())) {
return;
mattxwang marked this conversation as resolved.
Show resolved Hide resolved
}

report({
message: messages.rejected(value),
node: decl,
result,
ruleName,
word: value,
});
});
}

/**
* @param {valueParser.Node} node
*/
function isAnnotation(node) {
return node.type === 'word' && node.value.startsWith('!');
}
};
};

rule.ruleName = ruleName;
rule.messages = messages;
rule.meta = meta;
module.exports = rule;
1 change: 1 addition & 0 deletions lib/rules/index.js
Expand Up @@ -7,6 +7,7 @@ const importLazy = _importLazy(require);
/** @type {typeof import('stylelint').rules} */
const rules = {
'alpha-value-notation': importLazy('./alpha-value-notation'),
'annotation-no-unknown': importLazy('./annotation-no-unknown'),
'at-rule-allowed-list': importLazy('./at-rule-allowed-list'),
'at-rule-disallowed-list': importLazy('./at-rule-disallowed-list'),
'at-rule-empty-line-before': importLazy('./at-rule-empty-line-before'),
Expand Down