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 selector-attribute-name-disallowed-list #4992

Merged
merged 6 commits into from Nov 17, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
1 change: 1 addition & 0 deletions docs/user-guide/rules/list.md
Expand Up @@ -173,6 +173,7 @@ Grouped first by the following categories and then by the [_thing_](http://apps.
- [`selector-attribute-operator-blacklist`](../../../lib/rules/selector-attribute-operator-blacklist/README.md): Specify a list of disallowed attribute operators. **(deprecated)**
- [`selector-attribute-operator-disallowed-list`](../../../lib/rules/selector-attribute-operator-disallowed-list/README.md): Specify a list of disallowed attribute operators.
- [`selector-attribute-operator-whitelist`](../../../lib/rules/selector-attribute-operator-whitelist/README.md): Specify a list of allowed attribute operators. **(deprecated)**
- [`selector-attribute-name-disallowed-list`](../../../lib/rules/selector-attribute-name-disallowed-list/README.md): Specify a list of disallowed attribute names.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's order this alphabetically, i.e. put it before the operator rules.

- [`selector-class-pattern`](../../../lib/rules/selector-class-pattern/README.md): Specify a pattern for class selectors.
- [`selector-combinator-allowed-list`](../../../lib/rules/selector-combinator-allowed-list/README.md): Specify a list of allowed combinators.
- [`selector-combinator-blacklist`](../../../lib/rules/selector-combinator-blacklist/README.md): Specify a list of disallowed combinators. **(deprecated)**
Expand Down
3 changes: 3 additions & 0 deletions lib/rules/index.js
Expand Up @@ -274,6 +274,9 @@ const rules = {
'selector-attribute-operator-whitelist': importLazy(() =>
require('./selector-attribute-operator-whitelist'),
)(),
'selector-attribute-name-disallowed-list': importLazy(() =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's order this alphabetically, i.e. put it before the operator rules.

require('./selector-attribute-name-disallowed-list'),
)(),
'selector-attribute-quotes': importLazy(() => require('./selector-attribute-quotes'))(),
'selector-class-pattern': importLazy(() => require('./selector-class-pattern'))(),
'selector-combinator-allowed-list': importLazy(() =>
Expand Down
49 changes: 49 additions & 0 deletions lib/rules/selector-attribute-name-disallowed-list/README.md
@@ -0,0 +1,49 @@
# selector-attribute-name-disallowed-list

Specify a list of disallowed attribute names.

<!-- prettier-ignore -->
```css
[class~="foo"] {}
/** ↑
* This name */
```

## Options

`array|string`: `["array", "of", "names"]|"name"`

Given:

```
["class", "id"]
```

The following patterns are considered violations:

<!-- prettier-ignore -->
```css
[class*="foo"] {}
```

<!-- prettier-ignore -->
```css
[id~="bar"] {}
```

The following patterns are _not_ considered violations:

<!-- prettier-ignore -->
```css
[lang~="en-us"] {}
```

<!-- prettier-ignore -->
```css
[target="_blank"] {}
```

<!-- prettier-ignore -->
```css
[href$=".bar"] {}
```
@@ -0,0 +1,51 @@
'use strict';

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

testRule({
ruleName,

config: ['class', 'id'],

accept: [
{
code: 'a[title] { }',
},
{
code: 'a[target="_blank"] { }',
},
{
code: 'a[href$=".pdf"] { }',
},
{
code: 'div[lang~="en-us"] { }',
},
],

reject: [
{
code: '[class*="foo"] { }',
message: messages.rejected('class'),
line: 1,
column: 2,
},
{
code: '[ class*="foo" ] { }',
message: messages.rejected('class'),
line: 1,
column: 3,
},
{
code: '[class *= "foo"] { }',
message: messages.rejected('class'),
line: 1,
column: 2,
},
{
code: '[id~="bar"] { }',
message: messages.rejected('id'),
line: 1,
column: 2,
},
],
});
65 changes: 65 additions & 0 deletions lib/rules/selector-attribute-name-disallowed-list/index.js
@@ -0,0 +1,65 @@
// @ts-nocheck

'use strict';

const _ = require('lodash');
const isStandardSyntaxRule = require('../../utils/isStandardSyntaxRule');
const parseSelector = require('../../utils/parseSelector');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const validateOptions = require('../../utils/validateOptions');

const ruleName = 'selector-attribute-name-disallowed-list';

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

function rule(listInput) {
const list = [].concat(listInput);

return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: list,
possible: [_.isString],
});

if (!validOptions) {
return;
}

root.walkRules((ruleNode) => {
if (!isStandardSyntaxRule(ruleNode)) {
return;
}

if (!ruleNode.selector.includes('[') || !ruleNode.selector.includes('=')) {
return;
}

parseSelector(ruleNode.selector, result, ruleNode, (selectorTree) => {
selectorTree.walkAttributes((attributeNode) => {
const attributeName = attributeNode.qualifiedAttribute;

if (!attributeName || (attributeName && !list.includes(attributeName))) {
return;
}

report({
message: messages.rejected(attributeName),
node: ruleNode,
index: attributeNode.sourceIndex + attributeNode.offsetOf('attribute'),
result,
ruleName,
});
});
});
});
};
}

rule.primaryOptionArray = true;

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