Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add selector-attribute-name-disallowed-list (#4992)
- Loading branch information
Showing
5 changed files
with
187 additions
and
0 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
54 changes: 54 additions & 0 deletions
54
lib/rules/selector-attribute-name-disallowed-list/README.md
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,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"] {} | ||
``` |
63 changes: 63 additions & 0 deletions
63
lib/rules/selector-attribute-name-disallowed-list/__tests__/index.js
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,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
66
lib/rules/selector-attribute-name-disallowed-list/index.js
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,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; |