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 all 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
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ Grouped first by the following categories and then by the [_thing_](http://apps.

### Selector

- [`selector-attribute-name-disallowed-list`](../../../lib/rules/selector-attribute-name-disallowed-list/README.md): Specify a list of disallowed attribute names.
- [`selector-attribute-operator-allowed-list`](../../../lib/rules/selector-attribute-operator-allowed-list/README.md): Specify a list of allowed attribute operators.
- [`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.
Expand Down
3 changes: 3 additions & 0 deletions lib/rules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,9 @@ const rules = {
'selector-attribute-brackets-space-inside': importLazy(() =>
require('./selector-attribute-brackets-space-inside'),
)(),
'selector-attribute-name-disallowed-list': importLazy(() =>
require('./selector-attribute-name-disallowed-list'),
)(),
'selector-attribute-operator-allowed-list': importLazy(() =>
require('./selector-attribute-operator-allowed-list'),
)(),
Expand Down
54 changes: 54 additions & 0 deletions lib/rules/selector-attribute-name-disallowed-list/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# selector-attribute-name-disallowed-list

Specify a list of disallowed attribute names.

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

## Options

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

Given:

```
["class", "id", "/^data-/"]
```

The following patterns are considered violations:

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

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

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

The following patterns are _not_ considered violations:

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

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

<!-- prettier-ignore -->
```css
[href$=".bar"] {}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
'use strict';

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

testRule({
ruleName,

config: ['class', 'id', '/^data-/'],

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,
},
{
code: '[data-foo*="bar"] { }',
message: messages.rejected('data-foo'),
line: 1,
column: 2,
},
{
code: '[ data-foo *= "bar" ] { }',
message: messages.rejected('data-foo'),
line: 1,
column: 3,
},
],
});
66 changes: 66 additions & 0 deletions lib/rules/selector-attribute-name-disallowed-list/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// @ts-nocheck

'use strict';

const _ = require('lodash');
const isStandardSyntaxRule = require('../../utils/isStandardSyntaxRule');
const matchesStringOrRegExp = require('../../utils/matchesStringOrRegExp');
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, _.isRegExp],
});

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 (!matchesStringOrRegExp(attributeName, list)) {
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;