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 keyframe-block-no-duplicate-selectors #6024

Merged
merged 7 commits into from Apr 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 @@ -50,6 +50,10 @@ Within each cateogory, the rules are grouped by the [_thing_](http://apps.workfl

- [`keyframe-declaration-no-important`](../../../lib/rules/keyframe-declaration-no-important/README.md): Disallow `!important` within keyframe declarations.

### Keyframe blocks
jeddy3 marked this conversation as resolved.
Show resolved Hide resolved

- [`keyframe-block-no-duplicate-selectors`](../../../lib/rules/keyframe-block-no-duplicate-selectors/README.md): Disallow duplicate selectors within keyframe blocks.

### Declaration block

- [`declaration-block-no-duplicate-custom-properties`](../../../lib/rules/declaration-block-no-duplicate-custom-properties/README.md): Disallow duplicate custom properties within declaration blocks.
Expand Down
1 change: 1 addition & 0 deletions lib/rules/index.js
Expand Up @@ -119,6 +119,7 @@ const rules = {
'function-url-scheme-disallowed-list': importLazy('./function-url-scheme-disallowed-list'),
'function-whitespace-after': importLazy('./function-whitespace-after'),
'hue-degree-notation': importLazy('./hue-degree-notation'),
'keyframe-block-no-duplicate-selectors': importLazy('./keyframe-block-no-duplicate-selectors'),
'keyframe-declaration-no-important': importLazy('./keyframe-declaration-no-important'),
'keyframes-name-pattern': importLazy('./keyframes-name-pattern'),
'length-zero-no-unit': importLazy('./length-zero-no-unit'),
Expand Down
40 changes: 40 additions & 0 deletions lib/rules/keyframe-block-no-duplicate-selectors/README.md
@@ -0,0 +1,40 @@
# keyframe-block-no-duplicate-selectors

Disallow duplicate selectors within keyframe blocks.

<!-- prettier-ignore -->
```css
@keyframes foo {
0% {}
0% {} /* This duplicated selector */
100% {}
}
mattxwang marked this conversation as resolved.
Show resolved Hide resolved
```

## Options

### `true`

The following patterns are considered problems:

<!-- prettier-ignore -->
```css
@keyframes foo { 0% {} 0% {} 100% {} }
mattxwang marked this conversation as resolved.
Show resolved Hide resolved
```

<!-- prettier-ignore -->
```css
@keyframes foo { 0% {} 0%, 100% {} }
mattxwang marked this conversation as resolved.
Show resolved Hide resolved
```

The following patterns are _not_ considered problems:

<!-- prettier-ignore -->
```css
@keyframes foo { 0% {} 100% {} }
```

<!-- prettier-ignore -->
```css
@keyframes foo { from {} to {} }
```
40 changes: 40 additions & 0 deletions lib/rules/keyframe-block-no-duplicate-selectors/__tests__/index.js
@@ -0,0 +1,40 @@
'use strict';

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

testRule({
ruleName,
config: [true],

accept: [
{
code: '@keyframes foo { 0% {} 100% {} }',
},
{
code: '@keyframes foo { from {} to {} }',
},
],

reject: [
mattxwang marked this conversation as resolved.
Show resolved Hide resolved
{
code: '@keyframes foo { from {} to {} to {} }',
message: messages.rejected('to'),
},
{
code: '@keyframes foo { 0% {} 0% {} 100% {} }',
message: messages.rejected('0%'),
},
{
code: '@-webkit-keyframes foo { 0% {} 0% {} 100% {} }',
message: messages.rejected('0%'),
},
{
code: '@-moz-keyframes foo { 0% {} 0% {} 100% {} }',
message: messages.rejected('0%'),
},
{
code: '@keyframes foo { 0% {} 0%, 100% {} }',
message: messages.rejected('0%'),
},
],
});
mattxwang marked this conversation as resolved.
Show resolved Hide resolved
56 changes: 56 additions & 0 deletions lib/rules/keyframe-block-no-duplicate-selectors/index.js
@@ -0,0 +1,56 @@
'use strict';

const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const validateOptions = require('../../utils/validateOptions');

const ruleName = 'keyframe-block-no-duplicate-selectors';

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

const meta = {
url: 'https://stylelint.io/user-guide/rules/list/keyframe-block-no-duplicate-selectors',
};

/** @type {import('stylelint').Rule} */
const rule = (primary) => {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, { actual: primary });

if (!validOptions) {
return;
}

root.walkAtRules(/^(-(moz|webkit)-)?keyframes$/i, (atRuleKeyframes) => {
const selectors = new Set();

atRuleKeyframes.walkRules((keyframeRule) => {
const ruleSelectors = keyframeRule.selectors;

jeddy3 marked this conversation as resolved.
Show resolved Hide resolved
ruleSelectors.forEach((selector) => {
const isDuplicate = selectors.has(selector);

if (isDuplicate) {
report({
message: messages.rejected(selector),
node: keyframeRule,
result,
ruleName,
});

return;
}

selectors.add(selector);
});
});
});
};
};

rule.ruleName = ruleName;
rule.messages = messages;
rule.meta = meta;
module.exports = rule;